《c#sqllink数据库操作类》由会员分享,可在线阅读,更多相关《c#sqllink数据库操作类(14页珍藏版)》请在金锄头文库上搜索。
1、sqllink.csc#数据库操作类using System;using System.Configuration;using System.Data;using System.Data.SqlClient;namespace Chenzhang/ /SQL Server数据库操作组件/ public class SqlLink :IDisposable private SqlDataAdapter dsCommand;private AppSettingsReader appsettingsreader;private String cn;/ / Constructor/ public Sq
2、lLink()dsCommand =new SqlDataAdapter() ;appsettingsreader = new AppSettingsReader();cn=(string)appsettingsreader.GetValue(SQLConnectionString, typeof(string);/ / Dispose/ public void Dispose()Dispose(true);GC.SuppressFinalize(this);protected virtual void Dispose(bool disposing) if (!disposing) retur
3、n;if (dsCommand != null) /dispose dsCommand.SelectCommandif (dsCommand.SelectCommand!=null)if (dsCommand.SelectCommand.Connection!=null)dsCommand.SelectCommand.Connection.Dispose();dsCommand.SelectCommand.Dispose();/dispose dsCommand.InsertCommandif (dsCommand.InsertCommand!=null)if (dsCommand.Inser
4、tCommand.Connection!=null)dsCommand.InsertCommand.Connection.Dispose();dsCommand.InsertCommand.Dispose();/dispose dsCommand.UpdateCommandif (dsCommand.UpdateCommand!=null)if (dsCommand.UpdateCommand.Connection!=null)dsCommand.UpdateCommand.Connection.Dispose();dsCommand.UpdateCommand.Dispose();dsCom
5、mand.Dispose();dsCommand=null;/ / 获取select语句查询结果记录集/ / select语句/ 结果记录集public DataTable SelectSql(String sql)dsCommand.SelectCommand=new SqlCommand();dsCommand.SelectCommand.Connection=new SqlConnection(cn);DataSet ds=new DataSet() ;dsCommand.SelectCommand.CommandText=sql;dsCommand.SelectCommand.Comm
6、andType=CommandType.Text;dsCommand.SelectCommand.Connection.Open();trydsCommand.Fill(ds,result);return ds.Tablesresult;catch return null;finallyif (dsCommand.SelectCommand!=null)if (dsCommand.SelectCommand.Connection!=null)dsCommand.SelectCommand.Connection.Dispose();dsCommand.SelectCommand.Dispose(
7、);dsCommand.Dispose();ds.Dispose();/ / 获取存储过程运行结果记录集,存储过程不带参数/ / 存储过程名称/ 结果记录集public DataTable SelectSql_sp(String sp_name)dsCommand.SelectCommand=new SqlCommand();dsCommand.SelectCommand.Connection=new SqlConnection(cn);DataSet ds=new DataSet() ;dsCommand.SelectCommand.CommandText=sp_name;dsCommand
8、.SelectCommand.CommandType=CommandType.StoredProcedure;dsCommand.SelectCommand.Connection.Open();trydsCommand.Fill(ds,result);return ds.Tablesresult;catch return null;finallyif (dsCommand.SelectCommand!=null)if (dsCommand.SelectCommand.Connection!=null)dsCommand.SelectCommand.Connection.Dispose();ds
9、Command.SelectCommand.Dispose();dsCommand.Dispose();ds.Dispose();/ / 获取存储过程运行结果记录集,存储过程带1个参数/ / 存储过程名称/ 存储过程参数名称,如Age,StudentName/ 存储过程参数类型,如int,varchar/ 存储过程参数值,如18,张三/ 结果记录集public DataTable SelectSql_sp_par1(String sp_name,String par1_name,String par1_type,String par1_value)int par1_dbtype=0;if (p
10、ar1_type.ToLower()=varchar)par1_dbtype=Convert.ToInt16(SqlDbType.VarChar);else if (par1_type.ToLower()=string)par1_dbtype=Convert.ToInt16(SqlDbType.VarChar);else if (par1_type.ToLower()=integer) par1_dbtype=Convert.ToInt16(SqlDbType.Int);else if (par1_type.ToLower()=int) par1_dbtype=Convert.ToInt16(
11、SqlDbType.Int);dsCommand.SelectCommand=new SqlCommand();dsCommand.SelectCommand.Connection=new SqlConnection(cn);DataSet ds=new DataSet() ;dsCommand.SelectCommand.CommandText=sp_name;dsCommand.SelectCommand.CommandType=CommandType.StoredProcedure;SqlParameter objPar;objPar=dsCommand.SelectCommand.Pa
12、rameters.Add(par1_name,par1_dbtype);objPar.Direction=ParameterDirection.Input;objPar.Value=par1_value;dsCommand.SelectCommand.Connection.Open();trydsCommand.Fill(ds,result);return ds.Tablesresult;catch return null;finallyif (dsCommand.SelectCommand!=null)if (dsCommand.SelectCommand.Connection!=null)dsCommand.SelectCommand.Connection.