【问题标题】:How to get datatable as a result of stored procedure如何通过存储过程获取数据表
【发布时间】:2009-08-08 14:57:33
【问题描述】:

以下是我的存储过程。

ALTER PROCEDURE SP_GetModels 
(
    @CategoryID bigint
)
AS
BEGIN
    Select ModelID,ModelName From Model where CategoryID=@CategoryID
END

我在后面的代码中调用存储过程

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;
            }

我有一个下拉列表,我必须将包含模型名称的数据读取器对象绑定到该下拉列表。 如何将数据源设置为下拉列表作为数据阅读器

【问题讨论】:

    标签: c# asp.net sql-server data-binding


    【解决方案1】:
    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)
        {
    
        }
    }
    

    【讨论】:

    • 在数据表旁边有来自 t sql 的输出参数怎么样?如何获得输出参数?可能吗?样品?
    【解决方案2】:

    您应该能够像这样直接将 SqlDataReader 绑定到下拉列表:

    MyDropDownList.DataSource = GetModels();
    MyDropDownList.DataTextField = "ModelName";
    MyDropDownList.DataValueField = "ModelID";
    

    您还需要指定要显示哪个成员(属性)(DataTextField),以及在下拉列表中选择条目时将哪个成员用作值(DataValueField)。

    我会强烈建议您在 GetModels() 过程中从 SqlDataReader 获取数据,创建 Model 类的实例,该类将保存您拥有和需要的那些字段,关闭 SqlDataReader,然后将其作为List<Model> 返回并将该列表绑定到下拉列表。比直接绑定 SqlDataReader 好多了!

    public class Model
    {
      public int ModelID { get; set; }
      public string ModelName { get; set; }
    }
    

    在你的 GetModels() 中:

    public List<Model> GetModels()
    {
      List<Model> result = new List<Model>();
    
      using(SqlConnection conn = new SqlConnection(ConfigurationManager.
                                         ConnectionStrings["SampleCs"].ConnectionString))
      {
         using(SqlCommand cmd = new SqlCommand("SP_GetModels", conn))
         {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@CategoryID", SqlDbType.BigInt, 10).Value = CategoryID;
    
            conn.Open();
    
            using(SqlDataReader dreader = cmd.ExecuteReader())
            { 
               while(dreader.Read())
               {
                   Model workItem = new Model() 
                                    { ModelID = dreader.GetInt(0), 
                                      ModelName = dreader.GetString(1) };
                   result.Add(workItem);
               }
               reader.Close();
            }
    
            conn.Close();
        }
      }
      return result;
    }
    

    马克

    【讨论】:

      【解决方案3】:

      首先,确保数据读取器在返回时自动关闭:

       SqlDataReader dreader= cmd.ExecuteReader(CommandBehavior.CloseConnection);
      

      然后绑定到一个列表:

       DropDownList1.DataSource = GetModels();
       DropDownList1.DataValueField = "ModelID";
       DropDownList1.DataTextField = "ModelName";
       DropDownList1.DataBind();
      

      【讨论】:

        【解决方案4】:

        我不认为 SqlDataReader 继承自 IListSource,如果我没记错的话,您只能使用从 IListSource 继承的类进行数据绑定。如果要获取 DataTable,则应使用 SqlDataAdapter 来执行命令。扩展 Marc 的解决方案:

        public void BindData()
        {
            dropDownList1.DataSource = LoadModelData();
            dropDownList1.DataValueField = "ModelID";
            dropDownList1.DataTextField = "ModelName";
            dropDownList1.DataBind();
        }
        public DataTable LoadModelData()
        { 
            DataSet dataset = new DataSet();
            using (conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SampleCs"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("SP_GetModels", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@CategoryID", SqlDbType.BigInt, 10).Value = CategoryID;
                SqlDataAdapter adapter = new SqlDataAdapter(cmd, conn);
                adapter.Fill(dataset);
            }
            return dataset.Tables[0];
        }
        

        【讨论】:

          【解决方案5】:

          这个怎么样

          SqlDataReader dr = cmd.ExecuteReader();
                      while (dr.Read())
                      {
                          DropDownList1.Items.Add(new ListItem(dr["ModelName"].ToString(), dr["ModelID"].ToString()));
          
                      }
                      con.Close();
          

          【讨论】:

            猜你喜欢
            • 2011-10-14
            • 2022-08-24
            • 1970-01-01
            • 1970-01-01
            • 2018-07-25
            • 2011-03-03
            • 2016-10-21
            • 1970-01-01
            • 2017-05-12
            相关资源
            最近更新 更多