c#sqllink数据库操作类

上传人:xiao****1972 文档编号:84136311 上传时间:2019-03-02 格式:DOC 页数:14 大小:52KB
返回 下载 相关 举报
c#sqllink数据库操作类_第1页
第1页 / 共14页
c#sqllink数据库操作类_第2页
第2页 / 共14页
c#sqllink数据库操作类_第3页
第3页 / 共14页
c#sqllink数据库操作类_第4页
第4页 / 共14页
c#sqllink数据库操作类_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《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.

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号