尝试
string checkuser = "select count(*) from [User] where [email protected]";
你的问题是ExecuteScalar
返回第一行,结果第一列的值,它不能转换为整数
,如果您有编号列,例如age
,做如下
string checkuser = "select age from [User] where [email protected]";
SQL语句广泛开放的SQL注入攻击,你最好使用参数
string sql= "select count(*) from [User] where UserName = @UserName";
using(SqlConnection con = new SqlConnection(conString))
using(SqlCommand cmd= new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddWithValue("@UserName", txtusername.Text);
int temp = Convert.ToInt32(cmd.ExecuteScalar().ToString());
if(temp == 1)
{}
}
这将是错误的转换为int。该命令将返回所有字段中的所有文本。然后,您将尝试将所有这些文本转换为失败的整数。你可以改变SQL查询来使用“select count(*)from ...”,然后这应该工作。 –
你打算在'temp'中储存什么? – StevieB