您的位置:首页 > 数据库

C# SQL Server的封装(SQLDataAdapter等)之一(共二)

2017-08-05 21:54 477 查看
本文章仅是本人学习后总结,如有错误之处,请大家指正。
既然你看到这篇文章,说明最少懂,如果看完了还不懂。。。。。你怎么不上天呢。。。
以下Demo均以SQL Server为例说明。
七个DEMO分别说明以下情况
1.connection字符串
2.Command的常用的三个方法。
3.SqlDataApater的Fill方法。
4.SqlDataAdapter的Update和SqlDataBuilder之间的协作。
5.如何执行存储。
6.如何执行事务。
7.简单的扩展,针对三层架构,表对应类,封装快速获取数据。


Demo1.

说明:SQL的连接字符串,举例说明常见的SQL连接字符串的。

//实际存储字符串的字段
private static string _conStr;

//获取字符串,这是本人常用的方式
public static string ConStr {
get {
if (string.IsNullOrEmpty(_conStr))
{
//需要引用System.Configuration
//获取app.config中的配置文件,这个配置文件指的是程序运行目录下的config文件,而不是指项目下的配置文件
_conStr = System.Configuration.ConfigurationManager.ConnectionStrings["SQL"].ConnectionString;
}
return _conStr;
}
}

//Config文件内容
<configuration>
<connectionStrings>
<!--有很多连接字符串方式,这两种最常见-->
<!--常见的方式1:建议采用此方法-->
<add name="SQL" connectionString="Data Source=.;Initial Catalog=DemoDate;User ID=sa;Password=123.com;"/>
<!--常见的方式2:能看懂即可-->
<add name="SQL1" connectionString="Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;"/>
</connectionStrings>
</configuration>


Demo2:

说明:封装SQL的Command的常用的三个方法

//params 所修饰的参数,必须为最后一个,且参数类型必须为数组,表明不确定参数个数
//封装SQLExecuteNonQuery返回所影响的行数
public static int SQLExecuteNonQuery(string comStr, params SqlParameter[] paramters)
{
//使用using,可以在对应调用完成后,自动调用Close
using (SqlConnection connection = new SqlConnection(SQLHelper.ConStr))
{
using (SqlCommand command = connection.CreateCommand())
{
try
{
command.CommandText = comStr;
if (paramters != null)
{
command.Parameters.AddRange(paramters);
}
//必须先将连接打开
connection.Open();
return command.ExecuteNonQuery();
}
catch (Exception ex)
{
//异常后进行连接的关闭
connection.Close();
throw ex;
}
}
}
}

//封装ExecuteScalar方法,返回单行单列值
//使用泛型方法,内部进行类型转换,当然你得确保类型转换不会出错
public static T SQLExecuteScalar<T>(string comStr,params SqlParameter[] paramters)
{
//使用using,可以在对应调用完成后,自动调用Close
using (SqlConnection connection = new SqlConnection(SQLHelper.ConStr))
{
using (SqlCommand command = connection.CreateCommand())
{
try
{
command.CommandText = comStr;
if (paramters != null)
{
command.Parameters.AddRange(paramters);
}
//必须先将连接打开,在执行command的方法之前打开就可以,没必有创建connection就进行打开连接
connection.Open();
return (T)command.ExecuteScalar();
}
catch (Exception ex)
{
//异常后进行连接的关闭
connection.Close();
throw ex;
}
}
}
}

//封装ExecuteReader方法,这样会产生一个问题Connection什么时候进行关闭,调用完方法后,Reader一直进行读取connection未关闭,怎么办
public static SqlDataReader SQLDataReader(string comStr, params SqlParameter[] paramters)
{
using (SqlConnection connection = new SqlConnection(SQLHelper.ConStr))
{
using (SqlCommand command = connection.CreateCommand())
{
try
{
command.CommandText = comStr;
if (paramters != null)
{
command.Parameters.AddRange(paramters);
}
//必须先将连接打开
connection.Open();
//CommandBehavior.CloseConnection
//在执行该命令时,如果关闭关联的 DataReader 对象,则关联的 Connection 对象也将关闭。
return command.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
//异常后进行连接的关闭
connection.Close();
throw ex;
}
}
}
}


Demo3:

说明:SqlDataApater的Fill方法

//使用SqlDataAdapter进行数据的填充
//此处comStr参数必须为SELECT
public static DataSet SQLAdapter(string comStr, params SqlParameter[] paramters)
{
DataSet dSet=new DataSet();

ad63
using (SqlDataAdapter adapter = new SqlDataAdapter(comStr, SQLHelper.ConStr))
{
try
{
if (paramters != null)
{
adapter.SelectCommand.Parameters.AddRange(paramters);
}
adapter.Fill(dSet);
return dSet;
}
catch (Exception ex)
{
adapter.SelectCommand.Connection.Close();
throw ex;
}
}
}


以上没有进行详细测试,只是正常生成,自己学习时,请多测试,练习。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息