求一个dataset的第三层类方法
求一个dataset的第三层类方法
要全部的
包括try catch finally的应用
conn的open 和close 等等调用方法
整个一个三层
呵呵谢谢大家
我自己用的是dataread的三层类方法,,不想用dataread了所以想求一个 dataset的三层
各们帮帮忙啊 嘎嘎
[解决办法]
using System;
using System.Data;
using System.Data.SqlClient;
namespace WishDemo.Base
{
/// <summary>
/// 数据服务层的基类
/// </summary>
public abstract class DbObject
{
//存储SQL连接
protected SqlConnection cn;
protected SqlCommand cmd;
//存储连接字符串
private string connectionstring;
/// <summary>
/// 提供连接字符串并实例化基于此字符串的新的连接
/// </summary>
/// <param name= "newConnectionstring "> 连接数据库的连接字符串 </param>
public DbObject( string newConnectionstring )
{
connectionstring = newConnectionstring;
cn = new SqlConnection( connectionstring );
}
/// <summary>
/// 返回只读连接字符串
/// </summary>
protected string ConnectionString
{
get
{
return connectionstring;
}
}
/// <summary>
/// Private routine allowed only by this base class, it automates the task
/// of building a SqlCommand object designed to obtain a return value from
/// the stored procedure.
/// </summary>
/// <param name= "storedProcName "> Name of the stored procedure in the DB, eg. sp_DoTask </param>
/// <param name= "parameters "> Array of IDataParameter objects containing parameters to the stored proc </param>
/// <returns> Newly instantiated SqlCommand instance </returns>
private SqlCommand BuildIntCommand(string storedProcName, IDataParameter [] parameters)
{
SqlCommand command = BuildQueryCommand( storedProcName, parameters );
command.Parameters.Add( new SqlParameter ( "ReturnValue ",
SqlDbType.Int,
4, /* Size */
ParameterDirection.ReturnValue,
false, /* is nullable */
0, /* byte precision */
0, /* byte scale */
string.Empty,
DataRowVersion.Default,
null ));
return command;
}
/// <summary>
/// Builds a SqlCommand designed to return a SqlDataReader, and not
/// an actual integer value.
/// </summary>
/// <param name= "storedProcName "> Name of the stored procedure </param>
/// <param name= "parameters "> Array of IDataParameter objects </param>
/// <returns> </returns>
private SqlCommand BuildQueryCommand(string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = new SqlCommand( storedProcName, cn );
command.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter parameter in parameters)
{
command.Parameters.Add( parameter );
}
return command;
}
/// <summary>
/// Runs a stored procedure, can only be called by those classes deriving
/// from this base. It returns an integer indicating the return value of the
/// stored procedure, and also returns the value of the RowsAffected aspect
/// of the stored procedure that is returned by the ExecuteNonQuery method.
/// </summary>
/// <param name= "storedProcName "> Name of the stored procedure </param>
/// <param name= "parameters "> Array of IDataParameter objects </param>
/// <param name= "rowsAffected "> Number of rows affected by the stored procedure. </param>
/// <returns> An integer indicating return value of the stored procedure </returns>
protected int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected )
{
int result;
cn.Open();
SqlCommand command = BuildIntCommand( storedProcName, parameters );
rowsAffected = command.ExecuteNonQuery();
result = (int)command.Parameters[ "ReturnValue "].Value;
cn.Close();
return result;
}
/// <summary>
/// Creates a DataSet by running the stored procedure and placing the results
/// of the query/proc into the given tablename.
/// </summary>
/// <param name= "storedProcName "> </param>
/// <param name= "parameters "> </param>
/// <param name= "tableName "> </param>
/// <returns> </returns>
protected DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName )
{
DataSet dataSet = new DataSet();
cn.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = BuildQueryCommand( storedProcName, parameters );
sqlDA.Fill( dataSet, tableName );
cn.Close();
return dataSet;
}
/// <summary>
/// Takes an -existing- dataset and fills the given table name with the results
/// of the stored procedure.
/// </summary>
/// <param name= "storedProcName "> </param>
/// <param name= "parameters "> </param>
/// <param name= "dataSet "> </param>
/// <param name= "tableName "> </param>
/// <returns> </returns>
protected void RunProcedure(string storedProcName, IDataParameter[] parameters, DataSet dataSet, string tableName )
{
cn.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = BuildIntCommand( storedProcName, parameters );
sqlDA.Fill( dataSet, tableName );
cn.Close();
}
}
}
[解决办法]
在你的基础上修改,大致这样
public DataTable GetDataTable(string sqlstr,string conn)
{
SalCommand command = new SalCommand(sqlstr, conn);
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
adapter.Fill(ds, "aaa ");
return ds.Tables[0];
}