C# windows application-c中的MySql错误System.InvalidOperationException#
我得到一个错误: “System.InvalidOperationException”类型的未处理异常 发生在MySql.Data.CF.dll中 在我的windows应用程序中 下面是我的代码。C# windows application-c中的MySql错误System.InvalidOperationException#,c#,mysql,windows,cmd,C#,Mysql,Windows,Cmd,我得到一个错误: “System.InvalidOperationException”类型的未处理异常 发生在MySql.Data.CF.dll中 在我的windows应用程序中 下面是我的代码。 In app.config: inside connectionStrings tag add name="ConnectionString" connectionString="server=localhost;database=my_db;user=root;port=3306; p
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();
或
或
当然,为您的环境使用适当连接器的建议仍然有效
你的代码重构了一点
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);
}
}
您的代码失败,因为正在尝试执行一个命令,而该命令未绑定到打开的连接。有很多方法可以绑定连接
MySqlCommand cmd = conn.CreateCommand();
或
或
当然,为您的环境使用适当连接器的建议仍然有效
你的代码重构了一点
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);
}
}
您是为Windows Phone/Mobile开发的吗?不,只是一个Windows窗体应用程序。如果我没有弄错的话,MySql.Data.CF.dll是为.NET Compact Framework开发的,如果我没记错的话,它只对Windows Phone/Mobile项目有用。尝试改用MySql.Data.dll(并从项目引用中删除MySql.Data.CF.dll),我也尝试过。但它似乎不起作用。报告的异常消息并不孤单。InnerException字段中还有一个。我敢打赌,这对于一个没有开放连接的命令来说是有意义的……你是为Windows Phone/Mobile开发的吗?不,只是一个Windows窗体应用程序。如果我没有弄错的话,MySql.Data.CF.dll是为.NET Compact Framework开发的,如果我没记错的话,它只对Windows Phone/Mobile项目有用。尝试改用MySql.Data.dll(并从项目引用中删除MySql.Data.CF.dll),我也尝试过。但它似乎不起作用。报告的异常消息并不孤单。InnerException字段中还有一个。我敢打赌,对于一个没有开放连接的命令来说,这是值得一提的……没错,当我添加“cmd.connection=conn”时,它工作得非常好。非常感谢,史蒂夫。竖起大拇指!这是正确的,当我添加'cmd.Connection=conn'时,它工作得非常好。非常感谢,史蒂夫。竖起大拇指!