主题:关于连接数据库的疑问?
以下代码是连接数据库的定义的一个类,高手提供的。我的问题是:为什么“执行无返回值的存储过程”的函数需要Open和Close连接,而“执行返回数据集的存储过程”的函数不需要Open和Close连接?谢谢!
using System;
using System.Data;
using System.Data.SqlClient;
namespace DBAccess
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Class1
{
private SqlConnection conn = null;
private SqlCommand cmd = null;
private string connStr = null;
public string ConnStr
{
set
{
this.connStr = value;
}
get
{
return this.connStr;
}
}
public Class1()
{
this.connStr = ("server = (local) ; database = demo ; uid = sa ; pwd = sa");
this.conn = new SqlConnection(this.connStr);
}
/// <summary>
/// 有参构造函数
/// </summary>
/// <param name="connectionString"> </param>
public Class1(string connectionString)
{
//指定连接字体串
this.connStr = connectionString;
//创建连接对象
this.conn = new SqlConnection(this.connStr);
}
/// <summary>
/// 打开数据库连接
/// </summary>
public void Open()
{
if (this.conn != null && this.conn.State == ConnectionState.Closed)
{
this.conn.Open();
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void Close()
{
if (this.conn != null && this.conn.State == ConnectionState.Open)
{
this.conn.Close();
}
}
/// <summary>
/// 执行返回数据集的存储过程
/// 适用于断开模式的查询
/// </summary>
/// <param name="proc"> </param>
/// <param name="paraList"> </param>
/// <returns> </returns>
public DataSet ExcuteReturnDataSet(string proc, SqlParameter[] paramList)
{
//创建命令对象
this.cmd = new SqlCommand();
//指定连接对象
this.cmd.Connection = this.conn;
//指定命令类型
this.cmd.CommandType = CommandType.StoredProcedure;
//指定存储过程名
this.cmd.CommandText = proc;
//设置参数
if (paramList != null && paramList.Length > 0)
{
foreach (SqlParameter para in paramList)
{
this.cmd.Parameters.Add(para);
}
}
//创建数据适配器
SqlDataAdapter da = new SqlDataAdapter();
//指定数据适配器所用的查询命令
da.SelectCommand = this.cmd;
//创建数据集
DataSet ds = new DataSet();
//填充
da.Fill(ds);
//返回数据集
return ds;
}
/// <summary>
/// 执行无返回值的存储过程
/// 适用于连接模式的增、删、改
/// </summary>
/// <param name="proc"> </param>
/// <param name="paramList"> </param>
public void ExcuteCommand(string proc, SqlParameter[] paramList)
{
//打开数据库连接
this.Open();
//创建命令对象
this.cmd = new SqlCommand();
//指定连接对象
this.cmd.Connection = this.conn;
//指定命令类型
this.cmd.CommandType = CommandType.StoredProcedure;
//指定存储过程名
this.cmd.CommandText = proc;
//设置参数
if (paramList != null && paramList.Length > 0)
{
foreach (SqlParameter para in paramList)
{
this.cmd.Parameters.Add(para);
}
}
//执行存储过程
this.cmd.ExecuteNonQuery();
//关闭数据库连接
this.Close();
}
}
}
using System;
using System.Data;
using System.Data.SqlClient;
namespace DBAccess
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Class1
{
private SqlConnection conn = null;
private SqlCommand cmd = null;
private string connStr = null;
public string ConnStr
{
set
{
this.connStr = value;
}
get
{
return this.connStr;
}
}
public Class1()
{
this.connStr = ("server = (local) ; database = demo ; uid = sa ; pwd = sa");
this.conn = new SqlConnection(this.connStr);
}
/// <summary>
/// 有参构造函数
/// </summary>
/// <param name="connectionString"> </param>
public Class1(string connectionString)
{
//指定连接字体串
this.connStr = connectionString;
//创建连接对象
this.conn = new SqlConnection(this.connStr);
}
/// <summary>
/// 打开数据库连接
/// </summary>
public void Open()
{
if (this.conn != null && this.conn.State == ConnectionState.Closed)
{
this.conn.Open();
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void Close()
{
if (this.conn != null && this.conn.State == ConnectionState.Open)
{
this.conn.Close();
}
}
/// <summary>
/// 执行返回数据集的存储过程
/// 适用于断开模式的查询
/// </summary>
/// <param name="proc"> </param>
/// <param name="paraList"> </param>
/// <returns> </returns>
public DataSet ExcuteReturnDataSet(string proc, SqlParameter[] paramList)
{
//创建命令对象
this.cmd = new SqlCommand();
//指定连接对象
this.cmd.Connection = this.conn;
//指定命令类型
this.cmd.CommandType = CommandType.StoredProcedure;
//指定存储过程名
this.cmd.CommandText = proc;
//设置参数
if (paramList != null && paramList.Length > 0)
{
foreach (SqlParameter para in paramList)
{
this.cmd.Parameters.Add(para);
}
}
//创建数据适配器
SqlDataAdapter da = new SqlDataAdapter();
//指定数据适配器所用的查询命令
da.SelectCommand = this.cmd;
//创建数据集
DataSet ds = new DataSet();
//填充
da.Fill(ds);
//返回数据集
return ds;
}
/// <summary>
/// 执行无返回值的存储过程
/// 适用于连接模式的增、删、改
/// </summary>
/// <param name="proc"> </param>
/// <param name="paramList"> </param>
public void ExcuteCommand(string proc, SqlParameter[] paramList)
{
//打开数据库连接
this.Open();
//创建命令对象
this.cmd = new SqlCommand();
//指定连接对象
this.cmd.Connection = this.conn;
//指定命令类型
this.cmd.CommandType = CommandType.StoredProcedure;
//指定存储过程名
this.cmd.CommandText = proc;
//设置参数
if (paramList != null && paramList.Length > 0)
{
foreach (SqlParameter para in paramList)
{
this.cmd.Parameters.Add(para);
}
}
//执行存储过程
this.cmd.ExecuteNonQuery();
//关闭数据库连接
this.Close();
}
}
}