封装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 ...
随机推荐
- 一名3年工作经验的java程序员应该具备的职业技能
一名3年工作经验的Java程序员应该具备的技能,这可能是Java程序员们比较关心的内容.我这里要说明一下,以下列举的内容不是都要会的东西—-但是如果你掌握得越多,最终能得到的评价.拿到的薪水势必也越高 ...
- sql server 游标的简单用法
sql server游标: --定义游标 declare cursor1 cursor for select ID,Name from A --打开游标 open cursor1 declare @i ...
- STM32开发 -- 4G模块开发详解(转)
STM32开发 -- 4G模块开发详解(1) STM32开发 -- 4G模块开发详解(2) STM32开发 -- 4G模块开发详解(3) STM32开发 -- 4G模块开发详解(4)
- nginx+php+memcache实现hash一致性memcache 集群
我们工作中可能会遇到key-value数据库,如果我们面对的不止一台memcache服务器,而是很多台.那么现在就回出现一个问题: 当我们访问nginx服务器的时候,我们会判断memcache中是否有 ...
- Angular 请求数据
Angular 请求数据 get post 以及 jsonp 请求数据 引入 HttpModule .JsonpModule 普通的 HTTP 调用并不需要用到 JsonpModule,不过稍后我们就 ...
- 第三周作业HAproxy文件操作
#coding:utf-8 #Author:Mr Zhi """ HAproxy配置文件操作: 1. 根据用户输入输出对应的backend下的server信息 2. 可添 ...
- Navicat Premium 12.0.18安装与激活(转)
转载:https://www.jianshu.com/p/42a33b0dda9c 一.Navicat Premium 12下载 Navicat Premium 12是一套数据库开发管理工具,支持连接 ...
- Linux - PWM的驱动编写【转】
本文转载自:https://blog.csdn.net/u012264124/article/details/77482853 比如要用到pwm1,那么首先要保证这个pwm1并没有被别的驱动程序占用. ...
- git如何将一个分支合并到另一个分支?
答: git merge --no-edit <another branch>
- outlook使用笔记
使用电子邮件客户端(pc端)软件, 确实是不得已. 出于某些考试/了解的目的? 现在使用 在线/网页端电子邮件 确实要好得多, 方便得多了. outlook和其他软件都是 设置的 "帐户 a ...