using System.Data.SqlClient;using System.Configuration;namespace Logon{public partial class Form1 : Form{public Form1(){InitializeComponent();}ManagerForm mf = new ManagerForm();UserForm uf = new UserForm();bool testMyUser(){DBManager db = new DBManager();SqlParameter[] parames=new SqlParameter[3];parames[0] = new SqlParameter("@name",txtName.Text);parames[1] = new SqlParameter("@password",txtPassword.Text);parames[2] = new SqlParameter("@role",cmbRole.Text);DataTable dt = db.SelectMyUser("select * from MyUser where Name=@name and Password=@password and Role=@role",parames);if(dt.Rows.Count>0){return true;}else {return false;}}private void btnLog_Click(object sender, EventArgs e){if(cmbRole.Text=="Manager"){if(testMyUser()){mf.Show();}else {MessageBox.Show("用户名或密码错误!");}} }class DBManager{public static readonly string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;SqlConnection cn = new SqlConnection(connectionString);public DBManager(){cn.Open();运行时,此处显示cn实例失败}public int RunMyUser(string strSql, SqlParameter[] parames){SqlCommand cm = new SqlCommand();cm.Connection = cn;cm.CommandType = CommandType.Text;cm.CommandText = strSql;cm.Parameters.Clear();if (parames != null){foreach (SqlParameter parame in parames){cm.Parameters.Add(parame);}}int i = cm.ExecuteNonQuery();return i;}public DataTable SelectMyUser(string strSql, SqlParameter[] parames){SqlCommand cm = new SqlCommand();cm.Connection = cn;cm.CommandType = CommandType.Text;cm.CommandText = strSql;cm.Parameters.Clear();if (parames != null){foreach (SqlParameter parame in parames){cm.Parameters.Add(parame);}}SqlDataAdapter da = new SqlDataAdapter();da.SelectCommand = cm;DataSet ds = new DataSet();da.Fill(ds);DataTable dt = ds.Tables[0];return dt;}}}
- 2 回答
- 0 关注
- 74 浏览
添加回答
举报
0/150
提交
取消