public void userValidate(string sqlStr){DBConnection.Con.Open();//數(shù)據(jù)庫的連接類打開try{SqlCommand sqlselect = new SqlCommand(sqlStr, DBConnection.Con);int i = (int)sqlselect.ExecuteScalar();if (i == 1){this.Hide();DBConnection.Con.Close();string sqlcmd = string.Format("select * from Roles where rolename='{0}'", genre());FrmMian frmmian = new FrmMian(sqlcmd);frmmian.Show();}else{MessageBox.Show("用戶不存在或密碼錯誤", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); txtPwd.Clear(); txtUserName.Clear(); txtUserName.Focus();}}catch (SqlException ex){MessageBox.Show(ex.Message);}finally{DBConnection.Con.Close();}}
2 回答
慕碼人8056858
TA貢獻1803條經(jīng)驗 獲得超6個贊
sqlselect是SqlCommand類型對象,用于執(zhí)行SQL語句。
ExecuteScalar();這是SqlCommand的方法。用于執(zhí)行返回當個結果值的SQL語句。
比如:select sname from users where id=1這樣的話只返回id為1的人的姓名。只可以返回單個結果值,至于是返回數(shù)值,還是字符串,均可。只是返回的結果需要數(shù)據(jù)類型轉換。
看你的代碼應該是返回一個數(shù)值,if(i==1)就是判斷你返回的結果,一般來說,如果返回的結果不大于0應該就是查詢失敗了或者數(shù)據(jù)不存在。否則就是查詢成功,或數(shù)據(jù)存在。
希望對你有所幫助!
達令說
TA貢獻1821條經(jīng)驗 獲得超6個贊
int i = (int)sqlselect.ExecuteScalar();中i是什么意思 if(i==1)也不懂
這個i就是函數(shù)sqlselect.ExecuteScalar()的返回值啊;
if(i==1)就是判斷函數(shù)的返回值是不是等于1;
- 2 回答
- 0 關注
- 1336 瀏覽
添加回答
舉報
0/150
提交
取消
