2009-08-08 66 views
5

以下是我的存儲過程。如何獲取作爲存儲過程結果的數據表

ALTER PROCEDURE SP_GetModels 
(
    @CategoryID bigint 
) 
AS 
BEGIN 
    Select ModelID,ModelName From Model where [email protected] 
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; 
      } 

我有一個下拉列表,而我具有結合其含有MODELNAME DataReader對象存儲過程。 如何設置數據源爲DropDownList作爲DataReader的

回答

2

你應該能夠SqlDataReader對象直接綁定到下拉列表如下:

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

您還需要指定其成員(屬性)是怎麼回事(DataTextField),以及在下拉列表(DataValueField)中選擇條目時將使用哪一個值作爲值。

強烈建議你搶從SqlDataReader的數據在你GetModels()過程中,創建一個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; 
} 

馬克

1

首先,返回時,它確保您有DataReader的自動關閉:

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

然後綁定到一個清單:

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

我不認爲SqlDataReader繼承自IListSource,如果我沒有記錯,只能使用IListSource繼承的類來進行數據綁定。如果你想獲得一個DataTable,你應該使用SqlDataAdapter來執行命令。擴大對馬克的解決方案:

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

這個怎麼樣

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

      } 
      con.Close(); 
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) 
    { 

    } 
} 
+0

怎麼樣在t SQL數據表有旁邊的輸出參數?如何獲得輸出參數呢?可能嗎?樣品? – 2015-07-30 09:07:55

相關問題