封装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 ...
随机推荐
- ltp执行过程总结
命令行:./runltp -b DEVICE -f timers -p -l result-log-timers.20180824 -o screen-log-timers.20180824 runl ...
- Golang内存分配内置函数之new函数
new函数用来分配内存,主要分配值类型,比如int.float32.struct等,返回的是指针 package main import ( "fmt" ) func main() ...
- Golang字符串函数认识(二)
package main import ( "fmt" "strings" ) func main(){ //返回字符在指定字符串中最后一次出现的位置 last ...
- 07:vue定义路由
1.1 定义路由 1.说明 1. 路由是单页面应用程序(SPA)的关键,Vue提供过来路由插件,使用这个路由就要安装这个插件 2. 安装: npm install vue-router 3. 依赖于v ...
- Android之xml解析
利用类下载器解析Xml文件要解析的xml文件<?xml version="1.0" encoding="utf-8"?><info> & ...
- python 之 函数的参数
函数的参数好几种类型:包括位置参数.默认参数.可变参数.关键字参数.命名关键字参数. 廖大神python学习笔记,大神网站:百度搜索“廖雪峰的官网” 1.位置参数:调用函数时根据函数定义的参数位置来传 ...
- myeclise中创建maven web程序
myeclipse自带了许多插件,因此使用频率很高,但是对maven框架下web程序似乎不是很好的支持,每次创建web程序总是会报一大堆的异常,因此特此记录一下如何在myeclipse下创建一个web ...
- 《AngularJS开发下一代Web应用》读书笔记与感想
该书一共130页打算四天读完,边读边记录. 1. 2.学习MogoDB 3. 4. 5. 创建标识符的一段简单伪码模板: var myModule = angular.module(...); myM ...
- Flask学习【第2篇】:Flask基础
知识点回顾 flask依赖wsgi,实现wsgi的模块:wsgiref,werkzeug,uwsgi 实例化Flask对象,里面是有参数的 app = Flask(__name__,template_ ...
- List of 3rd Party .NET UI & Reporting Components
https://www.codeproject.com/Reference/788434/List-of-rd-Party-NET-UI-Reporting-Components Introducti ...