连接必须有效并打开mysql [英] Connection must be valid and open mysql
问题描述
我在修复mysql db中的列中的最后一个值时遇到问题。
我得到连接必须是有效的并且打开错误。
有人可以请帮助?
谢谢
我的尝试:
I am having a problem in retirieving the last value in a column in mysql db.
I am getting connection must be valid and open error.
Can someone please help?
Thanks
What I have tried:
String connString = System.Configuration.ConfigurationManager.ConnectionStrings["web"].ToString();
using (MySqlConnection con = new MySqlConnection(connString))
{
using (MySqlCommand cmd = new MySqlCommand("SELECT TOP 1 idProyecto FROM proyectos order by idProyecto DESC LIMIT 1"))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
con.Open();
val1 = cmd.ExecuteScalar().ToString();
con.Close();
return val1;
}
}
推荐答案
cmd.Connection = conn;
cmd.Connection = conn;
您已将连接对象声明为con,然后告诉您的命令对象使用连接对象称为conn。
由于这实际编译了,看来你在同一范围或更高的范围内有一个名为conn的另一个连接对象等级范围。这样做是不好的做法。
You've declared your connection object as "con" and then told your command object to use a connection object called "conn".
Since this actually compiled, it appears you've got another connection object called "conn" somewhere above this in the same scope or in a higher level scope. That would be bad practice to have that.
这篇关于连接必须有效并打开mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!