110,538
社区成员
发帖
与我相关
我的任务
分享
class DBHelper
{
private static string strSql = string.Format(@"server=CHENJINGLONGPC\SQLEXPRESS;database=FamilyFinace;Integrated Security=True");
public static SqlConnection connect= new SqlConnection(strSql);
}
登录系统
try
{
DBHelper.connect.Open();
string sql = string.Format(@"select count(*) from FamilyUser where UserName='{0}' and UserPassword='{1}'", txtUserName.Text.Trim(), txtPassWord.Text);
SqlCommand conform = new SqlCommand(sql, DBHelper.connect);
int result = (int)conform.ExecuteScalar();
if (result > 0)
{
this.DialogResult = DialogResult.OK;
}
else
{
lblUPError.Visible = true;
}
// MessageBox.Show("success");
}
catch (SqlException ex)
{
MessageBox.Show("登录失败 原因如下" + ex.Message, "家庭理财管理系统");
}
finally
{
DBHelper.connect.Close();
}
private bool checkEmpty()
{
bool result = true;
if (txtUserName.Text.Trim() == string.Empty)
{
lblUserError.Visible = true;
result = false;
}
else
{
lblUserError.Visible = false;
}
if (txtPassWord.Text == string.Empty)
{
lblPassError.Visible = true;
result = false;
}
else
{
lblPassError.Visible = false;
}
if (txtCPass.Text == string.Empty)
{
lblCoformError.Visible = true;
result = false;
}
else if (txtPassWord.Text != txtCPass.Text)
{
lblPassError.Visible = false;
lblCoformError.Visible = false;
MessageBox.Show("密码不匹配请重新输入,请重新输入");
txtPassWord.Clear();
txtCPass.Clear();
result = false;
}
return result;
}
private void btnRegist_Click(object sender, EventArgs e)
{
// SqlCommand ins = null;
bool isEmpty = checkEmpty();
if (isEmpty == true)
{
try
{
DBHelper.connect.Open();
string sql = string.Format(@"insert into [FamilyUser] values('{0}','{1}')", txtUserName.Text.Trim(), txtPassWord.Text);
SqlCommand ins = new SqlCommand(sql, DBHelper.connect);
int count = ins.ExecuteNonQuery();
if (count == 1)
{
MessageBox.Show("你已经成功注册");
this.Close();
}
}
catch (SqlException ex)
{
MessageBox.Show("注册失败 原因如下" + ex.Message);
}
finally
{
DBHelper.connect.Close();
}
}