如何检索数据集 [英] How do I retrieve a dataset
本文介绍了如何检索数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个存储过程,它接受2个参数并进行一些操作并返回数据集,如何将两个参数传递给存储过程并检索数据集,我已经完成了一些代码但不知道它是对的还是错误,请帮忙,在此先感谢:)
I have a stored procedure which takes 2 parameters and do some manipulations and returns a dataset, How do I pass the two parameters to the stored procedure and retrieve the dataset , I have done some code but dont know whether it is right or wrong, please help, thanks in advance :)
public static DataSet getDataSet(DateTime todayDate, DateTime previousDate)
{
string connString = "my connection string";
SqlConnection con = new SqlConnection(connString);
con.Open();
SqlCommand cmd = new SqlCommand("cst_sp_vls_FetchCVUEData", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@startDate", SqlDbType.DateTime).Value =todayDate;
cmd.Parameters.Add("@endDate", SqlDbType.DateTime).Value =previousDate;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
推荐答案
public DataSet getDataSet(DateTime todayDate,DateTime previousDate){
string connString =我的连接字符串;
SqlConnection con = new SqlConnection(connString);
con.Open();
DataSet dataSet = new DataSet();
SqlParameter [] paramArray = new SqlParameter [2];
尝试
{
paramArray [0] =新的SqlParameter(startDate,SqlDbType.DateTime,5);
paramArray [0] .Value = todayDate;
paramArray [1] = new SqlParameter(endDate,SqlDbType.DateTime,5);
paramArray [1] .Value = previousDate;
dataSet = object.RetriveData(cst_sp_vls_FetchCVUEData ,paramArray);
}
catch
{
throw;
}
返回数据集;
}
//这是RetriveData方法..(你可以在这个类或另一个类中添加它并制作itz对象并调用该方法)
public DataSet RetriveData(string SpName,SqlParameter [] OraParams)
{
DataSet dataSet = new DataSet();
尝试使用(SqlCommand sqlCommand = new SqlCommand(SpName,connectionString here))
$
$
sqlCommand.CommandType = CommandType.StoredProcedure;
foreach(SqlParameter) SqlParams中的sqlParam)
{
sqlCommand .Parameters.Add(sqlParam);
}
SqlDataAdapter SqlDataAdr = new SqlDataAdapter(sqlCommand );
SqlDataAdr.Fill(dataSet);
}
}
catch(exception ex)
{
throw ex;
}
终于
{
}
返回dataSet;
}
public DataSet getDataSet(DateTime todayDate, DateTime previousDate) {
string connString = "my connection string";
SqlConnection con = new SqlConnection(connString);
con.Open();
DataSet dataSet = new DataSet();
SqlParameter[] paramArray = new SqlParameter[2];
try
{
paramArray[0] = new SqlParameter("startDate", SqlDbType.DateTime, 5);
paramArray[0].Value = todayDate;
paramArray[1] = new SqlParameter(endDate, SqlDbType.DateTime, 5);
paramArray[1].Value = previousDate ;
dataSet = object.RetriveData("cst_sp_vls_FetchCVUEData", paramArray);
}
catch
{
throw;
}
return dataSet;
}
//this is RetriveData method.. (you can add it in this class or another class and making itz object and call that method)
public DataSet RetriveData(string SpName, SqlParameter[] OraParams)
{
DataSet dataSet = new DataSet();
try
{
using (SqlCommand sqlCommand = new SqlCommand(SpName,connectionString here))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter sqlParam in SqlParams)
{
sqlCommand .Parameters.Add(sqlParam );
}
SqlDataAdapter SqlDataAdr = new SqlDataAdapter(sqlCommand);
SqlDataAdr.Fill(dataSet);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
return dataSet;
}
这篇关于如何检索数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文