資源簡介
C#的sqlserver數(shù)據(jù)庫操作封裝類,封裝了sql語句的查詢、修改、插入、刪除操作,以及存儲(chǔ)過程的執(zhí)行,包括有輸入、輸出參數(shù)的存儲(chǔ)過程,存儲(chǔ)過程的執(zhí)行無需輸入任何參數(shù)名稱,只需輸入?yún)?shù)值即可。同時(shí)封裝了大批量數(shù)據(jù)的更新操作,是普通DataAdapter和Command批量操作效率的30倍以上。
代碼片段和文件信息
using?System;
using?System.Collections;
using?System.Collections.Generic;
using?System.Linq;
using?System.Web;
using?System.Data.SqlClient;
using?System.Data;
namespace?SqlHelper
{
????public?class?SqlCompose
????{
????????#region?構(gòu)造函數(shù)
????????///?
????????///?構(gòu)造函數(shù)
????????///?
????????///?
????????public?SqlCompose(string?connStr)
????????{
????????????this.connStr?=?connStr;
????????}
????????#endregion
????????#region?變量
????????//數(shù)據(jù)庫連接對象
????????private?SqlConnection?_connection?=?null;
????????//數(shù)據(jù)庫連接字符串
????????private?string?connStr?=?string.Empty;
????????#endregion
????????#region?屬性
????????///?
????????///?數(shù)據(jù)庫連接對象
????????///?
????????private?SqlConnection?Connnection
????????{
????????????get
????????????{
????????????????if?(this._connection?==?null?||?this._connection.State?==?ConnectionState.Broken?||?this._connection.State?==?ConnectionState.Closed)
????????????????{
????????????????????this._connection?=?new?SqlConnection(connStr);
????????????????}
????????????????return?this._connection;
????????????}????????????
????????}
????????#endregion
????????#region?方法
????????#region?BuildQueryCommand
????????///?
????????///?創(chuàng)建Command對象
????????///?
????????///?sql語句或存儲(chǔ)過程名稱
????????///?是否是存儲(chǔ)過程
????????///?輸入?yún)?shù)集合???
????????///?輸出參數(shù)集合?
????????///?
????????private?SqlCommand?BuildQueryCommand(string?sqlOrprocedureName?bool?isProcedure?object[]?inParameters?object[]?outParameters)
????????{
????????????SqlCommand?command?=?new?SqlCommand(sqlOrprocedureName?this.Connnection);
????????????command.CommandType?=?isProcedure???CommandType.StoredProcedure?:?CommandType.Text;
????????????if?(isProcedure?&&?((inParameters?!=?null?&&?inParameters.Length?>?0)?||?(outParameters?!=?null?&&?outParameters.Length?>?0)))
????????????{
????????????????//獲取存儲(chǔ)過程參數(shù),獲取參數(shù)順序?yàn)椋鎯?chǔ)過程執(zhí)行返回值ReturnValue>輸入?yún)?shù)Input>輸入輸出參數(shù)InputOutput>輸出參數(shù)Output。
????????????????//其中Output為純輸出參數(shù),不用傳參,但sqlserver存儲(chǔ)過程里定義的輸出參數(shù),在C#中一般為InputOutput類型,除非存儲(chǔ)過程中定義的是返回值而非輸出參數(shù)。
????????????????command.Connection.Open();
????????????????SqlCommandBuilder.DeriveParameters(command);
????????????????command.Connection.Close();
????????????????
????????????????try
????????????????{???????????????????
????????????????????//存儲(chǔ)過程參數(shù)賦值
????????????????????for?(int?i?=?1;?i?????????????????????{
????????????????????????if?(inParameters?!=?null?&&?inParameters.Length?>?0?&&?i?<=?inParameters.Length)
????????????????????????{
????????????????????????????command.Parameters[i].Value?=?inParameters[i?-?1];
????????????????????????}
????????????????????????else?if?(outParameters?!=?null?&&?outParameters.Length?>?0)
????????????????????????{
???????
評論
共有 條評論