關於SQLSERVER資料操作的常用類庫
來源:互聯網
上載者:User
server|sqlserver|資料 根據自己平時的經驗寫的針對SQLSERVER操作的通用類庫
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
namespace Whgw_lc
{
/// <summary>
/// Data 的摘要說明。
/// </summary>
/// <summary>
/// 資料庫相關操作類
/// 作者:DarkAngel
/// 時間:2004-9-24
/// </summary>
public class Data
{
/// <summary>
///串連資料庫字串
/// </summary>
protected static string con="data source=10.68.19.203;initial catalog=Whgw;persist security info=False;user id=sa;password=sc2000;workstation id=DARKANGEL;packet size=4096";
/// <summary>
/// 資料庫連接對象
/// </summary>
protected System.Data .SqlClient.SqlConnection DataConnection;
/// <summary>
/// SqlDataAdapter對象
/// </summary>
protected System.Data.SqlClient.SqlDataAdapter DataAdapter;
/// <summary>
/// SqlCommand對象
/// </summary>
protected System.Data.SqlClient.SqlCommand DataCommand;
/// <summary>
/// SqlCommandBuilder對象
/// </summary>
protected System.Data.SqlClient.SqlCommandBuilder DataComBuilder;
/// <summary>
/// DataSet對象
/// </summary>
protected System.Data.DataSet DataSet;
/// <summary>
/// DataRow對象
/// </summary>
protected System.Data.DataRow DataRow;
/// <summary>
/// SqlDataReader對象
/// </summary>
protected System.Data.SqlClient.SqlDataReader DataReader;
//protected System.Web.UI.Page mypage;
/// <summary>
///建構函式
/// </summary>
public Data()
{
//
// TODO: 串連資料庫參數
//
//con=
DataConnection=DataConn();
}
/// <summary>
/// 資料庫連接參數屬性
/// </summary>
public string Con
{
get{return con;}
set{con= value;}
}
/// <summary>
/// 建立資料庫連接
/// </summary>
/// <returns>返回資料庫連接對象</returns>
public SqlConnection DataConn()
{
SqlConnection DataConnection=new SqlConnection(Con);
return DataConnection;
}
/// <summary>
/// 查詢結果,返回
/// </summary>
/// <param name="mysql">查詢語句</param>
/// <returns>返回datareader</returns>
public SqlDataReader DataQuery(string Sql)
{
DataConnection=DataConn();
DataConnection.Open();
DataReader=null;
DataCommand=new SqlCommand(Sql,DataConnection);
DataReader=DataCommand.ExecuteReader();
return DataReader;
}
/// <summary>
/// 查詢結果,返回DataSet
/// </summary>
/// <param name="mysql">查詢語句</param>
/// <param name="tabName">表名</param>
/// <returns>DataSet</returns>
public virtual DataSet DataSetQuery(string Sql,string TableName)
{
DataConnection=DataConn();
DataConnection.Open();
DataAdapter=new SqlDataAdapter(Sql,DataConnection);
DataSet=new DataSet();
DataAdapter.Fill(DataSet,TableName);
return DataSet;
}
public DataSet DataSetQuery(DataSet myds,string TableName,string Sql)
{
DataConnection=DataConn();
DataConnection.Open();
DataAdapter=new SqlDataAdapter(Sql,DataConnection);
DataAdapter.Fill(myds,TableName);
return DataSet;
}
/// <summary>
/// 查詢結果,返回DataSet,帶起始和結束
/// </summary>
/// <param name="mysql">查詢語句</param>
/// <param name="tabName">表名</param>
/// <param name="starIndex">起始行</param>
/// <param name="count">終止行</param>
/// <returns>返回DataSet</returns>
public DataSet DataSetQuery(string Sql,string TableName,int StarIndex,int Count)
{
DataConnection=DataConn();
DataConnection.Open();
DataAdapter=new SqlDataAdapter(Sql,DataConnection);
DataSet=new DataSet();
DataAdapter.Fill(DataSet,StarIndex,Count,TableName);
return DataSet;
}
/// <summary>
/// 修改資料庫內容
/// </summary>
/// <param name="mysql">查詢語句</param>
/// <returns>如果執行成功返回真,否者返回假</returns>
public bool EditData(string Sql)
{
DataConnection=DataConn();
DataConnection.Open();
DataCommand=new SqlCommand(Sql,DataConnection);
try
{
DataCommand.ExecuteNonQuery();
return true;
}
catch(Exception ex)
{
System.Console.Write(ex.Message.ToString());
return false;
}
DataConnection.Close();
}
/// <summary>
/// 修改DataSet內容,並更新資料庫
/// </summary>
/// <param name="DataSet">DataSet對象</param>
/// <param name="myda">SqlDataAdapter容器</param>
/// <param name="tbName">表名</param>
/// <returns>返回DataSet</returns>
public DataSet EditData(DataSet DataSet,SqlDataAdapter myda,string TableName)
{
DataComBuilder=new SqlCommandBuilder(myda);
myda.UpdateCommand=DataComBuilder.GetUpdateCommand();
myda.DeleteCommand=DataComBuilder.GetDeleteCommand();
myda.InsertCommand=DataComBuilder.GetInsertCommand();
myda.Update(DataSet,TableName);
DataSet.AcceptChanges();
return DataSet;
}
/// <summary>
/// 無參數預存程序的執行
/// </summary>
/// <param name="proName">預存程序名稱</param>
/// <returns>是否執行成功</returns>
public bool DataProcedure(string ProcedureName)
{
DataConnection=DataConn();
DataConnection.Open();
DataCommand=new SqlCommand();
DataCommand.CommandText=ProcedureName;
DataCommand.Connection=DataConnection;
DataCommand.CommandType=System.Data.CommandType.StoredProcedure;
try
{
DataCommand.ExecuteNonQuery();
return true;
}
catch(Exception ex)
{
System.Console.Write(ex.Message.ToString());
return false;
}
}
/// <summary>
/// 關閉資料庫連接
/// </summary>
public void CloseConnection()
{
DataConnection.Close();
}
}
}