从C#中的存储过程获取组合框的信息 [英] Get information to combobox from store procedure in C#
本文介绍了从C#中的存储过程获取组合框的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在C#窗口窗体中显示来自商店的员工姓名程序,并访问组合框中的员工ID。我编写了存储过程以获取员工信息
I want to display the Employee Name from Store procedure in C# window form and to access the Employee ID in combobox. I wrote the Store Procedure to get the Employee Info
Create Procedure Get_Employee_Info
AS
Declare @Error Int
SELECT Employee_Id
, EmployeeName
FROM Employee
SELECT @Error = @@error
RETURN @Error
我尝试了什么:
当我运行EXEC Get_Employee时,我得到了信息,我不知道如何向combobox显示数据。请指导我。
What I have tried:
When I run the EXEC Get_Employee, I get the information and I don't know how to display the data to combobox.Please guide me.
推荐答案
要做到这一点你就行了d将数据作为DataTable绑定到控件,然后设置DisplayMember& ValueMember属性,下面应该有所帮助;
To do this you would bind the data as a DataTable to the control and then set the DisplayMember & ValueMember properties, the below should assist;
DataTable dtEmployee;
using(SqlConnection connDb = new SqlConnection(ConnectionString))
{
conn.Open();
using(SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Get_Emplyee_Info";
using(SqlDataAdapter adap = new SqlDataAdapter(cmd))
{
adap.Fill(dtEmployee);
}
}
conn.Close();
}
comboBox1.DataSource = dtEmployee;
comboBox1.DisplayMember = "EmployeeName";
comboBox1.ValueMember = "Employee_Id";
// complete
亲切的问候
Kind Regards
你在数据库中有商店程序Get_employee_Info,
现在
首先,你需要从C#中获取这个存储过程的数据。
第二个,你需要通过设置将数据表绑定到组合框Datasource,DataMember和ValueMamber。
步骤1:从存储过程中将数据导入C#:
You have Store procedure "Get_employee_Info" in Database,
Now
First, you need to get the data from this stored procedure in C#.
Second, you need to Bind the combo box with the data table by setting "Datasource","DataMember" and "ValueMamber".
Steps 1: Get the data into C# from Store procedure:
Copy from this link:
https://blogs.msmvps.com/deborahk/dal-retrieve-a-datatable-using-a-stored-procedure/
Public DataTable ExecuteDataTable(string storedProcedureName,
params SqlParameter[] arrParam)
{
DataTable dt = new DataTable();
// Open the connection
using (SqlConnection cnn = new SqlConnection(
"Data Source=.\sqlexpress;Initial Catalog=AcmeRentals;
Integrated Security=True"))
{
cnn.Open();
// Define the command
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = cnn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = storedProcedureName;
// Handle the parameters
if (arrParam != null)
{
foreach (SqlParameter param in arrParam)
cmd.Parameters.Add(param);
}
// Define the data adapter and fill the dataset
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}
}
}
return dt;
}
步骤2:将我们从存储过程获得的数据表绑定到组合框。
Steps 2: Bind the data table which we get from Stored procedure to combo box.
// Get the data from SP.
DataTable empployeeTable = Dac.ExecuteDataTable("Get_Employee_Info", null);
// bind employee data table into combo box.
comboBoxEmployee.DataSource = empployeeTable;
comboBoxEmployee.DisplayMember = "EmployeeName";
comboBoxEmployee.ValueMember = "Employee_Id";
这篇关于从C#中的存储过程获取组合框的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文