因此,通常当您创建一个组合框时,您将成为放置选择值的人,但我希望我的组合框中的数据将从我的数据库 mysql 中选择。我要怎么做?
我被困在从我的 sql 中选择数据到组合框中!
到目前为止,这是我的代码!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MySqlConnection connection = null;
string hostname = "localhost";
string database = "aparece_hoteldb";
string username = "root";
string password = "";
connection = new MySqlConnection("host=" + hostname +
";database=" + database +
";username=" + username +
";password=" + password + ";");
string table = "reservations";
string query = "SELECT * FROM " + table;
connection.Open();
MySqlDataAdapter da_res = null;
DataSet ds_res = null;
ds_res = new DataSet();
da_res = new MySqlDataAdapter(query, connection);
da_res.Fill(ds_res, table);
dataGridView2.DataSource = ds_res.Tables[table];
}
private void label2_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}