private void btnLogin_Click(object sender, EventArgs e) { string username = txtUserName.Text; string userpwd = txtUserPwd.Text; string sql = "select * ...
private void btnLogin_Click(object sender, EventArgs e)
{
string username = txtUserName.Text;
string userpwd = txtUserPwd.Text;
string sql = "select * from UserInfo where username = @username and userpwd = @userpwd";
SqlParameter[] param =
{
new SqlParameter("@username",SqlDbType.VarChar),
new SqlParameter("@userpwd",SqlDbType.VarChar)
};
param[0].Value = username;
param[1].Value = userpwd;
DataSet ds = DataManager.Get(sql, param);
if (ds.Tables.Count > 0)
{
this.Hide();
FrmMain main = new FrmMain();
main.Show();
}
else
{
MessageBox.Show("登錄失敗!", "提示", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Hand);
}
}
DataManager類:
public static DataSet Get(string sql, SqlParameter[] pars)
{
return new DataService().Get(sql, pars);
}
DataService類:
public DataSet Get(string sql, SqlParameter[] pars)
{
Init(sql, pars, SysControl.ConnectionString);
return Get();
}
private DataSet Get()
{
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
try
{
da.Fill(ds);
}
catch (System.Exception)
{
//throw;
}
con.Close();
return ds;
}
SysControl類:
/// <summary>
/// 資料庫連接字元串
/// </summary>
public static string ConnectionString = ConfigurationManager.AppSettings["connectionString"];
在app配置文件裡面添加鏈接:
<appSettings>
<add key ="connectionString" value="server=.;user id=sa; password=123456; database=db;"/>
</appSettings>