发布网友 发布时间:2022-04-30 07:08
共3个回答
懂视网 时间:2022-04-30 11:29
建立解决方案:.首先创建windows窗体应用程序,然后拖出登录界面
2. 写配置文件App.config:
????<configuration>
????????<connectionStrings>
????????<add name="mssqlserver" connectionString="Data Source=.;Initial Catalog=test;User ID=sa;Password=1q2w3e4R"/>
????????</connectionStrings>
</configuration>
其中的name可以随意去写,没有任何限制。连接字符串中的【Data Source=.】为服务器名称,【Initial Catalog-test】为表名,后面的为用户名和密码。
3. 写SqlHelper代码:
????3.1 读取配置文件:
????????·首先要添加引用:System.Configuration
????????·然后读取连接字符串:
????????????private static readonly string connStr = ConfigurationManager.ConnectionStrings["mssqlserver"].ConnectionString;
????3.2 封装常用的方法
????????·执行insert/delete/update的方法
????????????public static int ExecuteNonQuery(string sql, CommandType cmdType, params SqlParameter[] pms)
????????{
//创建连接对象
????using (SqlConnection con = new SqlConnection(connStr))
????{
//创建命令对象
???? using (SqlCommand cmd = new SqlCommand(sql, con))
????{
//设置当前执行的是存储过程还是带参数的Sql语句
cmd.CommandType = cmdType;
//判断是否传递参数 注意:如果传递的不是数组类型,那么不去判空也不会报错,但是如果是数组类型,那么就会报错。所以为了保险起见,应该进行判空。
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
con.Open();
return cmd.ExecuteNonQuery();
????}
????}
????????}
????????·执行返回单个值的方法
????????????public static object ExecuteScalar(string sql, CommandType cmdType, params SqlParameter[] pms)
????????{
????using (SqlConnection con = new SqlConnection(connStr))
????{
????using (SqlCommand cmd = new SqlCommand(sql, con))
????{
cmd.CommandType = cmdType;
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
con.Open();
return cmd.ExecuteScalar();
????}
????}
????????}
????????·返回SqlDataReader的方法
????????????public static SqlDataReader ExecuteReader(string sql, CommandType cmdType, params SqlParameter[] pms)
????????{
????SqlConnection con=new SqlConnection(connStr);
????using (SqlCommand cmd = new SqlCommand(sql, con))
????{
????cmd.CommandType = cmdType;
????if (pms != null)
????{
cmd.Parameters.AddRange(pms);
????}
????try
????{
con.Open();
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
????}
????catch
????{
con.Close();
con.Dispose();
throw;
????}
????}
????????}
????????·执行返回DataTable方法
????????????public static DataTable ExecuteDataTable(string sql, CommandType cmdType, params SqlParameter[] pms)
????????{
????DataTable dt=new DataTable();
????using (SqlDataAdapter adapter = new SqlDataAdapter(sql, connStr))
????{
adapter.SelectCommand.CommandType = cmdType;
if (pms != null)
????{
adapter.SelectCommand.Parameters.AddRange(pms);
????}
adapter.Fill(dt);
return dt;
????}
????????}
4. 执行登录操作:
????????private void button1_Click(object sender, EventArgs e)
????{
string sql = "select COUNT(*) from users where loginId=@uid and loginPwd=@pwd";
SqlParameter[] pms=new SqlParameter[]
{
new SqlParameter("@uid",textBox1.Text.Trim()),
new SqlParameter("@pwd",textBox2.Text)
};
int n=(int) SqlHelper.ExecuteScalar(sql, CommandType.Text, pms);
if (n > 0)
{
MessageBox.Show("成功!");
}
else
{
MessageBox.Show("失败!");
}
????}
通过SqlHelper实现登录
标签:
热心网友 时间:2022-04-30 08:37
protected voidbtnLogin_Click(object sender, EventArgs e)//登陆响应事件热心网友 时间:2022-04-30 09:55
//单击事件