使用C#从SQL Server检索radioButton值

时间:2013-11-26 17:20:47

标签: c# sql sql-server

我正在尝试从SQL Server Compact中检索数据库中的radioButton值 但我的代码不起作用,并在(unable to cast object of type 'System.Data.SqlServerCe Command' to type 'System.IConvertible)行显示错误消息(genderval = Convert.ToBoolean(gendercmnd);) .. 怎么了? 感谢

if (con.State != ConnectionState.Open)
    con.Open();

Boolean genderval = false;
SqlCeCommand gendercmnd = new SqlCeCommand(@"SELECT Gender FROM Std_info WHERE Std_id='" + TextBox1.Text + "'", con);
genderval = Convert.ToBoolean(gendercmnd);
if (genderval == true)
    Convert.ToBoolean(radioButton1.Checked);
else
    Convert.ToBoolean(radioButton2.Checked);  
con.Close();

5 个答案:

答案 0 :(得分:2)

尝试执行命令

genderval = (Boolean)gendercmnd.ExecuteScalar();

另请注意,您的查询应参数化。这是一个完整的示例(使用SqlConnection,但如果您将Sql类替换为您的提供程序类,它应该适合您)

bool genderval;

using (SqlConnection conn = new SqlConnection("youeConnStr"))
{
    conn.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT Gender FROM Std_info WHERE Id = @id", conn))
    {
        int id = Convert.ToInt32(TextBox1.Text);
        cmd.Parameters.Add(new SqlParameter("@id", id));
        genderval = (bool)cmd.ExecuteScalar();
    }
}

答案 1 :(得分:0)

看起来好像你还没有真正查询过数据库,但是正在尝试转换实际的命令而不是收集的结果。

我不确定SQL Server Compact是否同样适用,但您需要 ExecuteNonScalar 执行命令本身,检索结果然后运行比较在那。

答案 2 :(得分:0)

您是否正在对数据库执行命令并获得结果?因为该代码看起来不像是在做它。我认为你需要在sqlcecommand上调用ExecuteScalar()。

答案 3 :(得分:0)

您要做的是从SQL服务器获取结果,并将其转换为布尔值。

您实际在做的是尝试将SQL命令转换为布尔值。

您需要做的是实际打开与SQL服务器的连接并执行查询。但它比那复杂得多......

if (con.State != ConnectionState.Open)
   con.Open();

Boolean genderval = false;
SqlCeCommand gendercmnd = new SqlCeCommand(@"SELECT Gender FROM Std_info WHERE Std_id='" + TextBox1.Text + "'", con);

gendercmnd.Connection.Open(); // Open the connection using the gendercmd string.
sqlReader = gendercmnd.ExecuteReader(); // Execute the read, which returns an SqlCEDataReader object that we can use to access the data. 


genderval = Convert.ToBoolean(gendercmndResult);
            if (genderval == true)
                Convert.ToBoolean(radioButton1.Checked);
            else
                Convert.ToBoolean(radioButton2.Checked);  
con.Close();

现在我们需要从读取器获取值,并将其转换为布尔值。

if (con.State != ConnectionState.Open)
   con.Open();

Boolean genderval = false;
String gendercmndResult = null;
SqlCeCommand gendercmnd = new SqlCeCommand(@"SELECT Gender FROM Std_info WHERE Std_id='" + TextBox1.Text + "'", con);

gendercmnd.Connection.Open(); // Open the connection using the gendercmd string.
sqlReader = gendercmnd.ExecuteReader(); returns an SqlCEDataReader object that we can use to access the data.

while (sqlReader.Read(){ //While the reader is reading...
    gendercmndResult = (sqlReader.GetString(0)); //Assign the returned value from the SQL server to a string called gendercmndResult
}


genderval = Convert.ToBoolean(gendercmndResult); //Now we can cast the string as a bool
            if (genderval == true)
                Convert.ToBoolean(radioButton1.Checked);
            else
                Convert.ToBoolean(radioButton2.Checked);  
con.Close();

答案 4 :(得分:0)

最终解决方案..!

//........ retrieving radioButton value ....... 

if (conn.State != ConnectionState.Open) 
      conn.Open(); 
try { 
       Boolean genderval = SqlCeCommand gendercmnd = new SqlCeCommand(@"SELECT Gender FROM Std_Info     WHERE Std_id='" + TextBox1.Text + "'", con); 

genderval = (Boolean)gendercmnd.ExecuteScalar(); 

if (genderval == true) 
    Convert.ToBoolean(radioButton1.Checked=true); 
else 
    Convert.ToBoolean(radioButton2.Checked = true); 
conn.Close(); 
     } 
catch (Exception ex)
     {
          MessageBox.Show(ex.Message);
     }

谢谢大家的帮助