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

相关文章

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(

Python中Markdown库的使用示例详解

《Python中Markdown库的使用示例详解》Markdown库是一个用于处理Markdown文本的Python工具,这篇文章主要为大家详细介绍了Markdown库的具体使用,感兴趣的... 目录一、背景二、什么是 Markdown 库三、如何安装这个库四、库函数使用方法1. markdown.mark

MySQL数据库函数之JSON_EXTRACT示例代码

《MySQL数据库函数之JSON_EXTRACT示例代码》:本文主要介绍MySQL数据库函数之JSON_EXTRACT的相关资料,JSON_EXTRACT()函数用于从JSON文档中提取值,支持对... 目录前言基本语法路径表达式示例示例 1: 提取简单值示例 2: 提取嵌套值示例 3: 提取数组中的值注意

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

css渐变色背景|<gradient示例详解

《css渐变色背景|<gradient示例详解》CSS渐变是一种从一种颜色平滑过渡到另一种颜色的效果,可以作为元素的背景,它包括线性渐变、径向渐变和锥形渐变,本文介绍css渐变色背景|<gradien... 使用渐变色作为背景可以直接将渐China编程变色用作元素的背景,可以看做是一种特殊的背景图片。(是作为背

JAVA调用Deepseek的api完成基本对话简单代码示例

《JAVA调用Deepseek的api完成基本对话简单代码示例》:本文主要介绍JAVA调用Deepseek的api完成基本对话的相关资料,文中详细讲解了如何获取DeepSeekAPI密钥、添加H... 获取API密钥首先,从DeepSeek平台获取API密钥,用于身份验证。添加HTTP客户端依赖使用Jav