关于SQLSERVER数据操作的常用类库

80酷酷网    80kuku.com

  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();
}

}
}



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: