c# - MySQL C# 错误;“连接必须有效且打开”
问题描述
您好,我得到了我正在处理的这个应用程序,但我一直在填充一个下拉框。我想在下拉框中从我的数据库中获取我的 SQL 数据,但 SQL 说“连接必须有效且打开”,但我有另一个功能,包括使用相同的代码将文本保存到我的 SQL 数据库,并且工作正常,所以我没有t真的知道什么是错的。
这是我的代码。
private void Form1_Load(object sender, EventArgs e)
{
try {
//This is my connection string i have assigned the database file address path
string MyConnection2 = "datasource=localhost;username=root;database=game4rent";
//This is my insert query in which i am taking input from the user through windows forms
string Query = "select Klantnummer,voornaam from game4rent.klanten";
//This is MySqlConnection here i have created the object and pass my connection string.
MySqlConnection conn = new MySqlConnection(MyConnection2);
//This is command class which will handle the query and connection object.
MySqlCommand sc = new MySqlCommand(Query, conn);
MySqlDataReader MyReader2;
MyReader2 = sc.ExecuteReader();
conn.Open();
DataTable dt = new DataTable();
dt.Columns.Add("Klantnummer", typeof(string));
dt.Columns.Add("voornaam", typeof(string));
dt.Load(MyReader2);
cbKlantenNummers.ValueMember = "Klantnummer";
cbKlantenNummers.DisplayMember = "Klantnummer,voornaam";
cbKlantenNummers.DataSource = dt;
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这是另一个有效的代码。
private void cmdOpslaanKlanten_Click(object sender, EventArgs e)
{
try
{
//This is my connection string i have assigned the database file address path
string MyConnection2 = "datasource=localhost;username=root;password=";
//This is my insert query in which i am taking input from the user through windows forms
string Query = "insert into game4rent.klanten(voornaam,achternaam,straat,huisnummer,woonplaats) values('" + this.txtVoornaam.Text + "','" + this.txtAchternaam.Text + "','" + this.txtStraat.Text + "','" + this.txtHuisnummer.Text + "','" + this.txtWoonplaats.Text + "');";
//This is MySqlConnection here i have created the object and pass my connection string.
MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
//This is command class which will handle the query and connection object.
MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
MySqlDataReader MyReader2;
MyConn2.Open();
MyReader2 = MyCommand2.ExecuteReader(); // Here our query will be executed and data saved into the database.
MessageBox.Show("Save Data");
while (MyReader2.Read())
{
}
MyConn2.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
解决方案
您正在尝试在打开连接之前执行查询:
MyReader2 = sc.ExecuteReader();
conn.Open();
如错误所述,必须在执行查询之前打开连接:
conn.Open();
MyReader2 = sc.ExecuteReader();
推荐阅读
- android - 如何为每个第 n 个字符添加两个点?
- javascript - 如何在 Vue3 + Parcel 2 中粘贴(插入)本地图像?
- python - 对 FormRecognizer 的请求停留在 notStarted 状态
- reactjs - 在 React Router 中渲染组件不起作用
- java - Java Process Builder - 如何在 Diff 之前对 2 个文件进行排序
- python - 使用 AWS lambda、Zeep 和 IRESS 创建 REST API
- java - 使用点“。”时,Java 无法解析日期。而不是破折号“-”
- android - 在同一张地图上显示多个注册用户的位置(Android Studio、Firebase、Kotlin)
- python - 我们可以在 Python 应用程序的单个查询中查询来自不同数据库服务器的表吗?前任。oracle 和 postgres 之间的连接
- mysql - 用 SQL 获取两点之间的距离