如何获得数据表作为存储程序的结果 [英] How to get datatable as a result of stored procedure
本文介绍了如何获得数据表作为存储程序的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是我的存储过程。
ALTER PROCEDURE SP_GetModels
(
@CategoryID BIGINT
)
AS
BEGIN
选择ModelID,MODELNAME从模型,其中类别id = @类别ID
端
和我打电话的代码存储过程中落后于
公开的SqlConnection康恩;
公共SqlDataReader的GetModels()
{
DataTable的DT =新的DataTable();
公共无效的DbConnection()
{
康恩=新的SqlConnection(ConfigurationManager.ConnectionStrings [SampleCs]的ConnectionString);
conn.Open();
}
的DbConnection();
的SqlCommand CMD =新的SqlCommand(SP_GetModels,康恩);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(@类别ID,SqlDbType.BigInt,10).value的类别id =;
// SqlDataAdapter的madap =新SqlDataAdapter的(CMD,康涅狄格州);
SqlDataReader的dreader = cmd.ExecuteReader();
//madap.Fill(dt);
返回dreader;
}
我有一个DropDownList我所要绑定包含MODELNAME DataReader对象。
如何设置数据源为DropDownList作为DataReader的
解决方案
私人无效PopDataBaseName ()
{
试
{
的SqlCommand CMD =新的SqlCommand(sp_generate_reportCON);
cmd.Parameters.Add(@ TABLE_NAME,SqlDbType.VarChar,100).value的= TextBox1.Text;
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter的ADP =新SqlDataAdapter的(CMD);
的DataSet DS =新的DataSet();
adp.Fill(DS);
}
赶上(异常前)
{
}
}
Following is my stored procedure.
ALTER PROCEDURE SP_GetModels
(
@CategoryID bigint
)
AS
BEGIN
Select ModelID,ModelName From Model where CategoryID=@CategoryID
END
and i am calling stored procedure in code behind as
public SqlConnection conn;
public SqlDataReader GetModels()
{
DataTable dt = new DataTable();
public void DbConnection()
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SampleCs"].ConnectionString);
conn.Open();
}
DbConnection();
SqlCommand cmd = new SqlCommand("SP_GetModels", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CategoryID", SqlDbType.BigInt, 10).Value = CategoryID;
// SqlDataAdapter madap = new SqlDataAdapter(cmd, conn);
SqlDataReader dreader= cmd.ExecuteReader();
//madap.Fill(dt);
return dreader;
}
I have a dropdownlist to which i have to bind datareader object which contain modelname. how can i set datasource to dropdownlist as datareader
解决方案
private void PopDataBaseName()
{
try
{
SqlCommand cmd = new SqlCommand("sp_generate_report", con);
cmd.Parameters.Add("@TABLE_NAME", SqlDbType.VarChar,100).Value = TextBox1.Text;
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
}
catch (Exception ex)
{
}
}
这篇关于如何获得数据表作为存储程序的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文