嘗試
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