在C#應用程序中,我想在我的儀表板表單中設置/顯示用戶名(登錄),那么我該怎么做。提前致謝。以下是我的代碼。con.dataGet("Select * from [User] Where UserName = '"+txtUserName.Text+"' AND Password = '"+txtPassword.Text+"'"); DataTable dt = new DataTable(); con.sda.Fill(dt); if (dt.Rows.Count > 0) { this.Hide(); frmMain frm = new frmMain(); frm.Show(); } else { MessageBox.Show("Invalid Username And Password..!", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }請幫幫我。問候, 達拉姆拉吉
1 回答

阿晨1998
TA貢獻2037條經驗 獲得超6個贊
首先,也是最重要的一點,永遠不要使用串聯的SQL查詢,因為您容易受到SQL注入的攻擊。使用參數將值傳遞到 SQL 命令中。如何使用它們的示例:SqlCommand Parameters。
其次,要獲得所需的數據,您只需使用用數據庫中的信息填充的變量即可。dt
if (dt.Rows.Count > 0)
{
//getting the value from the DataTable
var userName = dt.Rows[0]["UserName_Column"];
//at this point you have the user name
//also a good idea is to store the user id, if available
this.Hide();
frmMain frm = new frmMain();
frm.Show();
}
- 1 回答
- 0 關注
- 128 瀏覽
添加回答
舉報
0/150
提交
取消