DB通用类:SQLServer通用类库

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

DB通⽤类:SQLServer通⽤类库

SQLServer通⽤类A

using System;

using System.Data;

using System.Data.SqlClient;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Xml;

namespace CommonLib

{

class DB_SQLServerA

{

///<summary>

///数据库连接字符串

///</summary>

//public static string connectionString = String.Format("packet size=4096;data source={0};persist security info=True;initial catalog={1};user id={2};password={3}",

// ConfigurationManager.AppSettings["DataSourse"], ConfigurationManager.AppSettings["DataBase"], ConfigurationManager.AppSettings["UserName"], ConfigurationManager.AppSettings["PassWord"]);

////public static readonly string connectionString = String.Format("packet size=4096;data source={0};persist security info=True;initial catalog={1};user id={2};password={3}", System.Configuration.ConfigurationManager.AppSettings["ServerIP // System.Configuration.ConfigurationManager.AppSettings["Server"], System.Configuration.ConfigurationManager.AppSettings["user"], System.Configuration.ConfigurationManager.AppSettings["password"]);

// Hashtable to store cached parameters

private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());

public static string connectionString = "";

public static void SetLinkString(string strDataource, string strDataBase, string strUserID, string strPWD)

{

if ((strDataBase == null) || (strDataBase.Length <= 0) || (strDataource == null) || (strDataource.Length <= 0)

|| (strUserID == null) || (strUserID.Length <= 0) || (strPWD == null) || (strPWD.Length <= 0))

{

MessageBox.Show("输⼊链接字符串属性错误");

}

else

{

connectionString = "packet size=4096;data source=" + strDataource + ";persist security info=True;initial catalog=" + strDataBase

+ ";user id=" + strUserID + ";password=" + strPWD + ";";

}

}

#region//GetTable⽅法

///<summary>

///执⾏⼀条返回结果集的SqlCommand,通过⼀个已经存在的数据库连接

///使⽤参数数组提供参数

///</summary>

///<param name="connecttionString">⼀个现有的数据库连接</param>

///<param name="cmdTye">SqlCommand命令类型</param>

///<param name="cmdText">存储过程的名字或者 T-SQL 语句</param>

///<param name="commandParameters">以数组形式提供SqlCommand命令中⽤到的参数列表</param>

///<returns>返回⼀个表集合(DataTableCollection)表⽰查询得到的数据集</returns>

public static DataTableCollection QuaryData(string connecttionString, CommandType cmdTye, string cmdText, SqlParameter[] commandParameters)

{

SqlCommand cmd = new SqlCommand();

DataSet ds = new DataSet();

using (SqlConnection conn = new SqlConnection(connecttionString))

{

PrepareCommand(cmd, conn, null, cmdTye, cmdText, commandParameters);

SqlDataAdapter adapter = new SqlDataAdapter();

adapter.SelectCommand = cmd;

adapter.Fill(ds);

}

DataTableCollection table = ds.Tables;

return table;

}

///<summary>

///执⾏⼀条返回结果集的SqlCommand,通过⼀个已经存在的数据库连接

///使⽤参数数组提供参数

///</summary>

///<param name="cmdText">T-SQL 语句</param>

///<returns></returns>

public static DataTableCollection QuaryData(string cmdText)

{

return QuaryData(DB_SQLServerA.connectionString, CommandType.Text, cmdText, null);

}

///<summary>

///执⾏⼀条返回结果集的SqlCommand,通过⼀个已经存在的数据库连接

///使⽤参数数组提供参数

///</summary>

///<param name="cmdTye">SqlCommand命令类型</param>

///<param name="cmdText">存储过程的名字或者 T-SQL 语句</param>

///<param name="commandParameters">以数组形式提供SqlCommand命令中⽤到的参数列表</param>

///<returns>返回⼀个表集合(DataTableCollection)表⽰查询得到的数据集</returns>

public static DataTableCollection QuaryData(CommandType cmdTye, string cmdText, SqlParameter[] commandParameters)

{

return QuaryData(DB_SQLServerA.connectionString, cmdTye, cmdText, commandParameters);

}

///<summary>

///存储过程专⽤

///</summary>

///<param name="cmdText">存储过程的名字</param>

///<param name="commandParameters">以数组形式提供SqlCommand命令中⽤到的参数列表</param>

///<returns>返回⼀个表集合(DataTableCollection)表⽰查询得到的数据集</returns>

public static DataTableCollection QuaryDataProducts(string cmdText, SqlParameter[] commandParameters)

{

return QuaryData(DB_SQLServerA.connectionString, CommandType.StoredProcedure, cmdText, commandParameters);

}

///<summary>

/// Sql语句专⽤

///</summary>

///<param name="cmdText"> T-SQL 语句</param>

///<param name="commandParameters">以数组形式提供SqlCommand命令中⽤到的参数列表</param>

///<returns>返回⼀个表集合(DataTableCollection)表⽰查询得到的数据集</returns>

public static DataTableCollection QuaryDataText(string cmdText, SqlParameter[] commandParameters)

{

return QuaryData(DB_SQLServerA.connectionString, CommandType.Text, cmdText, commandParameters);

}

#endregion

///<summary>

///为执⾏命令准备参数

相关文档
最新文档