sql 错误 "Input string was not in a correct format"
Error "Input string was not in a correct format" with sql
我无法从我的 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
的DataType对于这件事有点(在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");
}
我无法从我的 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
的DataType对于这件事有点(在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");
}