封装sqlhelper【一】
控件信息展示:
//定义调用数据库类文件 namespace SqlHelper
{
public class TblClass
{
public int classId { get; set; }
public string class1 { get; set; }
public string classname { get; set; }
}
} //主文件 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; namespace SqlHelper
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//定义一个连接字符串
//readonly修饰的变量,只能在初始化的时候赋值,以及在构造函数中赋值
//其他地方只能读取不能设置值
private static readonly string constr =@"database=ItcastCater;server=LAPTOP-2FGC7ARC\Wangjin;user=sa;pwd=sa";
//1. 执行增(insert)、删(delect)、改(update)的方法
//ExecteNonQuery()
public static int ExecteNonQuery(string sql, params SqlParameter[] pms)
{
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand comm = new SqlCommand(sql, conn))
{
if (pms != null)
{
comm.Parameters.AddRange(pms);
}
conn.Open();
return comm.ExecuteNonQuery();
}
}
}
//返回单个值的方法封装 ExecuteScalar
public static object ExecuteScalar(string sql, params SqlParameter[] pms)
{
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand comm = new SqlCommand(sql, conn))
{
if (pms != null)
{
comm.Parameters.AddRange(pms);
}
conn.Open();
return comm.ExecuteScalar();
}
}
}
//返回SqlDataReader类型的多行多列数据 因为reader使用的时候必须保持连接池打开,所以方法和以上不一样
public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pms)
{
SqlConnection conn = new SqlConnection(constr); using (SqlCommand comm = new SqlCommand(sql, conn))
{
if (pms != null)
{
comm.Parameters.AddRange(pms);
}
try
{
conn.Open();
//System.Data.CommandBehavior.CloseConnection表示使用完毕以后在关闭reader的同时
//内部会将关联的connection对象页关闭掉
return comm.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
catch
{
//关闭连接
conn.Close();
conn.Dispose();
//向上抛异常,表示需要获取的数据出现异常,并且提示出错信息
throw;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
string sql = "select count(*) from classtable where classId=@uid and class=@pwd";
SqlParameter[] pms = new SqlParameter[]{
new SqlParameter("@uid",SqlDbType.Int){Value=textBox1.Text.Trim()},
new SqlParameter("@pwd",SqlDbType.VarChar,){Value=textBox2.Text}
};
int r=(int) SqlHelper.Form1.ExecuteScalar(sql, pms);
if (r > )
{
MessageBox.Show("登陆成功");
}
else
{
MessageBox.Show("登陆失败");
}
}
//使用ExecuteReader读取数据
private void button3_Click(object sender, EventArgs e)
{
List<TblClass> list=new List<TblClass>();
string sql = "select * from classtable";
using (SqlDataReader reader = SqlHelper.Form1.ExecuteReader(sql))
{
if (reader.HasRows)
{
while (reader.Read())
{
TblClass model = new TblClass();
model.classId = reader.GetInt32();
model.class1 = reader.GetString();
model.classname = reader.IsDBNull() ? null : reader.GetString();
list.Add(model);
}
}
MessageBox.Show(list.Count.ToString());
}
}
}
}
封装sqlhelper【一】的更多相关文章
- 数据操作的封装--sqlhelper
为了提高软件的灵活性和可维护性,软件的代码须要科学的管理.我们引入了架构这个词.设计模式提醒我们,软件中反复性的代码须要封装起来. 近期在做收费系统时.须要和数据库进行频繁的联系.既然是反复的使用,就 ...
- 封装SqlHelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...
- C# 封装SqlHelper
在项目配置文件中添加数据库连接字符串 <connectionStrings> <add connectionString="Data Source=主机;Initial C ...
- 封装sqlhelper类
using System;using System.Collections.Generic;using System.Data;using System.Data.Common;using Syste ...
- ADO.Net和SqlHelper封装
1.什么是ADO.Net 简单来讲,ADO.NET是用于和数据源打交道的.Net结束,是一组向.NET程序员公开数据访问服务的类 2.ADO.NET的组成部分和对象模型 (1)ADO.NET的两个 ...
- ado.net的简单数据库操作(二)之封装SqlHelperl类
今天我书接上回,接着昨天的ado.net的数据库操作的相关知识来讲哈! 从上篇文章给出的实例来看,你一定会发现,操作数据库其实还挺麻烦的,就连一个最简单的数据库操作语句都要包括 定义数据库连接字符串. ...
- 【整理】待毕业.Net码农就业求职储备
声明:本文题目来源于互联网,仅供即将从学校毕业的.Net码农(当然,我本人也是菜逼一个)学习之用.当然,学习了这些题目不一定会拿到offer,但是针对就业求职做些针对性的准备也是不错的.此外,除了技术 ...
- C#向Sql数据库插入控制
string name = textBox1.Text; int age = Convert.ToInt32(textBox2.Text.Trim()); ? null : (int?)Convert ...
- .net学习总结
.NET 学前入门 了解.Net能做什么 了解.NET,C#语言及其特点(分清.NET和C#的关系),对.Net学习有系统全面的认识. C#基础 变量,赋值运算符.数据类型转换等. 选择结构控制(if ...
随机推荐
- HFA and outhandler differentiate or not
better trouble shooting auto-open accidently? HFA not stable? check code modification ASAP!!!
- django后台管理--添加自定义action
管理员动作 简单来说,Django管理员的基本工作流程是“选择一个对象,然后进行更改”.这对大多数用例都很有效. 然而当你一次性要对多个对象做相同的改变,这个流程是非常的单调乏味的. 在这些情况下,D ...
- MySQL5.7.25解压版安装详细教程
MySQL5.7.25解压版安装详细教程 安装步骤: 1.首先,你要下载MySQL解压版,下载地址:https://www.mysql.com/downloads/,图解: 2.解压安装包,根据自己的 ...
- linux 文件 md5校验
为解决官方发布的软件包被别人更改或者软件在传输过程中出现传输错误等问题,软件官方在提供软件包的同时,还提供一个保存MD5校验码的文件. Linux/unix中可以使用命令 # md5sum 文件名 方 ...
- Vue父子组件生命周期
转载自:https://blog.csdn.net/a8725585/article/details/79092505 vue父子组件钩子函数触发顺序 beforeMount后mounted前构造子组 ...
- Bootstrap3基础 form-horizontal 表单元素横向布局 简单示例
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- Python3基础 list append 向尾部添加一个元素
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 日志统计 尺取法【蓝桥杯2018 C/C++ B组】
标题:日志统计 小明维护着一个程序员论坛.现在他收集了一份"点赞"日志,日志共有N行.其中每一行的格式是: ts id 表示在ts时刻编号id的帖子收到一个"赞" ...
- 笔记本电脑切换到无线热点无法联网问题&Spring Cloud相关工程启动报错问题
通过禁用本地网络,和禁用另一个无线网络,以及禁用后重开,修改密码,重连的方式均失败后, 使用IE浏览器浏览提示失败,点击诊断,诊断出DNS服务器无响应异常. 突然想到通过ipconfig查看ip,网关 ...
- hihoCoder week6 01背包
01背包 题目链接 https://hihocoder.com/contest/hiho6/problem/1 #include <bits/stdc++.h> using namespa ...