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

相关文章

Go语言使用select监听多个channel的示例详解

《Go语言使用select监听多个channel的示例详解》本文将聚焦Go并发中的一个强力工具,select,这篇文章将通过实际案例学习如何优雅地监听多个Channel,实现多任务处理、超时控制和非阻... 目录一、前言:为什么要使用select二、实战目标三、案例代码:监听两个任务结果和超时四、运行示例五

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

SQL Server 中的 WITH (NOLOCK) 示例详解

《SQLServer中的WITH(NOLOCK)示例详解》SQLServer中的WITH(NOLOCK)是一种表提示,等同于READUNCOMMITTED隔离级别,允许查询在不获取共享锁的情... 目录SQL Server 中的 WITH (NOLOCK) 详解一、WITH (NOLOCK) 的本质二、工作

MySQL CTE (Common Table Expressions)示例全解析

《MySQLCTE(CommonTableExpressions)示例全解析》MySQL8.0引入CTE,支持递归查询,可创建临时命名结果集,提升复杂查询的可读性与维护性,适用于层次结构数据处... 目录基本语法CTE 主要特点非递归 CTE简单 CTE 示例多 CTE 示例递归 CTE基本递归 CTE 结

Spring AI使用tool Calling和MCP的示例详解

《SpringAI使用toolCalling和MCP的示例详解》SpringAI1.0.0.M6引入ToolCalling与MCP协议,提升AI与工具交互的扩展性与标准化,支持信息检索、行动执行等... 目录深入探索 Spring AI聊天接口示例Function CallingMCPSTDIOSSE结束语

go动态限制并发数量的实现示例

《go动态限制并发数量的实现示例》本文主要介绍了Go并发控制方法,通过带缓冲通道和第三方库实现并发数量限制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录带有缓冲大小的通道使用第三方库其他控制并发的方法因为go从语言层面支持并发,所以面试百分百会问到

PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例

《PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例》词嵌入解决NLP维度灾难,捕捉语义关系,PyTorch的nn.Embedding模块提供灵活实现,支持参数配置、预训练及变长... 目录一、词嵌入(Word Embedding)简介为什么需要词嵌入?二、PyTorch中的nn.Em

Python Web框架Flask、Streamlit、FastAPI示例详解

《PythonWeb框架Flask、Streamlit、FastAPI示例详解》本文对比分析了Flask、Streamlit和FastAPI三大PythonWeb框架:Flask轻量灵活适合传统应用... 目录概述Flask详解Flask简介安装和基础配置核心概念路由和视图模板系统数据库集成实际示例Stre

Spring Bean初始化及@PostConstruc执行顺序示例详解

《SpringBean初始化及@PostConstruc执行顺序示例详解》本文给大家介绍SpringBean初始化及@PostConstruc执行顺序,本文通过实例代码给大家介绍的非常详细,对大家的... 目录1. Bean初始化执行顺序2. 成员变量初始化顺序2.1 普通Java类(非Spring环境)(

Java Spring的依赖注入理解及@Autowired用法示例详解

《JavaSpring的依赖注入理解及@Autowired用法示例详解》文章介绍了Spring依赖注入(DI)的概念、三种实现方式(构造器、Setter、字段注入),区分了@Autowired(注入... 目录一、什么是依赖注入(DI)?1. 定义2. 举个例子二、依赖注入的几种方式1. 构造器注入(Con