ADO.NET数据库连接模块

        下面是一个封装了的ADO.NET连接数据库的模块,可以执行几乎所有的存储过程,稍做修改便可执行所有的SQL Command的单语句命令:

  1 None.gif using  System;
  2 None.gif using  System.Configuration;
  3 None.gif using  System.Data;
  4 None.gif using  System.Data.SqlClient;
  5 None.gif
  6 None.gif namespace  DBModules
  7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  9InBlock.gif    /// 数据库访问辅助类,该类中都是静态的方法,以更方便的调用存储过程
 10ExpandedSubBlockEnd.gif    /// </summary>

 11InBlock.gif    public sealed class SqlHelper
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 13ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 14InBlock.gif        /// 这里用私有函数,防止实例化该类
 15ExpandedSubBlockEnd.gif        /// </summary>

 16InBlock.gif        private SqlHelper()
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 18InBlock.gif            
 19ExpandedSubBlockEnd.gif        }

 20ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 21InBlock.gif        /// 获取数据库连接字符串
 22ExpandedSubBlockEnd.gif        /// </summary>

 23InBlock.gif        public static string connectionString 
 24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 25ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gifreturn ConfigurationSettings.AppSettings["connectString"];}
 26ExpandedSubBlockEnd.gif        }

 27InBlock.gif        
 28ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 29InBlock.gif        /// Private routine allowed only by this base class, it automates the task
 30InBlock.gif        /// of building a SqlCommand object designed to obtain a return value from
 31InBlock.gif        /// the stored procedure.
 32InBlock.gif        /// </summary>
 33InBlock.gif        /// <param name="storedProcName">Name of the stored procedure in the DB, eg. sp_DoTask</param>
 34InBlock.gif        /// <param name="parameters">Array of IDataParameter objects containing parameters to the stored proc</param>
 35ExpandedSubBlockEnd.gif        /// <returns>Newly instantiated SqlCommand instance</returns>

 36InBlock.gif        private static SqlCommand BuildIntCommand(
 37InBlock.gif            SqlConnection connection,
 38InBlock.gif            string storedProcName, 
 39InBlock.gif            IDataParameter[] parameters)
 40ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 41InBlock.gif            SqlCommand command = 
 42InBlock.gif                BuildQueryCommand( connection,storedProcName, parameters );            
 43InBlock.gif
 44InBlock.gif            command.Parameters.Add( new SqlParameter ( "ReturnValue",
 45InBlock.gif                SqlDbType.Int,
 46ExpandedSubBlockStart.gifContractedSubBlock.gif                4,            /**//* Size */
 47InBlock.gif                ParameterDirection.ReturnValue,
 48ExpandedSubBlockStart.gifContractedSubBlock.gif                false,        /**//* is nullable */
 49ExpandedSubBlockStart.gifContractedSubBlock.gif                0,            /**//* byte precision */
 50ExpandedSubBlockStart.gifContractedSubBlock.gif                0,            /**//* byte scale */
 51InBlock.gif                string.Empty,
 52InBlock.gif                DataRowVersion.Default,
 53InBlock.gif                null ));
 54InBlock.gif
 55InBlock.gif            return command;
 56ExpandedSubBlockEnd.gif        }

 57InBlock.gif
 58ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 59InBlock.gif        /// Builds a SqlCommand designed to return a SqlDataReader, and not
 60InBlock.gif        /// an actual integer value.
 61InBlock.gif        /// </summary>
 62InBlock.gif        /// <param name="storedProcName">Name of the stored procedure</param>
 63InBlock.gif        /// <param name="parameters">Array of IDataParameter objects</param>
 64ExpandedSubBlockEnd.gif        /// <returns></returns>

 65InBlock.gif        private static SqlCommand BuildQueryCommand(
 66InBlock.gif            SqlConnection connection,
 67InBlock.gif            string storedProcName, 
 68InBlock.gif            IDataParameter[] parameters)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 70InBlock.gif            if(connectionString==null)
 71InBlock.gif                throw new ApplicationException("Sql连接字符串connectionString没有初始化");
 72InBlock.gif
 73InBlock.gif            SqlCommand command    = new SqlCommand( storedProcName,connection );
 74InBlock.gif            command.CommandType = CommandType.StoredProcedure;
 75InBlock.gif
 76InBlock.gif            foreach (SqlParameter parameter in parameters)
 77ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 78InBlock.gif                command.Parameters.Add( parameter );
 79ExpandedSubBlockEnd.gif            }

 80InBlock.gif
 81InBlock.gif            return command;
 82InBlock.gif
 83ExpandedSubBlockEnd.gif        }

 84InBlock.gif
 85ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 86InBlock.gif        /// Runs a stored procedure, can only be called by those classes deriving
 87InBlock.gif        /// from this base. It returns an integer indicating the return value of the
 88InBlock.gif        /// stored procedure, and also returns the value of the RowsAffected aspect
 89InBlock.gif        /// of the stored procedure that is returned by the ExecuteNonQuery method.
 90InBlock.gif        /// </summary>
 91InBlock.gif        /// <param name="storedProcName">Name of the stored procedure</param>
 92InBlock.gif        /// <param name="parameters">Array of IDataParameter objects</param>
 93InBlock.gif        /// <param name="rowsAffected">Number of rows affected by the stored procedure.</param>
 94ExpandedSubBlockEnd.gif        /// <returns>An integer indicating return value of the stored procedure</returns>

 95InBlock.gif        public static int RunIntProcedure(
 96InBlock.gif            string storedProcName, 
 97InBlock.gif            IDataParameter[] parameters, 
 98InBlock.gif            out int rowsAffected )
 99ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
100InBlock.gif            int result        = 0;    
101InBlock.gif            rowsAffected    = 0;
102InBlock.gif            SqlConnection connection = new SqlConnection(SqlHelper.connectionString);
103InBlock.gif            try
104ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{    
105InBlock.gif                SqlCommand command    = BuildIntCommand(connection,storedProcName, parameters );
106InBlock.gif                rowsAffected        = command.ExecuteNonQuery();
107InBlock.gif                result                = (int)command.Parameters["ReturnValue"].Value;
108ExpandedSubBlockEnd.gif            }

109InBlock.gif            finally
110ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
111InBlock.gif                connection.Close();
112ExpandedSubBlockEnd.gif            }

113InBlock.gif            return result;
114ExpandedSubBlockEnd.gif        }

115InBlock.gif
116ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
117InBlock.gif        /// 运行存储过程,并且返回存储过程的结果
118InBlock.gif        /// </summary>
119InBlock.gif        /// <param name="storedProcName">Name of the stored procedure</param>
120InBlock.gif        /// <param name="parameters">Array of IDataParameter objects</param>
121ExpandedSubBlockEnd.gif        /// <returns>An integer indicating return value of the stored procedure</returns>

122InBlock.gif        public static int RunProcedure(string storedProcName, IDataParameter[] parameters)
123ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
124InBlock.gif            int result = 0;
125InBlock.gif
126InBlock.gif            SqlConnection connection=new SqlConnection(SqlHelper.connectionString);
127InBlock.gif            try
128ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
129InBlock.gif                connection.Open();
130InBlock.gif                SqlCommand command    = BuildIntCommand(connection, storedProcName, parameters );
131InBlock.gif                command.CommandType = CommandType.StoredProcedure;            
132InBlock.gif                command.ExecuteNonQuery();
133InBlock.gif
134InBlock.gif                result                = (int)command.Parameters["ReturnValue"].Value;
135ExpandedSubBlockEnd.gif            }

136InBlock.gif            finally
137ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
138InBlock.gif                connection.Close();
139ExpandedSubBlockEnd.gif            }

140InBlock.gif
141InBlock.gif            return result;
142ExpandedSubBlockEnd.gif        }

143InBlock.gif
144ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
145InBlock.gif        /// Will run a stored procedure, can only be called by those classes deriving
146InBlock.gif        /// from this base. It returns a SqlDataReader containing the result of the stored
147InBlock.gif        /// procedure.
148InBlock.gif        /// </summary>
149InBlock.gif        /// <param name="storedProcName">Name of the stored procedure</param>
150InBlock.gif        /// <param name="parameters">Array of parameters to be passed to the procedure</param>
151InBlock.gif        /// <returns>A newly instantiated SqlDataReader object</returns>
152InBlock.gif        /// <remarks>
153InBlock.gif        /// 返回的SqlDataReader保持了一个打开的连接,一定要记住用完SqlDataReader后调用close方法。
154ExpandedSubBlockEnd.gif        /// </remarks>

155InBlock.gif        public static SqlDataReader RunDataReaderProcedure(string storedProcName, IDataParameter[] parameters )
156ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
157InBlock.gif            SqlDataReader returnReader;
158InBlock.gif            SqlConnection connection    = new SqlConnection(SqlHelper.connectionString);
159InBlock.gif
160InBlock.gif            connection.Open();
161InBlock.gif            SqlCommand command            = BuildQueryCommand( connection,storedProcName, parameters );
162InBlock.gif            command.CommandType            = CommandType.StoredProcedure;
163InBlock.gif                
164InBlock.gif            returnReader                = command.ExecuteReader();
165InBlock.gif            //connection.Close();
166InBlock.gif            return returnReader;
167ExpandedSubBlockEnd.gif        }

168InBlock.gif
169ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
170InBlock.gif        /// Creates a DataSet by running the stored procedure and placing the results
171InBlock.gif        /// of the query/proc into the given tablename.
172InBlock.gif        /// </summary>
173InBlock.gif        /// <param name="storedProcName">存储过程名称</param>
174InBlock.gif        /// <param name="parameters">存储过程参数</param>
175InBlock.gif        /// <param name="tableName">返回的DataSet中的Table的名称</param>
176ExpandedSubBlockEnd.gif        /// <returns>存储过程的结果集</returns>

177InBlock.gif        public static DataSet RunDataSetProcedure(
178InBlock.gif            string storedProcName, 
179InBlock.gif            IDataParameter[] parameters, 
180InBlock.gif            string tableName )
181ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
182InBlock.gif            DataSet dataSet                = new DataSet();
183InBlock.gif            SqlConnection connection    = new SqlConnection(SqlHelper.connectionString);
184InBlock.gif            try
185ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
186InBlock.gif                connection.Open();
187InBlock.gif                SqlDataAdapter sqlDA    = new SqlDataAdapter();
188InBlock.gif                sqlDA.SelectCommand        = BuildQueryCommand(connection, storedProcName, parameters );            
189InBlock.gif                sqlDA.Fill( dataSet, tableName );
190ExpandedSubBlockEnd.gif            }

191InBlock.gif            finally
192ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
193InBlock.gif                connection.Close();
194ExpandedSubBlockEnd.gif            }

195InBlock.gif
196InBlock.gif            return dataSet;
197ExpandedSubBlockEnd.gif        }

198InBlock.gif
199ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
200InBlock.gif        /// 运行一个存储过程,并且结果集用DataSet形式返回
201InBlock.gif        /// </summary>
202InBlock.gif        /// <param name="storedProcName">存储过程名称</param>
203InBlock.gif        /// <param name="parameters">存储过程参数</param>
204ExpandedSubBlockEnd.gif        /// <returns>存储过程的结果集,DataSet中的表名为Sql操作的数据表名</returns>

205InBlock.gif        public static DataSet RunDataSetProcedure(string storedProcName, IDataParameter[] parameters)
206ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
207InBlock.gif            DataSet dataSet                = new DataSet();
208InBlock.gif            SqlConnection connection    = new SqlConnection(SqlHelper.connectionString);
209InBlock.gif
210InBlock.gif            try
211ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
212InBlock.gif                connection.Open();
213InBlock.gif                SqlDataAdapter sqlDA    = new SqlDataAdapter();
214InBlock.gif                sqlDA.SelectCommand        = BuildQueryCommand(connection, storedProcName, parameters );
215InBlock.gif                sqlDA.Fill( dataSet);
216ExpandedSubBlockEnd.gif            }

217InBlock.gif            finally
218ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
219InBlock.gif                connection.Close();
220ExpandedSubBlockEnd.gif            }

221InBlock.gif
222InBlock.gif            return dataSet;
223ExpandedSubBlockEnd.gif        }

224InBlock.gif
225ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
226InBlock.gif        /// Takes an -existing- dataset and fills the given table name with the results
227InBlock.gif        /// of the stored procedure.
228InBlock.gif        /// </summary>
229InBlock.gif        /// <param name="storedProcName">存储过程名称</param>
230InBlock.gif        /// <param name="parameters">存储过程参数</param>
231InBlock.gif        /// <param name="dataSet">已有的DataSet,将向其中添加表数据</param>
232InBlock.gif        /// <param name="tableName">将向DataSet中添加数据的表名称</param>
233ExpandedSubBlockEnd.gif        /// <returns></returns>

234InBlock.gif        public static void RunDataSetProcedure(
235InBlock.gif            string storedProcName, 
236InBlock.gif            IDataParameter[] parameters, 
237InBlock.gif            DataSet dataSet, 
238InBlock.gif            string tableName )
239ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
240InBlock.gif            SqlConnection connection    = new SqlConnection(SqlHelper.connectionString);
241InBlock.gif            try
242ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
243InBlock.gif                connection.Open();
244InBlock.gif                SqlDataAdapter sqlDA    = new SqlDataAdapter();
245InBlock.gif                sqlDA.SelectCommand        = BuildIntCommand( connection,storedProcName, parameters );
246InBlock.gif                sqlDA.Fill( dataSet, tableName );
247ExpandedSubBlockEnd.gif            }

248InBlock.gif            finally
249ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
250InBlock.gif                connection.Close();
251ExpandedSubBlockEnd.gif            }

252ExpandedSubBlockEnd.gif        }

253InBlock.gif
254ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
255InBlock.gif        /// 运行一个存储过程,并且结果集用DataTable形式返回,这是DataSet中的第一个表
256InBlock.gif        /// </summary>
257InBlock.gif        /// <param name="storedProcName">存储过程名字</param>
258InBlock.gif        /// <param name="parameters">Sql参数</param>
259InBlock.gif        /// <returns>结果集的第一个表</returns>
260InBlock.gif        /// <remarks>不管结果集有多少个表,该方法仅仅返回结果集的第一个表.如果结果集不存在,返回null
261ExpandedSubBlockEnd.gif        /// </remarks>

262InBlock.gif        public static DataTable RunDataTableProcedure(string storedProcName, IDataParameter[] parameters)
263ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
264InBlock.gif            DataSet dataSet    = RunDataSetProcedure(storedProcName,parameters);
265InBlock.gif            if( dataSet!=null && dataSet.Tables.Count>0 )
266ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
267InBlock.gif                return dataSet.Tables[0];
268ExpandedSubBlockEnd.gif            }

269InBlock.gif            else
270ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
271InBlock.gif                return null;
272ExpandedSubBlockEnd.gif            }

273ExpandedSubBlockEnd.gif        }

274InBlock.gif
275ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
276InBlock.gif        /// 运行一个存储过程,并且结果集用DataRow形式返回,这是DataSet中的第一个表的第一行
277InBlock.gif        /// </summary>
278InBlock.gif        /// <param name="storedProcName">存储过程名字</param>
279InBlock.gif        /// <param name="parameters">Sql参数</param>
280InBlock.gif        /// <returns>结果集的第一个表的第一行</returns>
281InBlock.gif        /// <remarks>不管结果集有多少行,该方法仅仅返回第一行,如果结果集不存在,返回null
282ExpandedSubBlockEnd.gif        /// </remarks>

283InBlock.gif        public static DataRow RunDataRowProcedure(string storedProcName, IDataParameter[] parameters)
284ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
285InBlock.gif            DataTable dataTable = RunDataTableProcedure(storedProcName,parameters);
286InBlock.gif            if( dataTable!=null && dataTable.Rows.Count>0 )
287ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
288InBlock.gif                return dataTable.Rows[0];
289ExpandedSubBlockEnd.gif            }

290InBlock.gif            else
291ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
292InBlock.gif                return null;
293ExpandedSubBlockEnd.gif            }

294ExpandedSubBlockEnd.gif        }

295InBlock.gif
296ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
297InBlock.gif        /// 运行一个存储过程,并且结果集用DataTable形式返回,这是DataSet中的第一个表的第一行
298InBlock.gif        /// </summary>
299InBlock.gif        /// <param name="storedProcName">存储过程名字</param>
300InBlock.gif        /// <param name="parameters">Sql参数</param>
301InBlock.gif        /// <returns>结果集的第一个表的第一行的第一列</returns>
302InBlock.gif        /// <remarks>
303InBlock.gif        /// 不管结果集有多少行,该方法仅仅返回第一行的第一个值,如果结果集不存在,返回null
304ExpandedSubBlockEnd.gif        /// </remarks>

305InBlock.gif        public static object RunScalarProcedure(string storedProcName, IDataParameter[] parameters)
306ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
307InBlock.gif            DataRow row = RunDataRowProcedure(storedProcName,parameters);
308InBlock.gif            if( row!=null && row.ItemArray.Length>0 )
309ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
310InBlock.gif                return row.ItemArray[0];
311ExpandedSubBlockEnd.gif            }

312InBlock.gif            else
313ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
314InBlock.gif                return null;
315ExpandedSubBlockEnd.gif            }

316ExpandedSubBlockEnd.gif        }

317ExpandedSubBlockEnd.gif    }

318ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/sjpisaboy/archive/2006/04/06/368425.html

数据治理是确保数据准确性、可靠性、安全性、可用性和完整性的体系和框架。它定义了组织内部如何使用、存储、保护和共享数据的规则和流程。数据治理的重要性随着数字化转型的加速而日益凸显,它能够提高决策效率、增强业务竞争力、降低风险,并促进业务创新。有效的数据治理体系可以确保数据在采集、存储、处理、共享和保护等环节的合规性和有效性。 数据质量管理是数据治理中的关键环节,它涉及数据质量评估、数据清洗、标准化和监控。高质量的数据能够提升业务决策的准确性,优化业务流程,并挖掘潜在的商业价值。随着大数据和人工智能技术的发展,数据质量管理在确保数据准确性和可靠性方面的作用愈发重要。企业需要建立完善的数据质量管理和校验机制,并通过数据清洗和标准化提高数据质量。 数据安全与隐私保护是数据治理中的另一个重要领域。随着数据量的快速增长和互联网技术的迅速发展,数据安全与隐私保护面临前所未有的挑战。企业需要加强数据安全与隐私保护的法律法规和技术手段,采用数据加密、脱敏和备份恢复等技术手段,以及加强培训和教育,提高安全意识和技能水平。 数据流程管理与监控是确保数据质量、提高数据利用率、保护数据安全的重要环节。有效的数据流程管理可以确保数据流程的合规性和高效性,而实时监控则有助于及时发现并解决潜在问题。企业需要设计合理的数据流程架构,制定详细的数据管理流程规范,并运用数据审计和可视化技术手段进行监控。 数据资产管理是将数据视为组织的重要资产,通过有效的管理和利用,为组织带来经济价值。数据资产管理涵盖数据的整个生命周期,包括数据的创建、存储、处理、共享、使用和保护。它面临的挑战包括数据量的快速增长、数据类型的多样化和数据更新的迅速性。组织需要建立完善的数据管理体系,提高数据处理和分析能力,以应对这些挑战。同时,数据资产的分类与评估、共享与使用规范也是数据资产管理的重要组成部分,需要制定合理的标准和规范,确保数据共享的安全性和隐私保护,以及建立合理的利益分配和权益保障机制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值