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>