0

我正在尝试使用 ASP.NET C# 和 MS Access .mdb 数据库创建一个简单的用户注册系统。问题是,每当我尝试注册时,代码都会在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
4

2 回答 2

1

I'm not using any reserved words

password is indeed a reserved word in Access SQL. The following code fails with

OleDbException was unhandled: Syntax error in INSERT INTO statement.

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();
        }
    }
}

If I change the code to...

...
using (var cmd = new OleDbCommand(
        @"INSERT INTO Users (Email, [Password]) VALUES (?, ?)",
        conn))
{
...

...then it works fine.

于 2013-07-23T16:48:06.757 回答
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);
        }
    }

感谢您的评论:)

于 2013-07-23T17:21:06.620 回答