DBHelper的一个示例

2024-02-29 15:32
文章标签 示例 dbhelper

本文主要是介绍DBHelper的一个示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

DBHelper的一个示例

2011-11-29 11:35 by swarb, ... 阅读, ... 评论, 收藏, 编辑
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Data;
5 using System.Data.SqlClient;
6 using System.Configuration;
7
8
9 namespace PracticeMyBookShopDAL
10 {
11 public static class DBHelper
12 {
13
14 private static SqlConnection connection;
15 public static SqlConnection Connection
16 {
17 get
18 {
19 string connectionString = " Data Source=ADMIN-PC;Initial Catalog=MyBookShop;Integrated Security=True ";
20 if (connection == null)
21 {
22 connection = new SqlConnection(connectionString);
23 connection.Open();
24 }
25 else if (connection.State == System.Data.ConnectionState.Closed)
26 {
27 connection.Open();
28 }
29 else if (connection.State == System.Data.ConnectionState.Broken)
30 {
31 connection.Close();
32 connection.Open();
33 }
34 return connection;
35 }
36 }
37
38 public static int ExecuteCommand( string safeSql)
39 {
40 SqlCommand cmd = new SqlCommand(safeSql, Connection);
41 int result = cmd.ExecuteNonQuery();
42 return result;
43 }
44
45 public static int ExecuteCommand( string sql, SqlParameter[] values)
46 {
47 SqlCommand cmd = new SqlCommand(sql, Connection);
48 cmd.Parameters.AddRange(values);
49 return cmd.ExecuteNonQuery();
50 }
51
52 public static int ExecuteCommand( string sql, SqlParameter value)
53 {
54 SqlCommand cmd = new SqlCommand(sql, Connection);
55 cmd.Parameters.Add(value);
56 int result = cmd.ExecuteNonQuery();
57 return result;
58 }
59
60
61 public static int ExecuteScalar( string safeSql)
62 {
63 SqlCommand cmd = new SqlCommand(safeSql, Connection);
64 int result = ( int)cmd.ExecuteScalar();
65 return result;
66 }
67
68 public static int ExecuteScalar( string sql, SqlParameter[] values)
69 {
70 SqlCommand cmd = new SqlCommand(sql, Connection);
71 cmd.Parameters.AddRange(values);
72 int result = ( int)cmd.ExecuteScalar();
73 return result;
74 }
75
76 public static int ExecuteScalar( string sql, SqlParameter value)
77 {
78 SqlCommand cmd = new SqlCommand(sql, Connection);
79 cmd.Parameters.Add(value);
80 int result = ( int)cmd.ExecuteScalar();
81 return result;
82 }
83
84 public static SqlDataReader ExecuteReader( string safeSql)
85 {
86 SqlCommand cmd = new SqlCommand(safeSql, Connection);
87 SqlDataReader reader = cmd.ExecuteReader();
88 return reader;
89 }
90
91 public static SqlDataReader ExecuteReader( string sql, SqlParameter value)
92 {
93 SqlCommand cmd = new SqlCommand(sql, Connection);
94 cmd.Parameters.Add(value);
95 SqlDataReader reader = cmd.ExecuteReader();
96 return reader;
97 }
98
99 public static SqlDataReader ExecuteReader( string sql, SqlParameter[] values)
100 {
101 SqlCommand cmd = new SqlCommand(sql, Connection);
102 cmd.Parameters.AddRange(values);
103 SqlDataReader reader = cmd.ExecuteReader();
104 return reader;
105 }
106 public static DataTable GetDataSet( string safeSql)
107 {
108 DataSet ds = new DataSet();
109 SqlCommand cmd = new SqlCommand(safeSql, Connection);
110 SqlDataAdapter da = new SqlDataAdapter(cmd);
111 da.Fill(ds);
112 return ds.Tables[ 0];
113 }
114 public static SqlDataReader GetReader( string safeSql)
115 {
116 SqlCommand cmd = new SqlCommand(safeSql, Connection);
117 SqlDataReader reader = cmd.ExecuteReader();
118 return reader;
119 }
120
121 public static SqlDataReader GetReader( string sql, params SqlParameter[] values)
122 {
123 SqlCommand cmd = new SqlCommand(sql, Connection);
124 cmd.Parameters.AddRange(values);
125 SqlDataReader reader = cmd.ExecuteReader();
126 return reader;
127 }
128
129
130 }
131 }

这篇关于DBHelper的一个示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/759271

相关文章

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

mysql中insert into的基本用法和一些示例

《mysql中insertinto的基本用法和一些示例》INSERTINTO用于向MySQL表插入新行,支持单行/多行及部分列插入,下面给大家介绍mysql中insertinto的基本用法和一些示例... 目录基本语法插入单行数据插入多行数据插入部分列的数据插入默认值注意事项在mysql中,INSERT I

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul