OleDbConnection - ExecuteNonQuery 需要一个打开且可用的连接。连接当前状态已关闭
我在这里做错了什么?
using System.Data;
using System.Data.OleDb;
namespace myProject.Account
{
public class DbManager
{
private OleDbConnection OpenDbConnection()
{
string connectionString = GetConnectionString();
return new OleDbConnection {ConnectionString = connectionString};
}
private string GetConnectionString()
{
return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\myDataBase.accdb";
}
public void InsertUser(string name, string loginName, string password)
{
OleDbConnection conn = OpenDbConnection();
OleDbCommand command = new OleDbCommand(
"INSERT INTO tblUser (UserName, LoginName, Password) VALUES (@name,@login,@pwd)",
Conn);
command.Parameters.Add("@name", OleDbType.VarChar).Value = name;
command.Parameters.Add("@login", OleDbType.VarChar).Value = loginName;
command.Parameters.Add("@pwd", OleDbType.VarChar).Value = password;
command.ExecuteNonQuery();
}
}
}
。
我收到这个错误:
ExecuteNonQuery 需要一个打开且可用的连接。连接的当前状态已关闭。
描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以了解更多信息 有关错误及其在代码中的来源的信息。异常详细信息:System.InvalidOperationException:ExecuteNonQuery 需要打开且可用的连接。连接的当前状态已关闭。
来源错误:
第 31 行:command.ExecuteNonQuery();
我尝试查看其他一些线程,但没有一个有帮助:
What am I doing wrong here?
using System.Data;
using System.Data.OleDb;
namespace myProject.Account
{
public class DbManager
{
private OleDbConnection OpenDbConnection()
{
string connectionString = GetConnectionString();
return new OleDbConnection {ConnectionString = connectionString};
}
private string GetConnectionString()
{
return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\myDataBase.accdb";
}
public void InsertUser(string name, string loginName, string password)
{
OleDbConnection conn = OpenDbConnection();
OleDbCommand command = new OleDbCommand(
"INSERT INTO tblUser (UserName, LoginName, Password) VALUES (@name,@login,@pwd)",
Conn);
command.Parameters.Add("@name", OleDbType.VarChar).Value = name;
command.Parameters.Add("@login", OleDbType.VarChar).Value = loginName;
command.Parameters.Add("@pwd", OleDbType.VarChar).Value = password;
command.ExecuteNonQuery();
}
}
}
.
I got this error:
ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more
information about the error and where it originated in the code.Exception Details: System.InvalidOperationException: ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
Source Error:
Line 31: command.ExecuteNonQuery();
I have tried to look at some other threads but none have helped:
ExecuteNonQuery requires an open and available Connection. The connection's current state is closed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎忘记打开连接(并且为命令分配了错误的连接)。尝试:
相反。我也建议使用 using 语句。它将为您管理您的连接。关闭。
You appear to be forgetting to open your connection (and are assigning the wrong connection to the command). Try:
Instead. I'd recommend using the using statement, too. It will manage your connection.close for you.
后
添加
或者修改
OpenDbConnection
如下:After
add
Alternatively, modify
OpenDbConnection
as follows: