我在从sql数据库中获取int值时遇到麻烦:
if(Convert.ToDouble(dbh.getInfo("firstTime", username))==1)
而且我也尝试过:
if((int)dbh.getInfo("firstTime", username)==1)
这是getInfo函数:
public object getInfo(string infoReq, string username)
{
string query = "select (@infoReq) from AccountDB where username like @username";
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@infoReq", infoReq);
cmd.Parameters.AddWithValue("@username", username);
con.Open();
return cmd.ExecuteScalar();
}
}
catch (Exception e){
}
return MessageBox.Show("Please check your fields");
}
[dbh
是DBHandler
类型,当然是该函数的来源
在sql数据库中,此问题的@infoReq
的数据类型为一点(在sql中为[firstTime] BIT NOT NULL
)
我的代码有什么问题?
尝试一下:
public object getInfo(string infoReq, string username)
{
string query = "select @infoReq from AccountDB where username like '%@username%'";
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@infoReq", infoReq);
cmd.Parameters.AddWithValue("@username", username);
con.Open();
return cmd.ExecuteScalar();
}
}
catch (Exception e){
}
return MessageBox.Show("Please check your fields");
}