【问题标题】:There is no row at position 0;位置 0 没有行;
【发布时间】:2016-01-05 18:46:09
【问题描述】:

当我尝试在我的行中获取分数的特定数据时,帮助我不断收到此错误。

我的数据库中有这些数据:

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'的名字和分数就可以了; 但在第二列“基思”和“0”。 我的代码将我标记为错误“位置 0 处没有行”;

string.concat 方法()的问题似乎是什么?

【问题讨论】:

  • datatable.Rows[0]的问题,不是string.concat的问题。确保您的查询返回数据表中名称“Keith”的数据
  • 这种方法没有问题。您的 SQL 查询没有返回任何数据,因此表中没有任何行。另外,如果你只是从数据库中加载数据,不要使用DataTable,它是非常低效的。只需使用IDataReader
  • 或使用 .Fill 方法或查看 DataTable.Load Method 的 MSDN 链接
  • 所以你已经在 dt 中执行了阅读器和加载,你正试图从 dataTable 而不是 dt 中获取值。
  • @Nikita 是的先生,只是为了在我的表中获取数据“分数”,

标签: c# string-concatenation


【解决方案1】:

只使用 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
            {

            }

        }

【讨论】:

  • 如果你确定它只返回一行,使用 if 条件。如果返回多行,请使用 while 循环并读取每一行。 while(reader.Read() { reader.GetString(reader.GetOrdinal("Score"))})
【解决方案2】:

使用此代码

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");
  }
}

【讨论】:

    【解决方案3】:

    试试这个:

    if(dt.Rows.Count>0)
    {
    str1 = String.Concat(str1, (dataTable.Rows[0]["Score"].ToString()));
    }
    

    【讨论】:

    • 先生,我已经尝试过这段代码,(参见上面的编辑)但条件转到 catch 方法并且没有报告数据。
    • 你得到的行数是多少?
    • 第二排Score先生0
    • 试试这个并告诉我 RowCount 的值 int RowCount=datatable.Rows.Count;如果 RowCount 变量的值为 0,那么您的查询不会返回任何行。
    • RowCount 不包含内置函数,RowCount 声明是 int 吗? ,,先生上面的代码可以编辑吗?
    猜你喜欢
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    相关资源
    最近更新 更多