加载组合框
我创建OS的应用程序,在那里我可以添加一个客户的名字,姓氏,电子邮件,日期,服务类型(PC修复),技师PC品牌,PC类型,类型,以及电脑的问题。我能够使用phpMyAdmin将数据插入MySQL数据库。加载组合框
但是,我一直卡在这一部分。我正在尝试查看刚刚创建的服务订单。我想用客户的姓氏加载组合框,一旦我点击客户的名字,它就会填充上面提到的所有字段以及它被插入到数据库中的服务编号。我在加载组合框和texfields时遇到问题。
任何想法感谢!如果组合框是一个坏主意,并有更好的方法,请让我知道!我试过这段代码,但SQLDataAdapter不适合我。我无法找到一个我可以联系的例子。
private void cbViewServices_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbViewServices.SelectedIndex >- 1)
{
string lastName = cbViewServices.SelectedValue.ToString();
MySqlConnection conn = new MySqlConnection("server=localhost;uid=******;password=**********;database=dboserviceinfo;");
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select distinct LastName from tserviceinfo where LastName='" + lastName + "'", conn);
DataSet ds = new DataSet();
da.Fill(ds); conn.Close();
}
}
我不推荐使用“姓”作为参数来加载你的细节,因为这一领域最有可能不是唯一的。除非您的计划中出现这种情况。
此示例执行下列操作:
- 负载的客户ID(或你的情况姓氏)的组合框。
- 处理组合框的更改事件,并将其作为参数传递到 方法,将用它来装载细节。
- 使用传递的参数加载客户详细信息。
一对夫妇的准则:
- 在“使用”声明附上支配对象,所以这将是 妥善处置。
- 请勿使用字符串连接来创建您的SQL语句。使用 SQL参数来代替,这样你就可以避免SQL注入,并使代码更清晰。
-
查看MySQL .NET连接器提供程序文档以获取最佳实践。
//Load customer ID to a combobox private void LoadCustomersId() { var connectionString = "connection string goes here"; using (var connection = new MySqlConnection(connectionString)) { connection.Open(); var query = "SELECT Id FROM Customers"; using (var command = new MySqlCommand(query, connection)) { using (var reader = command.ExecuteReader()) { //Iterate through the rows and add it to the combobox's items while (reader.Read()) { CustomerIdComboBox.Items.Add(reader.GetString("Id")); } } } } } //Load customer details using the ID private void LoadCustomerDetailsById(int id) { var connectionString = "connection string goes here"; using (var connection = new MySqlConnection(connectionString)) { connection.Open(); var query = "SELECT Id, Firstname, Lastname FROM Customer WHERE Id = @customerId"; using (var command = new MySqlCommand(query, connection)) { //Always use SQL parameters to avoid SQL injection and it automatically escapes characters command.Parameters.AddWithValue("@customerId", id); using (var reader = command.ExecuteReader()) { //No customer found by supplied ID if (!reader.HasRows) return; CustomerIdTextBox.Text = reader.GetInt32("Id").ToString(); FirstnameTextBox.Text = reader.GetString("Firstname"); LastnameTextBox.Text = reader.GetString("Lastname"); } } } } //Pass the selected ID in the combobox to the customer details loader method private void CustomerIdComboBox_SelectedIndexChanged(object s, EventArgs e) { var customerId = Convert.ToInt32(CustomerIdComboBox.Text); LoadCustomerDetailsById(customerId); }
我不能完全肯定,如果这是你要找的,但大多数的准则仍然适用。
希望这会有所帮助!
尝试这样的事情将数据绑定到组合框:
public void ListCat()
{
DataTable linkcat = new DataTable("linkcat");
using (SqlConnection sqlConn = new SqlConnection(@"Connection stuff;"))
{
using (SqlDataAdapter da = new SqlDataAdapter("SELECT LastName FROM list WHERE LastName <> 'NULL'", sqlConn))
{
da.Fill(linkcat);
}
}
foreach (DataRow da in linkcat.Rows)
{
comboBox1.Items.Add(da[0].ToString());
}
}
从my own question服用。
SqlDataAdapter的使用与SQL Server而不是MySQL的沟通。
尝试以下操作:
MySqlDataAdapter da = new MySqlDataAdapter("select distinct LastName from tserviceinfo where LastName='" + lastName + "'", conn);
//USING
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
namespace YourNamespace
{
//Initialization
string connetionString = null;
SqlConnection cnn;
SqlCommand cmdDataBase;
SqlDataReader reader;
DataTable dt;
public frmName()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
FillComboNameOfCombo();
}
void FillcmbNameOfCombo()
{
string sqlQuery = "SELECT * FROM DATABASENAME.[dbo].[TABLENAME];";
connetionString = "Data Source=YourPathToServer;Initial Catalog=DATABASE_NAME;User ID=id;Password=pass";
cnn = new SqlConnection(connetionString);
cmdDataBase = new SqlCommand(sqlQuery, cnn);
try {
cnn.Open();
reader = cmdDataBase.ExecuteReader();
dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("COLUMN_NAME", typeof(string));
dt.Load(reader);
cnn.Close();
cmbGender.DataSource = dt;
cmbGender.ValueMember = "ID";
cmbGender.DisplayMember = "COLUMN_NAME";
dt = null;
cnn = null;
cmdDataBase = null;
connetionString = null;
reader = null;
}
catch (Exception ex) {
MessageBox.Show("Can not open connection ! " + ex.ToString());
}
}
}
我知道这不是最高效的代码,但它很容易阅读。 此代码在加载表单的那一刻将显示值(不是ID值)放到组合框中。 并且应该准备数据以通过它的ID发送到数据库。确保你的数据库被组织成接受ID到字段中。 – Nikola 2016-09-29 16:58:10
我们也可以用while循环。在SQLDatareader之后完成数据库连接时,我们可以使用while循环。
“userRead” 是SQLDATA读者
while (userRead.Read())
{
cboxReportNo.Items.Add(userRead[1].ToString());
}
数据集绑定在组合框数据源
this.comboBox1.DataSource = ds;
this.comboBox1.DisplayMember = "LastName";
this.comboBox1.ValueMember = "Id";
this.comboBox1.SelectedIndex = -1;
this.comboBox1.AutoCompleteMode = AutoCompleteMode.Append;
this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
您不妨看看使用你的连接语句,数据适配器,数据集,等等。另外请阅读参数化SQL,否则你会打开SQL注入攻击 – Manatherin 2012-07-31 01:06:35
你把你的控件绑定到ds吗?我相信在更改数据后,您需要手动更新控件或重新绑定它们以显示结果 – AaA 2012-07-31 02:42:44