我试图将我的ASP.NET在线注册表单连接到MS Access后端数据库。我使用的连接代码是
String connString;
connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Z\Desktop\Comp\MyWebsite\WorkDatabase.mdb";
OleDbConnection myConnection = new OleDbConnection(connString);
myConnection.Open();
string myQuery = "INSERT INTO Parent([Username], [FirstName], [Surname], [Email], [Mobile], [Postcode], [Password]) values('" + Usernametb.Text + "','" + Firsttnametb.Text + "','" + Surnametb.Text + "','" + Emailtb.Text + "','" + Mobiletb.Text + "','" + Postcodetb.Text + "','" + Passwordtb.Text + "')";
OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
try
{
using (myConnection)
{
myConnection.Open();
myCommand.ExecuteNonQuery();
SuccReglbl.Text = "successful registration";
}
}
catch (Exception ex)
{
SuccReglbl.Text = "Exception in DBHandler" + ex;
}
finally
{
}
myCommand.ExecuteNonQuery();
OleDbDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
}
myConnection.Close();
点击"注册"我一直收到错误按钮。错误是
" ExecuteNonQuery需要一个开放且可用的连接。连接的当前状态已关闭。"
我不确定如何解决此问题。
答案 0 :(得分:0)
执行第一个查询后,您将关闭数据库连接。但是,在以下try-catch块之后,您执行另一个查询。此时,连接已关闭。
我建议在执行所有查询后才关闭数据库连接。这样,您就不需要为每个查询提供新连接。
<强>更新强>
我删除了所有关闭&#39;除了最后一个和不必要的使用语句。这段代码应该可以正常工作:
String connString;
connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Z\Desktop\Comp\MyWebsite\WorkDatabase.mdb";
OleDbConnection myConnection = new OleDbConnection(connString);
myConnection.Open();
string myQuery = "INSERT INTO Parent([Username], [FirstName], [Surname], [Email], [Mobile], [Postcode], [Password]) values('" + Usernametb.Text + "','" + Firsttnametb.Text + "','" + Surnametb.Text + "','" + Emailtb.Text + "','" + Mobiletb.Text + "','" + Postcodetb.Text + "','" + Passwordtb.Text + "')";
OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection);
myCommand.ExecuteNonQuery();
try
{
myCommand.ExecuteNonQuery();
SuccReglbl.Text = "successful registration";
}
catch (Exception ex)
{
SuccReglbl.Text = "Exception in DBHandler" + ex;
}
finally
{
}
myCommand.ExecuteNonQuery();
OleDbDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
}
myConnection.Close();
答案 1 :(得分:0)
您关闭了两次数据库连接。 尝试先删除myConnection.Close();
答案 2 :(得分:0)
@Jerodev 我的代码的最新版本是 String connString; connString = @“Provider = Microsoft.Jet.OLEDB.4.0;数据源= C:\ Users \ Z \ Desktop \ MyWebsite \ WorkDatabase.mdb”;
OleDbConnection myConnection = new
OleDbConnection(connString);
myConnection.Open();
string myQuery = "INSERT INTO Parent([Username], [FirstName], [Surname], [Email], [Mobile], [Postcode], [Password]) values('" + Usernametb.Text + "','" + Firsttnametb.Text + "','" + Surnametb.Text + "','" + Emailtb.Text + "','" + Mobiletb.Text + "','" + Postcodetb.Text + "','" + Passwordtb.Text + "')";
OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection);
myCommand.ExecuteNonQuery();
try
{
using (myConnection)
{
myCommand.ExecuteNonQuery();
SuccReglbl.Text = "successful registration";
}
}
catch (Exception ex)
{
SuccReglbl.Text = "Exception in DBHandler" + ex;
}
finally
{
myConnection.Close();
}
OleDbDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
}
它给出了同样的错误,但它说的是错误符合 “OleDbDataReader myReader = myCommand.ExecuteReader();”
答案 3 :(得分:0)
根据您所说的内容,这就是您需要的所有代码。
请注意,您的代码容易受到SQL注入攻击,因为您将用户输入直接填充到查询中。用户可以拥有您的数据库或非常容易地销毁它。 See this for more information和研究OleDbParameter
。
string myQuery = "INSERT INTO Parent([Username], [FirstName], [Surname], [Email], [Mobile], [Postcode], [Password]) values('" + Usernametb.Text + "','" + Firsttnametb.Text + "','" + Surnametb.Text + "','" + Emailtb.Text + "','" + Mobiletb.Text + "','" + Postcodetb.Text + "','" + Passwordtb.Text + "')";
string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Z\Desktop\Comp\MyWebsite\WorkDatabase.mdb";
try
{
using(OleDbConnection myConnection = new OleDbConnection(connString))
{
using(OleDbCommand myCommand = myConnection.CreateCommand())
{
myCommand.CommandText = myQuery;
myConnection.Open();
myCommmand.ExecuteNonQuery();
SuccReglbl.Text = "successful registration";
}
}
}
catch (Exception ex)
{
SuccReglbl.Text = "Exception in DBHandler " + ex.Message;
}