註冊按鈕事件:sql
private void btnRegister_Click(object sender, EventArgs e)
{
string username = txtUserName.Text;
string userpwd = txtUserPwd.Text;
string tel = txtTel.Text;
string email = txtEmail.Text;
string name = txtName.Text;
int dept = Convert.ToInt32(txtDept.Text);
if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(userpwd))
{
MessageBox.Show("用戶名和密碼不能爲空!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
string sql = "INSERT INTO [UserInfo]([username],[userpwd],[name],[deptId],[tel],[email],[state],[registerTime],[lastLoginTime],[remark])" +
"VALUES(@username,@userpwd,@name,@deptId,@tel,@email,@state,GETDATE(),GETDATE(),'null')";
SqlParameter[] param =
{
new SqlParameter("@username",SqlDbType.VarChar),
new SqlParameter("@userpwd",SqlDbType.VarChar),
new SqlParameter("@name",SqlDbType.VarChar),
new SqlParameter("@deptId",SqlDbType.Int),
new SqlParameter("@tel",SqlDbType.VarChar),
new SqlParameter("@email",SqlDbType.VarChar),
new SqlParameter("@state",SqlDbType.VarChar)
};
param[0].Value = username;
param[1].Value = userpwd;
param[2].Value = name;
param[3].Value = dept;
param[4].Value = tel;
param[5].Value = email;
param[6].Value = "";
int count = DataManager.Set(sql, param);
if (count > 0)
{
DialogResult dr = MessageBox.Show("註冊成功,是否登陸?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr == DialogResult.OK)
{
FrmLogin login = new FrmLogin();
login.Show();
this.Hide();
}
else
{
this.Show();
}
}
else
{
MessageBox.Show("註冊失敗!","提示",MessageBoxButtons.AbortRetryIgnore,MessageBoxIcon.Hand);
}
}數據庫
調用底層的方法:app
DataManager類:ide
public static int Set(string sql, SqlParameter[] pars)
{
return new DataService().Set(sql, pars);
}this
DataService類:orm
public int Set(string sql, SqlParameter[] pars)
{
Init(sql, pars, SysControl.ConnectionString);
return Set();
}server
private int Set()
{
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
return i;
}事件
SysControl類:rem
/// <summary>
/// 數據庫鏈接字符串
/// </summary>
public static string ConnectionString = ConfigurationManager.AppSettings["connectionString"];字符串
在app配置文件裏面添加連接:
<appSettings> <add key ="connectionString" value="server=.;user id=sa; password=123456; database=db;"/> </appSettings>