为什么 protected SqlDataReader RunProcedure(..)这段还要写Command.CommandType=CommandType.StoredProcedure;

hiweiyi2000 2006-06-23 09:13:56
using System;
using System.Data;
using System.Data.SqlClient;


namespace Wrox.WebModules.Data
{
/// <summary>
/// DbObject 的摘要说明。
/// </summary>
public abstract class DbObject
{
protected SqlConnection Connection;
private string connectionString;

public DbObject(string newConnectionString)
{
connectionString=newConnectionString;
Connection=new SqlConnection(connectionString);

//
// TODO: 在此处添加构造函数逻辑
//
}
protected string ConnectionString
{
get
{
return connectionString;
}
}
private SqlCommand BuildQueryCommand(string storedProcName,IDataParameter[] parameters)
{
SqlCommand command=new SqlCommand(storedProcName,Connection);
command.CommandType=CommandType.StoredProcedure;
foreach(SqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
return command;
}
private SqlCommand BuildIntCommand(string storedProcName,IDataParameter[] parameters)
{
SqlCommand command=BuildQueryCommand(storedProcName,parameters);
command.Parameters.Add(new SqlParameter("ReturnValue",SqlDbType.Int,4,ParameterDirection.ReturnValue,false,0,0,string.Empty,DataRowVersion.Default,null));
return command;
}
protected int RunProcedure(string storedProcName,IDataParameter[] parameters,out int rowsAffected)
{
int result;
Connection.Open();
SqlCommand command=BuildIntCommand(storedProcName,parameters);
rowsAffected=command.ExecuteNonQuery();
result=(int)command.Parameters["ReturnValue"].Value;
Connection.Close();

return result;
}
protected SqlDataReader RunProcedure(string storedProcName,IDataParameter[] parameters)
{
SqlDataReader returnReader;
Connection.Open();
SqlCommand command=BuildQueryCommand(storedProcName,parameters);
command.CommandType=CommandType.StoredProcedure;
returnReader=command.ExecuteReader(CommandBehavior.CloseConnection);
return returnReader;
}
protected DataSet RunProcedure(string storedProcName,IDataParameter[] parameters,string tableName)
{
DataSet dataSet=new DataSet();
Connection.Open();
SqlDataAdapter sqlDA=new SqlDataAdapter();
sqlDA.SelectCommand=BuildQueryCommand(storedProcName,parameters);
sqlDA.Fill(dataSet,tableName);
Connection.Close();
return dataSet;
}
protected void RunProcedure(string storedProcName,IDataParameter[] parameters,DataSet dataSet,string tableName)
{
Connection.Open();
SqlDataAdapter sqlDA=new SqlDataAdapter();
sqlDA.SelectCommand=BuildIntCommand(storedProcName,parameters);
sqlDA.Fill(dataSet,tableName);
Connection.Close();
}


}
}
...全文
344 点赞 收藏 10
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
saucer 2006-06-24
应该不需要的,但上面的编码的质量很差,所以见怪不怪了
回复
amandag 2006-06-24
up
回复
hiweiyi2000 2006-06-24
private SqlCommand BuildIntCommand(string storedProcName,IDataParameter[] parameters)
{
SqlCommand command=BuildQueryCommand(storedProcName,parameters);
command.Parameters.Add(new SqlParameter("ReturnValue",SqlDbType.Int,4,ParameterDirection.ReturnValue,false,0,0,string.Empty,DataRowVersion.Default,null));

这里加的一个ReturnValue是什么?
return command;
}
protected int RunProcedure(string storedProcName,IDataParameter[] parameters,out int rowsAffected)
{
int result;
Connection.Open();
SqlCommand command=BuildIntCommand(storedProcName,parameters);
rowsAffected=command.ExecuteNonQuery();
rowsAffected这样直接得个int数吗?
result=(int)command.Parameters["ReturnValue"].Value;

直接result得个参数是什么?
Connection.Close();

return result;
}
回复
默认情况下,Command.CommandType 的类型不是存储过程,所以这里要写上这句。
回复
bingbingcha 2006-06-24
protected SqlDataReader RunProcedure(string storedProcName,IDataParameter[] parameters)

如果你贴的代码是全的话,这个函数是有问题的.会造成内存泄漏..你重复调用两次看看..会出错..

如果非要返回一个SqlDataReader 的话,需要额外添加方法,释放SqlDataReader 占用的连接资源...

这个类的问题多多..建议参考Microsoft和Yahoo!的SQL类.
回复
hiweiyi2000 2006-06-23
BuildQueryCommand这段不是有了吗?
如下:


private SqlCommand BuildQueryCommand(string storedProcName,IDataParameter[] parameters)
{
SqlCommand command=new SqlCommand(storedProcName,Connection);
command.CommandType=CommandType.StoredProcedure;
foreach(SqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
return command;
}

为什么要写多一次。在下面这段里,

protected SqlDataReader RunProcedure(string storedProcName,IDataParameter[] parameters)
{
SqlDataReader returnReader;
Connection.Open();
SqlCommand command=BuildQueryCommand(storedProcName,parameters);
command.CommandType=CommandType.StoredProcedure;
returnReader=command.ExecuteReader(CommandBehavior.CloseConnection);
return returnReader;
}


回复
hiweiyi2000 2006-06-23
command.CommandType=CommandType.StoredProcedure;
说的是这一句
回复
fxj_wl 2006-06-23
保证代码的完整性、规范性,就像if语句后最好有{},无论{}里有多少条语句
回复
hiweiyi2000 2006-06-23
protected SqlDataReader RunProcedure(string storedProcName,IDataParameter[] parameters)
{
SqlDataReader returnReader;
Connection.Open();
SqlCommand command=BuildQueryCommand(storedProcName,parameters);
command.CommandType=CommandType.StoredProcedure;
returnReader=command.ExecuteReader(CommandBehavior.CloseConnection);
return returnReader;
}


直接执行不就行了,不明白这段为什么要写多一次。
回复
hiweiyi2000 2006-06-23
为什么 protected SqlDataReader RunProcedure(..)这段还要写Command.CommandType=CommandType.StoredProcedure;

BuildQueryCommand这段不是有了吗?
如下:


private SqlCommand BuildQueryCommand(string storedProcName,IDataParameter[] parameters)
{
SqlCommand command=new SqlCommand(storedProcName,Connection);
command.CommandType=CommandType.StoredProcedure;
foreach(SqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
return command;
}

回复
相关推荐
发帖
.NET技术社区
创建于2007-09-28

5.8w+

社区成员

.NET技术交流专区
申请成为版主
帖子事件
创建了帖子
2006-06-23 09:13
社区公告
暂无公告