![](/img/trans.png)
[英]Populating C# datatable with refcursor values from an Oracle Stored Procedure
[英]C#: Populating combobox from stored procedure only displays first item
我試圖通過執行存儲過程在初始化時填充組合框。 當我在我的數據庫上運行該過程時,會出現 3 行:
但是一旦我在我的代碼中執行了這個過程:
public partial class AddInstitutionStart : Window
{
public AddInstitutionStart()
{
InitializeComponent();
ComboBoxInstitutionCategory.Items.Add(FillCombo.fillInstCategory());
}
}
public static class FillCombo
{
public static string fillInstCategory()
{
string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
SqlConnection Connect = null;
SqlCommand Command;
using (Connect = new SqlConnection(connstr))
{
Connect.Open();
using (Command = new SqlCommand("[dbo].[spMetadataTb_GetCategory]", Connect))
{
Command.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = Command.ExecuteReader();
while (dr.Read())
{
string categoryInst = dr.GetString(0);
return categoryInst;
}
}
Command.Dispose();
Connect.Close();
return "-2";
}
}
}
只顯示第一行:
我使用相同的邏輯來填充另一個組合框(不是在初始化時),它運行良好並顯示多個項目。 知道我在這里可能做錯了什么嗎?
像這樣的東西......也只處理你的讀者。
public static List<string> fillInstCategory()
{
string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
SqlConnection Connect = null;
SqlCommand Command;
List<string> categories = new List<string>();
using (Connect = new SqlConnection(connstr))
{
Connect.Open();
using (Command = new SqlCommand("[dbo].[spMetadataTb_GetCategory]", Connect))
{
Command.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = Command.ExecuteReader();
while (dr.Read())
{
string categoryInst = dr.GetString(0);
categories.Add(categoryInst);
}
}
Command.Dispose();
Connect.Close();
return categories;
}
}
大量可能的解決方案,但請忘記靜態!
從另一個答案返回一個列表,像這樣使用它:
public partial class AddInstitutionStart : Window
{
public AddInstitutionStart()
{
InitializeComponent();
foreach(string cat in new FillCombo().fillInstCategory())
ComboBoxInstitutionCategory.Items.Add(cat);
}
}
或者,如果AddRange
存在:
public partial class AddInstitutionStart : Window
{
public AddInstitutionStart()
{
InitializeComponent();
ComboBoxInstitutionCategory.Items.AddRange(new FillCombo().fillInstCategory());
}
}
此解決方案首先填充fillInstCategory()
的列表,然后通過 foreach 或 addrange 填充您的組合框。
使其成為可枚舉函數(使用與上述相同):
您的代碼中只有 2 個更改,第一個:
public IEnumerable<string> GetInstCategory()
{
...
}
然后用yield擴展返回字:
while (dr.Read())
yield return dr.GetString(0);
這會在讀取每個數據后立即填充組合框(不是一次,當所有數據都像解決方案 1 一樣讀取時),因為yield會導致在您的Items.Add
和While(dr.Read())
代碼部分之間來回跳躍。
注入一個方法(或 lambda 表達式):
您的代碼中只有 2 個更改,第一個:
public void GetInstCategory(Action<string> Injection)
{
...
}
然后:
while (dr.Read())
Injection(dr.GetString(0));
像這樣使用它:
public partial class AddInstitutionStart : Window
{
public AddInstitutionStart()
{
InitializeComponent();
new FillCombo().fillInstCategory(ComboBoxInstitutionCategory.Items.Add)) //note there is no "(" and ")" after Add, so the addition doesnt happen, just the function was passed to "fillInstCategory" to use it whenever necessary.
}
}
這以與解決方案 2 相同的方式填充組合框:讀取 1 個數據,然后將 1 添加到組合框,然后再次讀取...
我希望你能從中學到一些東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.