如何通过存储过程获取数据表

use*_*348 5 c# sql-server asp.net data-binding

以下是我的存储过程.

ALTER PROCEDURE SP_GetModels 
(
    @CategoryID bigint
)
AS
BEGIN
    Select ModelID,ModelName From Model where CategoryID=@CategoryID
END
Run Code Online (Sandbox Code Playgroud)

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

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;
            }
Run Code Online (Sandbox Code Playgroud)

我有一个下拉列表,我必须绑定包含modelname的datareader对象.如何将datasource设置为dropdownlist作为datareader

小智 10

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)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)


mar*_*c_s 2

您应该能够直接将 SqlDataReader 绑定到下拉列表,如下所示:

MyDropDownList.DataSource = GetModels();
MyDropDownList.DataTextField = "ModelName";
MyDropDownList.DataValueField = "ModelID";
Run Code Online (Sandbox Code Playgroud)

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

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

public class Model
{
  public int ModelID { get; set; }
  public string ModelName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在你的 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;
}
Run Code Online (Sandbox Code Playgroud)

马克