0

我有一个具有 3 个组合框的 Windows 应用程序。用户名,准备者,授权者。我想要用户名中所有员工的姓名,而在由我准备和授权的情况下,我想要选择的名称,但来自同一个表。所以我在员工表中分配了一个整数值的角色并触发了一个 stroed 过程。我无法填充组合框

SQL:

emp table:
create table emp1
(
    employee_id int constraint pk_employee_id_employee primary key not null,
    un_id varchar(10) constraint uk_un_id_employee unique not null,
    fname varchar(20) not null,
    lname varchar(20) not null,
    roles int not null
)

stored procedure:
alter proc rolecombo
(
    @roles int  
)
as begin
select * from emp1 where roles<@roles
end

C#代码:

 private void Form1_Load(object sender, EventArgs e)
        {
            con.Open();
            adp = new SqlDataAdapter(cmd);
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "rolecombo";
            cmd.Parameters.AddWithValue("@roles",comboBox3.SelectedValue);
            adp.Fill(dsautho, "emp1");
            comboBox3.DataSource = dsautho.Tables["emp1"];
            comboBox3.DisplayMember = "fname";
            comboBox3.ValueMember = "employee_id";
            comboBox3.SelectedIndex = -1;
            con.Close();
}
4

1 回答 1

1

试试这个示例代码:

public static DataSet DownDataBind()
{
    try
    {
        SqlConnection conn = new SqlConnection("Data Source=S1B01689;Initial Catalog=CosmosDB;User Id=sa;Password=Nttdata123");
        conn.Open();

        SqlDataAdapter adapter = new SqlDataAdapter("SELECT id,categName from CM_Categories",conn);
        DataSet ds = new DataSet();

        adapter.Fill(ds);

        adapter.Dispose();

        conn.Close();

        return ds;
    }
    catch (Exception exp)
    {
        string s = exp.Message.ToString();
        return null;
    }
}

private void Form1_Load(object sender, EventArgs e)
{
    try
    {
        DataSet ds = DownDataBind();
        comboBox1.DataSource = ds.Tables[0];// use Tables[0] instead of Table Name
        comboBox1.ValueMember = "Id";
        comboBox1.DisplayMember = "CategName";
        comboBox1.SelectedIndex = -1;         
    }
    catch (Exception exp)
    {
        MessageBox.Show(exp.Message);
    }
}
于 2013-05-14T09:35:35.247 回答