Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何基于SQL数据库值在Visual Studio C中的WinForm上设置复选框状态?_C#_Sql Server - Fatal编程技术网

C# 如何基于SQL数据库值在Visual Studio C中的WinForm上设置复选框状态?

C# 如何基于SQL数据库值在Visual Studio C中的WinForm上设置复选框状态?,c#,sql-server,C#,Sql Server,我试图根据从相应的SQL Server数据库检索到的值,将VS2017中C Windows窗体上的复选框设置为选中或未选中状态。我无法设置VS2017中Winform上绘制的复选框的checked属性,其中包含SQL Server表中的相应值 我在Visual Studio中有一个C Windows窗体,它与SQL Server中的SQL数据库相关联。此表单上有多个复选框,对应SQL数据库中的w/列名。这些列是SQL Server中的数据类型位。 Winform复选框是在窗体上绘制的独立复选框。

我试图根据从相应的SQL Server数据库检索到的值,将VS2017中C Windows窗体上的复选框设置为选中或未选中状态。我无法设置VS2017中Winform上绘制的复选框的checked属性,其中包含SQL Server表中的相应值

我在Visual Studio中有一个C Windows窗体,它与SQL Server中的SQL数据库相关联。此表单上有多个复选框,对应SQL数据库中的w/列名。这些列是SQL Server中的数据类型位。 Winform复选框是在窗体上绘制的独立复选框。我尝试了多种方法来完成看似简单的任务,但到目前为止还无法加载复选框状态

*SQL Server信息* SQL SERVER数据库名为companys,数据表名为Customers,感兴趣的特定列名为custNeed,它有一个位数据类型

companys.dbo.Customers.custNeed

VSC信息

下面是我当前对此方法的C代码尝试。此代码仅用于一个特定的复选框。此特定记录的值为1,VS2017中的默认检查状态为false

private void loadCheckMarks()
{
    //connect to SQL DB, then build adapter to fill datatable
    string query = "SELECT custNeed FROM Companies.dbo.Customers;";
    SqlDataAdapter da = new SqlDataAdapter(query, connection);
    connection.ConnectionString = connStr;
    connection.Open();
    DataTable dt = new DataTable();
    da.Fill(dt);

    //establish reader
    SqlDataReader reader;
    cmd.CommandText = "Select custNeed FROM Customers WHERE ID = 1000001;";
    reader = cmd.ExecuteReader();

    //set Checked status of Checkbox
    checkBox1.Checked = (bool)reader["custNeed"];
}

我在单击按钮时调用了该方法,并希望复选框从unchecked变为check,但什么也没发生

我不确定您在哪里定义cmd,但这里有很多内容与读取该查询和设置复选框无关。下面是一些使用ExecuteScalar方法的简单代码,因为您只请求一行和一列。这里没有错误处理,因为代码只是向您展示了一种方法,用于执行您所要求的操作。如果您没有找到基于您的id的记录,并且需要处理,那么它将失败

using (SqlConnection con = new SqlConnection("YOUR CONNECTION STRING"))
{
    string query = "SELECT custNeed FROM Customers WHERE ID = 1000001;";
    using (SqlCommand cmd = new SqlCommand(query, con))
    {
        con.Open();
        //since you're only returning a single column you can forego the reader 
        //and just use ExecuteScalar
        checkBox1.Checked = (bool)cmd.ExecuteScalar();
    }
}
另外,由于您避开了多个复选框,因此我将添加一个方法,该方法使用读取器读取返回的行。同样,没有错误处理和使用reader.Read;读一行而不是在读卡器中循环

虽然我不完全了解Customers表的模式,但我将仅使用几个列名作为示例

using (SqlConnection con = new SqlConnection("YOUR CONNECTION STRING"))
{
    string query = "SELECT custNeed, isActive FROM Customers WHERE ID = 1000001;";
    using (SqlCommand cmd = new SqlCommand(query, con))
    {
        con.Open();
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            reader.Read(); //reads the first single returned row. 
            checkBox1.Checked = (bool)reader["custNeed"];
            checkBox2.Checked = (bool)reader["isActive"];
        }
    }

}

在cmd.ExecuteReader上添加断点时会看到什么;调试代码?我没有看到读卡器;在ExecuteReader之后,但这也会抛出一个错误。这正是我要寻找的。非常感谢你的帮助!