简体   繁体   English

选择组合框时如何获取数据 windows Form c#

[英]How to get data when combo box is selected windows Form c#

private void namecombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (SqlConnection sqlconn = new SqlConnection(@"Data Source=DESKTOP-IIBSL6N;Initial Catalog=sales_management;Integrated Security=True"))
            {
                sqlconn.Open();
                SqlCommand sqlcmd = new SqlCommand("SELECT * FROM Customer WHERE Name = '" + this.namecombo.SelectedItem.ToString() + "' ", sqlconn);
                sqlcmd.ExecuteNonQuery();
                DataTable dtbl = new DataTable();
                SqlDataAdapter adapter = new SqlDataAdapter(sqlcmd);
                adapter.Fill(dtbl);
                foreach(DataRow dr in dtbl.Rows)
                {
                    accountnumtxtbox.Text = dr["acount_name"].ToString();
                    phonetxtbox.Text = dr["phone_number"].ToString();
                    officenumtxtbox.Text = dr["office_number"].ToString();
                    addresstxtbox.Text = dr["Address"].ToString();
                }
                sqlconn.Close();
            }
        }

this doesn't work at all whats the problem?这根本不起作用是什么问题?

using (SqlConnection sqlconn = new SqlConnection(@"Data Source=DESKTOP-IIBSL6N;Initial Catalog=sales_management;Integrated Security=True"))   
{  
    sqlconn.Open();  
    SqlCommand sqlcmd = new SqlCommand("SELECT * FROM Customer WHERE Name = N'" + this.customergrid.SelectedRows + "' ", sqlconn);  
    sqlcmd.ExecuteNonQuery();  
    DataTable dtbl = new DataTable();  
    SqlDataAdapter adapter = new SqlDataAdapter(sqlcmd);  
    adapter.Fill(dtbl);  
    foreach (DataRow dr in dtbl.Rows)  
    {  
        accountnumtxtbox.Text = dr["acount_name"].ToString();  
        phonetxtbox.Text = dr["phone_number"].ToString();  
        officenumtxtbox.Text = dr["office_number"].ToString();  
        addresstxtbox.Text = dr["Address"].ToString();  
    }  
    sqlconn.Close();  
}  

You try to do it this way, just adapt it to your needs.您尝试以这种方式进行操作,只需根据您的需要进行调整即可。

string sql = "SELECT * FROM Customers WHERE LastName = @lastName AND FirstName = @firstName";
        UserAccount account = UserAccount.Empty;

        using (SqlCommand cmd = new SqlCommand(sql, sqlConnection))
        {
            SqlParameter _firstName = new SqlParameter("@firstName", SqlDbType.NVarChar);
            SqlParameter _lastName = new SqlParameter("@lastName", SqlDbType.NVarChar);

            _firstName.Value = account.FirstName;
            _lastName.Value = account.LastName;

            cmd.Parameters.Add(_firstName);
            cmd.Parameters.Add(_lastName);

            DataSet dataSet = new DataSet();

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);

            adapter.Fill(dataSet);

            if (dataSet.Tables.Count > 0)
            {
                if (dataSet.Tables[0].Rows.Count > 0)
                {
                    DataRow row = dataSet.Tables[0].Rows[0];

                    //fill your properties with the results
                }
            }

            adapter.Dispose();
            dataSet.Dispose();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在c#中获取先前选择的组合框索引 - how to get the previously selected index of combo box in c# 如何将组合框的选定项获取到 c# 中的字符串变量 - How to get the selected item of a combo box to a string variable in c# 如何从C#中的数据gridview获取选定的行值到组合框 - How to get selected row values to combo box from data gridview in c# 如何从组合框 C# 中的选定值中获取选定索引 - How to get selected index from selected value in combo box C# C#Windows窗体-如何为组合框字符串分配一个双精度值,然后将其显示在文本框中? - C# Windows Form - How do I assign a double value to a combo box string to then be displayed in a text box? 从组合框中获取选定值 C# WPF - Get selected value from combo box in C# WPF 如何从Windows窗体的组合框下显示的列表中删除或隐藏组合框的先前选定值? - How to remove or hide the previously selected value of a combo box from the list that are displayed under the combo box in Windows Form? 如何使用C#Winforms在按钮单击事件中获得组合框选定的值? - How to get combo box selected value in button click event using C# Winforms? 如何在C#中检查组合框中的项目是否被选中? - How to check whether the item in the combo box is selected or not in C#? 如何在C#中设置组合框的选定值 - how to set selected value of combo box in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM