
請大佬解答,謝謝
uj5u.com熱心網友回復:
private void btnsubmit_Click(object sender, EventArgs e){
try
{
if (txtusername.Text == "")
{
MessageBox.Show("用戶名不能為空");
}
else
{
if (txtpassword.Text == "")
{
MessageBox.Show("密碼不能為空");
}
else
{
string username = txtusername.Text;//gain username
string password = txtpassword.Text;//gain password
string conn = @"server=LAPTOP-A4EP75C2;integrated security=true;";
//connnection string
SqlConnection connection = new SqlConnection(conn);
//create conn
connection.Open();
//open conn
string sql = string.Format("select count(*) from SQLstudent where sname='{0}' and password='{1}'", username, password);
//find data you want from database
SqlCommand command = new SqlCommand(sql, connection);
//keep the data a moment
int i = Convert.ToInt32(command.ExecuteScalar());
//執行后回傳記錄行數
if (i > 0)
//如果大于1,說明記錄存在,登錄成功
{
MessageBox.Show("登錄成功,歡迎!");
this.Hide();
}
else
{
MessageBox.Show("用戶名或者密碼錯誤!");
}
connection.Close();
}
}
}
catch(Exception ex)
{
MessageBox.Show ("例外錯誤" + ex);
}
}
uj5u.com熱心網友回復:
很明確了,SQLstudent 表沒有你是不是連錯資料庫了。
uj5u.com熱心網友回復:
string conn = ... 這一行里沒有指定使用哪個庫uj5u.com熱心網友回復:
select count(*) from SQLstudent where sname='{0}' and password='{1}' 這個表名無效,SQLstudent ,去數據庫看看轉載請註明出處,本文鏈接:https://www.uj5u.com/net/236924.html
標籤:C#
上一篇:EF contain()問題
下一篇:一個演算法問題,庫存篩選
