[英]C# & SQL Server - insert into different tables (if/case statement)
[英]C# and SQL Server : conditional statement for tables
我有一個使用管理員和員工登錄的程序。 我在SQL Server中有兩個表,分別為Admin
和Employee
。
我只有一個登錄窗口。 我有兩種形式AdminForm
和EmpForm
。 輸入用戶名和密碼后,我要閱讀兩個表。
Admin
,則它將顯示AdminForm
Employee
,它將顯示EmpForm
我是SQL Server的新手。 反正有可能嗎? 到目前為止,這是我的代碼:
private void btnLogin_Click(object sender, EventArgs e)
{
using (var connect = sqlcon.getConnection())
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM administrator WHERE username = @username AND password = @password"))
{
cmd.Connection = connect;
connect.Open();
cmd.Parameters.Add("@cusername", SqlDbType.Char).Value = tbUsername.Text;
cmd.Parameters.Add("@cpassword", SqlDbType.Char).Value = tbPassword.Text;
using (SqlDataReader re = cmd.ExecuteReader())
{
if (re.Read())
{
}
else
{
}
}
}
}
}
模式錯誤。 將兩個表合並為一個Users
表,並在表中增加一列,以表明用戶屬於哪個角色。
您需要2個sql查詢,它們使用參數用戶名和密碼,如果插入的參數正確,則一個查詢返回admin的對象,另一個查詢返回對象employee,否則返回null。
嘗試使用這兩個查詢的結果初始化Admin和Employee的對象。 比測試
if(admin != null)
打開AdminForm。 else if(employee != null)
打開EmpForm。 else
-顯示消息錯誤的用戶名或密碼。
SonerGönül正確地嘗試使用帶有或不帶有鹽的哈希來不以明文形式存儲密碼。
您的查詢可以像這樣工作:
Select [EntityType] =1 , SurrogateKey = EmployeeKey, LastName, FirstName from dbo.Employee where (blah blah blah)
UNION ALL
Select [EntityType] = 2, SurrogateKey = AdminKey, LastName, FirstName from dbo.Admin where (blah blah blah)
然后,您將有一種方法來區分行...。如果可以退回兩行,則可以做出決定。 EmployeeKey和AdminKey(無論您的PK名稱是什么)都必須是相同的數據類型。 (以及LastName和FirstName,如果所有數據類型都匹配,則只能UNION / UNION ALL。
..............
話雖如此……..您已經“混合”了許多擔憂。 創建一個DataLayer,它返回某種具有必要值的POCO對象。 button_click和SqlConnection不應使用相同的代碼。
pubic class LoginResult()
{
public int EntityType {get;set;}
public int SurrogateKey {get;set;}
public string LastName{get;set;}
public string FirstName{get;set;}
}
public interface IAccountManager
{
LoginResult AttemptLogin (string userName, string password)
}
public class IAccountManager() : IAccountManager
{
public LoginResult AttemptLogin (string userName, string password)
{
// put your SqlConnection/SqlReader code here
// do not put any "logic" "if checks" etc....the concern here is to only create the object
}
}
分開關注點要好一些。 谷歌“ C#層”的更多討論。
正如其他人所說,這不是正確的用戶名/密碼方式。 但是,您的問題的答案將是使用聯合,例如:
using (SqlCommand cmd = new SqlCommand("SELECT 1 as IsAdmin, *
FROM administrator
WHERE username = @username AND password = @password
UNION ALL
SELECT 0 as IsAdmin, *
FROM employees
WHERE username = @username AND password = @password "))
我使用了管理員和員工的權限並將其編碼。 感謝您的所有想法! 如果有人需要它,請參考以下代碼。 不要忘記加密密碼。
private void btnLogin_Click(object sender, EventArgs e)
{
using (var connect = sqlcon.getConnection())
{
using (SqlCommand cmd = new SqlCommand("SELECT rights FROM employee WHERE username = @username AND password = @password"))
{
cmd.Connection = connect;
connect.Open();
cmd.Parameters.Add("@username", SqlDbType.Char).Value = tbUsername.Text;
cmd.Parameters.Add("@password", SqlDbType.Char).Value = tbPassword.Text;
//SqlDataReader re = cmd.ExecuteReader();
string aeRights = (string)cmd.ExecuteScalar();
if (aeRights == "1")
{
frmAdmin frmA = new frmAdmin();
frmA.Show();
this.Hide();
}
else if (aeRights == "2")
{
frmEmp frmE = new frmEmp();
frmE.Show();
this.Hide();
}
else if (string.IsNullOrEmpty(aeRights))
{
MessageBox.Show("Invalid username or password! Please try again", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.