1.引用
using System.Data.SqlClient;//引入数据库
2.连接字符串
string connString148 = @"server=127.0.0.1;uid=sa;pwd=cc3279706705;database=xs";//连接字符串
3.登录按钮里面的代码
if (txtID.Text == "" || txtPassWord.Text == "") { MessageBox.Show("请输入完整用户名和密码!", "登录程序"); txtID.Focus();//该控件获得焦点 } else { try { string sql19148 = string.Format(@"select username, password from users where username like '{0}' and password like '{1}' ", txtID.Text.Trim(), txtPassWord.Text.Trim()); using (SqlConnection cn148 = new SqlConnection(connString148)) { cn148.Open(); SqlCommand cm148 = new SqlCommand(sql19148, cn148); int i = Convert.ToInt32(cm148.ExecuteScalar()); if (i >= 1) { //new frmMain().Show(); this.Close(); } else { MessageBox.Show("账号或密码错误"); txtID.Text = ""; txtPassWord.Text = ""; txtID.Focus(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); }