我正在尝试使用ASP.NET C#和MS Access .mdb数据库创建一个简单的用户注册系统。问题是,每当我尝试注册时,代码在“try”中的OleDbCommand
处失败并被“捕获”。我在MS Visual Studio 2012中使用了断点,据我所知,所有键都存在。我的代码如下:
以下是注册方法所在的班级:
public static class DbUtil
{
private static OleDbConnection openConnection()
{
OleDbConnection con = new OleDbConnection();
try
{
string conString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|MainDatabase.mdb";
con = new OleDbConnection(conString);
con.Open();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return con;
}
private static void closeConnection(OleDbConnection con)
{
try
{
con.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public static bool registerUser(string email, string password)
{
try
{
using (OleDbConnection con = openConnection())
{
string sqlString = @"INSERT INTO Users (Email, Password) VALUES (?, ?)";
using (OleDbCommand command = new OleDbCommand(sqlString, con))
{
command.Parameters.AddWithValue("Email", email);
command.Parameters.AddWithValue("Password", password);
command.ExecuteNonQuery();
closeConnection(con);
return true;
}
}
}
catch
{
return false;
}
}
}
这是调用方法的地方:
public void btnRegisterSubmit_Click(object sender, EventArgs e)
{
string strEmail = txtRegisterEmail.Text;
string strPassword = txtRegisterPassword.Text;
bool regSuccess = DbUtil.registerUser(strEmail, strPassword);
if (regSuccess == false)
{
Response.Redirect("Default.aspx?reg=fail");
}
else
{
if (regSuccess == true)
{
Response.Redirect("Default.aspx?reg=success");
}
else
{
Response.Redirect("Default.aspx?reg=error");
}
}
}
我目前正在使用赞美参数方法,这是第一次。我确实在SQL字符串中使用了一个简单的连接方法,但这也产生了同样的问题。
此外,没有打印错误或抛出异常。
任何帮助都将受到高度赞赏。提前谢谢!
更新:
例外:
System.Data.OleDb.OleDbException (0x80040E14): Syntax error in INSERT INTO statement. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at DbUtil.registerUser(String email, String password) in c:\Users\******\Documents\Visual Studio 2012\WebSites\********************\App_Code\DbUtil.cs:line 51
答案 0 :(得分:1)
我没有使用任何保留字
password
确实是Access SQL中的保留字。以下代码失败,带
OleDbException未处理:INSERT INTO语句中的语法错误。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
namespace oledbTest1
{
class Program
{
static void Main(string[] args)
{
using (var conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\__tmp\testData.accdb;"))
{
conn.Open();
using (var cmd = new OleDbCommand(
@"INSERT INTO Users (Email, Password) VALUES (?, ?)",
conn))
{
cmd.Parameters.AddWithValue("?", "gord@example.com");
cmd.Parameters.AddWithValue("?", "myPassword");
cmd.ExecuteNonQuery();
}
conn.Close();
}
Console.WriteLine("Done.");
Console.ReadKey();
}
}
}
如果我将代码更改为...
...
using (var cmd = new OleDbCommand(
@"INSERT INTO Users (Email, [Password]) VALUES (?, ?)",
conn))
{
...
...然后它运作正常。
答案 1 :(得分:1)
管理以解决此问题。我重写了registerUser()
方法,并删除了using
块,现在效果很好。以下是已删除部分的代码:
public static bool registerUser(string email, string password)
{
try
{
//using (OleDbConnection con = openConnection())
//{
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|MainDatabase.mdb");
string sqlString = "INSERT INTO [Users] ([Email], [Password]) VALUES (@Email, @Password)";
//using (OleDbCommand command = new OleDbCommand(sqlString, con))
//{
OleDbCommand command = new OleDbCommand(sqlString, con);
command.Parameters.AddWithValue("@Email", email);
command.Parameters.AddWithValue("@Password", password);
con.Open();
command.ExecuteNonQuery();
con.Close();
return true;
//}
//}
}
catch (Exception e)
{
return false;
//System.Diagnostics.Debug.WriteLine(e.Message);
}
}
感谢您的评论:)