位置 0 处没有行;
There is no row at position 0;
帮助我在尝试获取行中分数的特定数据时一直出现此错误。
我的数据库中有以下数据:
PlayerName Score
qwe 20
keith 0
我在 winform 中有这段代码:
using (SqlConnection conn = new SqlConnection("Data Source=Rogue;Initial Catalog=SoftProject;Integrated Security=True"))
{
conn.Open();
try
{
String str1 = "";
using (SqlCommand cmd = new SqlCommand("Select PlayerName,Score from PlayerData where PlayerName = @player", conn))
{
cmd.Parameters.AddWithValue("@player", txtPlayer.Text);
DataTable dataTable= new DataTable();
dataTable.Load(cmd.ExecuteReader());
if(dataTable.Rows.Count>0)
{
// concatenate the two string and get the table score row
str1 = String.Concat(str1, (dataTable.Rows[0]["Score"].ToString()));
}
else
{
//Report error
}
}
conn.Close();
}
catch()
{
MessageBox.Show("No data");
}
}
在此处的这段代码中,这使我的系统出现错误:
str1 = String.Concat(str1, (dataTable.Rows[0]["Score"].ToString()));
得到'qwe'和'20'的名字和分数就可以了;
但在第二列 'keith' 和 '0'。
我的代码将我标记为错误 "There is no row at position 0";
string.concat 方法 () 似乎有什么问题?
试试这个:
if(dt.Rows.Count>0)
{
str1 = String.Concat(str1, (dataTable.Rows[0]["Score"].ToString()));
}
只使用 SqlDatareader 就足够了
using (SqlConnection conn = new SqlConnection("Data Source=Rogue;Initial Catalog=SoftProject;Integrated Security=True"))
{
conn.Open();
try
{
String str1 = "";
using (SqlCommand cmd = new SqlCommand("Select PlayerName,Score from PlayerData where PlayerName = @player", conn))
{
cmd.Parameters.AddWithValue("@player", txtPlayer.Text);
SqlDataReader reader = cmd.ExecuteReader();
if(reader.Read())
{
str1 = String.Concat(str1, reader.GetString(reader.GetOrdinal("Score")));
}
else
{
}
}
conn.Close();
}
catch
{
}
}
使用此代码
using (SqlConnection conn = new SqlConnection("Data Source=Rogue;Initial Catalog=SoftProject;Integrated Security=True"))
{
conn.Open();
try
{
String str1 = "";
using (SqlCommand cmd = new SqlCommand("Select PlayerName,Score from PlayerData where PlayerName = @player", conn))
{
cmd.Parameters.AddWithValue("@player", txtPlayer.Text);
DataSet ds= new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds);
if(ds.Tables[0].Rows.Count>0)
{
// concatenate the two string and get the table score row
str1 += ds.Tables[0].Rows[0]["Score"].ToString();
}
else
{
//Report error
}
}
conn.Close();
}
catch()
{
MessageBox.Show("No data");
}
}
帮助我在尝试获取行中分数的特定数据时一直出现此错误。
我的数据库中有以下数据:
PlayerName Score
qwe 20
keith 0
我在 winform 中有这段代码:
using (SqlConnection conn = new SqlConnection("Data Source=Rogue;Initial Catalog=SoftProject;Integrated Security=True"))
{
conn.Open();
try
{
String str1 = "";
using (SqlCommand cmd = new SqlCommand("Select PlayerName,Score from PlayerData where PlayerName = @player", conn))
{
cmd.Parameters.AddWithValue("@player", txtPlayer.Text);
DataTable dataTable= new DataTable();
dataTable.Load(cmd.ExecuteReader());
if(dataTable.Rows.Count>0)
{
// concatenate the two string and get the table score row
str1 = String.Concat(str1, (dataTable.Rows[0]["Score"].ToString()));
}
else
{
//Report error
}
}
conn.Close();
}
catch()
{
MessageBox.Show("No data");
}
}
在此处的这段代码中,这使我的系统出现错误:
str1 = String.Concat(str1, (dataTable.Rows[0]["Score"].ToString()));
得到'qwe'和'20'的名字和分数就可以了; 但在第二列 'keith' 和 '0'。 我的代码将我标记为错误 "There is no row at position 0";
string.concat 方法 () 似乎有什么问题?
试试这个:
if(dt.Rows.Count>0)
{
str1 = String.Concat(str1, (dataTable.Rows[0]["Score"].ToString()));
}
只使用 SqlDatareader 就足够了
using (SqlConnection conn = new SqlConnection("Data Source=Rogue;Initial Catalog=SoftProject;Integrated Security=True"))
{
conn.Open();
try
{
String str1 = "";
using (SqlCommand cmd = new SqlCommand("Select PlayerName,Score from PlayerData where PlayerName = @player", conn))
{
cmd.Parameters.AddWithValue("@player", txtPlayer.Text);
SqlDataReader reader = cmd.ExecuteReader();
if(reader.Read())
{
str1 = String.Concat(str1, reader.GetString(reader.GetOrdinal("Score")));
}
else
{
}
}
conn.Close();
}
catch
{
}
}
使用此代码
using (SqlConnection conn = new SqlConnection("Data Source=Rogue;Initial Catalog=SoftProject;Integrated Security=True"))
{
conn.Open();
try
{
String str1 = "";
using (SqlCommand cmd = new SqlCommand("Select PlayerName,Score from PlayerData where PlayerName = @player", conn))
{
cmd.Parameters.AddWithValue("@player", txtPlayer.Text);
DataSet ds= new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds);
if(ds.Tables[0].Rows.Count>0)
{
// concatenate the two string and get the table score row
str1 += ds.Tables[0].Rows[0]["Score"].ToString();
}
else
{
//Report error
}
}
conn.Close();
}
catch()
{
MessageBox.Show("No data");
}
}