【问题标题】:Why am I getting 0 results in my datagridView?为什么我在 datagridView 中得到 0 个结果?
【发布时间】:2013-01-25 11:50:24
【问题描述】:

C#代码:

command = new SqlCommand(null, connection);
command = createSQLQuery(command); // returns a valid SQL Query that returns results in SQL Management Studio 2012
//dataGridView1.DataSource = GetData(command);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
connection.Close();
dataGridView1.DataSource = GetData(command);
debugMySQL();

public DataTable GetData(SqlCommand cmd) {
    //SqlConnection con = new SqlConnection(connString);
    //SqlCommand cmd = new SqlCommand(sqlcmdString, cn);

    SqlDataAdapter da = new SqlDataAdapter(cmd);
    connection.Open();
    DataTable dt = new DataTable();
    da.Fill(dt);
    connection.Close();
    return dt;
}

我正在使用带有 WinForm 的 C# 并使用 Visual Studio 2012 进行开发。

【问题讨论】:

  • 会不会是在调用GetData之前调用connection.Close();
  • 这是个好主意,所以我在设置数据源后关闭了连接。然后我在 da.Fill(dt) 收到一个错误,说“已经有一个打开的 DataReader 与此命令关联,必须先关闭。”
  • 第一个代码块调用GetData 两次。这是故意的吗?
  • 没有。直到现在我才看到。谢谢:)
  • 它仍然在 da.Fill(dt) 处产生相同的错误,说“已经有一个打开的 DataReader...”

标签: c# sql-server winforms datatable


【解决方案1】:

您的问题比较模糊,但我会尽力回答。

我认为,通常情况下,您需要在执行该命令之前定义要传递到 SqlCommand 的命令类型。

command.Commandtype = CommandType.StoredProcedure;
(要么)
command.CommandText = CommandType.Text;

之后您可以将 SQL 查询设置为字符串值。
command.CommandText = ""; // your SQL query, or the name of the stored procedure<br>

另外,您不需要设置网格的DataSource 两次。只需要在得到 SQL 查询执行结果返回后进行设置。
然而,您收到的结果是您的选择。但是,您似乎使用GetData() 方法和command.ExecuteReader() 方法执行了两次命令。此外,您还设置了两次网格的DataSource

我认为即使您设法正确设置命令对象,由于您的双重执行,数据可能会在它们之间以某种方式丢失。

顺便说一下,createSQLQuery() 方法返回什么数据类型。你说它返回了一个有效的 SQL 查询,但它是在 string 中返回一个有效的 sql 查询值,还是你的自定义命令对象解析成 SqlCommand

我将包含我所知道的最好的可操作过程,用于从 SQL 数据库中读取数据并将该数据绑定到网格控件中。

// I suppose you get the connection object as 'con'
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "...";   // You desired SQL query goes here, or if you want to execute a stored procedure, set the command type as 'StoredProcedure' and text as sp name.

SqlDataAdapter dap = new SqlDataAdapter();
dap.SelectCommand = cmd;
DataTable tbl = new DataTable();
dap.Fill(tbl);
<br>
if (tbl.Rows.Count > 0)
{
    grid.DataSource = tbl;
    grid.DataBind();
}

我建议您以DataTable 而不是SqlDataReader 接收结果。因为DataTable 更加灵活和通用。

【讨论】:

  • P.S.,如果你用SqlDataAdapter填充DataTable,你甚至不需要打开和关闭连接。
  • 还有,你的 sql 查询怎么样?它是否按您的预期正常工作?可能不是编码问题,而是查询执行……
  • 查询工作正常。我在 SQL Management Studio 中运行了几次。 debugMySQL 的代码向我展示了应该执行的查询,它也可以正常工作。
  • tbl.Rows.Count 看起来总是 0。我该如何解决?
  • 即使sp有参数,你也可以这样写来设置参数值...cmd.Parameters.AddWithValue("@param", yourvalue); ..对于每个参数。
【解决方案2】:
        var command = new SqlCommand();
        command.CommandText = createSQLQuery(command);
        command.Connection = connection;
        dataGridView1.DataSource = GetData(command);

        debugMySQL();

    }

    public DataTable GetData(SqlCommand cmd)
    {
        //SqlConnection con = new SqlConnection(connString);
        //SqlCommand cmd = new SqlCommand(sqlcmdString, cn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }

【讨论】:

    【解决方案3】:

    在您的 return dt; 之前,尝试添加以下内容:

    if(dt.row.count > 0)
    {
        var something =dt[0][0];
    }
    

    然后,在if 语句中放置一个断点,看看是否有任何返回。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多