MySql错误Windows应用程序中的System.InvalidOperationException

编程入门 行业动态 更新时间:2024-10-23 01:45:48
MySql错误Windows应用程序中的System.InvalidOperationException-c#(MySql error System.InvalidOperationException in windows application-c#)

我收到错误:

MySql.Data.CF.dll中发生未处理的类型为“System.InvalidOperationException”的异常

在我的Windows应用程序中。

以下是我的代码。

In app.config: inside connectionStrings tag add name="ConnectionString" connectionString="server=localhost;database=my_db;user=root;port=3306; password=mypwd; In LoginForms.cs using System.Configuration; using MySql.Data.MySqlClient; namespace MySoftware { public partial class Login : Form { MySqlConnection conn; public static int valid = 0; public Login() { InitializeComponent(); } private void btnLogin_Click(object sender, EventArgs e) { var connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; conn = new MySqlConnection(connectionString); conn.open(); MySqlCommand cmd = new MySqlCommand(); cmd.CommandText = "Verify_Login"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@uname", textBox1.Text); cmd.Parameters["@uname"].Direction = ParameterDirection.Input; cmd.Parameters.AddWithValue("@pwd", textBox2.Text); cmd.Parameters["@pwd"].Direction = ParameterDirection.Input; cmd.Parameters.AddWithValue("@result", MySqlDbType.Int32); cmd.Parameters["@result"].Direction = ParameterDirection.Output; cmd.ExecuteNonQuery(); // At this line the error is thrown int valid = (int)(cmd.Parameters["@result"].Value); } } } Verify_Login is a stored procedure which is created in MySQL as below. CREATE PROCEDURE `Verify_Login`(in uname varchar(20),in pwd varchar(20),out result bool) BEGIN select count(*) into result from Login where uname=uname and password=pwd; END Could anyone please help me with this?

I am getting the error:

An unhandled exception of type 'System.InvalidOperationException' occurred in MySql.Data.CF.dll

in my windows application program.

Below is my code.

In app.config: inside connectionStrings tag add name="ConnectionString" connectionString="server=localhost;database=my_db;user=root;port=3306; password=mypwd; In LoginForms.cs using System.Configuration; using MySql.Data.MySqlClient; namespace MySoftware { public partial class Login : Form { MySqlConnection conn; public static int valid = 0; public Login() { InitializeComponent(); } private void btnLogin_Click(object sender, EventArgs e) { var connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; conn = new MySqlConnection(connectionString); conn.open(); MySqlCommand cmd = new MySqlCommand(); cmd.CommandText = "Verify_Login"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@uname", textBox1.Text); cmd.Parameters["@uname"].Direction = ParameterDirection.Input; cmd.Parameters.AddWithValue("@pwd", textBox2.Text); cmd.Parameters["@pwd"].Direction = ParameterDirection.Input; cmd.Parameters.AddWithValue("@result", MySqlDbType.Int32); cmd.Parameters["@result"].Direction = ParameterDirection.Output; cmd.ExecuteNonQuery(); // At this line the error is thrown int valid = (int)(cmd.Parameters["@result"].Value); } } } Verify_Login is a stored procedure which is created in MySQL as below. CREATE PROCEDURE `Verify_Login`(in uname varchar(20),in pwd varchar(20),out result bool) BEGIN select count(*) into result from Login where uname=uname and password=pwd; END Could anyone please help me with this?

最满意答案

您的代码失败,因为尝试执行命令并且此命令未绑定到打开的连接。 你有很多方法来绑定连接

MySqlCommand cmd = conn.CreateCommand();

要么

MySqlCommand cmd = new MySqlCommand(sqlText, conn);

要么

MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn;

当然,为您的环境使用适当的连接器的建议仍然有效....

你的代码重构了一下

private void btnLogin_Click(object sender, EventArgs e) { var connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using(MySqlConnection conn = new MySqlConnection(connectionString)) using(MySqlCommand cmd = new conn.CreateCommand()) { conn.open(); cmd.CommandText = "Verify_Login"; cmd.CommandType = CommandType.StoredProcedure; ..... cmd.ExecuteNonQuery(); int valid = (int)(cmd.Parameters["@result"].Value); } }

Your code fails because is trying to execute a command and this command is not bound to an open connection. You have a lot of ways to bind the connection

MySqlCommand cmd = conn.CreateCommand();

or

MySqlCommand cmd = new MySqlCommand(sqlText, conn);

or

MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn;

Of course the suggestion to use the appropriate Connector for your environment still stands....

Your code refactored a bit

private void btnLogin_Click(object sender, EventArgs e) { var connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using(MySqlConnection conn = new MySqlConnection(connectionString)) using(MySqlCommand cmd = new conn.CreateCommand()) { conn.open(); cmd.CommandText = "Verify_Login"; cmd.CommandType = CommandType.StoredProcedure; ..... cmd.ExecuteNonQuery(); int valid = (int)(cmd.Parameters["@result"].Value); } }

更多推荐

本文发布于:2023-08-04 06:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1413131.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   错误   MySql   Windows   InvalidOperationException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!