ASP.NET 在尝试插入 MS Access DB 时出错

Posted

技术标签:

【中文标题】ASP.NET 在尝试插入 MS Access DB 时出错【英文标题】:ASP.NET getting an error when trying to insert into MSAccess DB 【发布时间】:2017-03-02 07:42:11 【问题描述】:

我有一个即将到期的学校项目,它是一个 ASP.NET 中的网络应用程序,我认为我已经完成并使用本地 mysql 数据库按预期工作。我的教授最近指示我们将数据库更改为 MS Access,并且我的大部分课程都在工作(例如登录,它只检查表中已有的数据)。我遇到的最后一个问题是我的注册页面,我收到关于我的插入语句的错误。 (在 System.Data.dll 中发生了“System.Data.OleDb.OleDbException”类型的异常,但未在用户代码中处理

附加信息:INSERT INTO 语句中的语法错误。)

我尝试根据其他帖子/youtube 视频调整各种内容,但到目前为止没有任何效果。我非常感谢有人指出我哪里出错了!提前致谢。

protected void submitBtn_Click(object sender, EventArgs e)
    
        OleDbConnection connect = new OleDbConnection(ConfigurationManager.ConnectionStrings["newregDBConnectionString"].ConnectionString);
                                                   
            if (parentRadBtn.Checked)
            
                if (firstNameBox.Text == "" || surnameBox.Text == "" || postcodeBox.Text == "" || teleBox.Text == "" || emailBox.Text == "" || userBox.Text == "" || passwordBox.Text == "")
                
                    Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
                    successLabel.Text = ("");
                    userBox.Text = "";
                    firstNameBox.Text = "";
                    surnameBox.Text = "";
                    postcodeBox.Text = "";
                    teleBox.Text = "";
                    emailBox.Text = "";
                    passwordBox.Text = "";
                                 
                else
                
                    OleDbCommand pa = new OleDbCommand("INSERT INTO parent(parentID, firstname, surname, postcode, telephone, email, password) VALUES (@parentID, @firstname, @surname, @postcode, @telephone, @email, @password)", connect);
                    pa.Parameters.AddWithValue("@parentID", userBox.Text);
                    pa.Parameters.AddWithValue("@firstname", firstNameBox.Text);
                    pa.Parameters.AddWithValue("@surname", surnameBox.Text);
                    pa.Parameters.AddWithValue("@postcode", postcodeBox.Text);
                    pa.Parameters.AddWithValue("@telephone", teleBox.Text);
                    pa.Parameters.AddWithValue("@email", emailBox.Text);
                    pa.Parameters.AddWithValue("@password", passwordBox.Text);

                    connect.Open();
                    pa.ExecuteNonQuery();
                    connect.Close();
                

                if (IsPostBack)
                
                    userBox.Text = "";
                    firstNameBox.Text = "";
                    surnameBox.Text = "";
                    postcodeBox.Text = "";
                    teleBox.Text = "";
                    emailBox.Text = "";
                    passwordBox.Text = "";
                

                       
            else if (childRadBtn.Checked)
            
                if (firstNameBox.Text == "" || dayDobList.Text == "" || monthDobList.Text == "" || yearDobList.Text == "" || genderList.Text == "" || userBox.Text == "" || passwordBox.Text == "")
                
                    Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
                    successLabel.Text = ("");
                    userBox.Text = "";
                    firstNameBox.Text = "";
                    dayDobList.Text = "";
                    monthDobList.Text = "";
                    yearDobList.Text = "";
                    genderList.Text = "";
                    passwordBox.Text = "";
                                 
                else
                
                    OleDbParameter dob = new OleDbParameter("@dob", OleDbType.DBDate);
                    dob.Value = new DateTime(Int32.Parse(yearDobList.Text), Int32.Parse(monthDobList.Text), Int32.Parse(dayDobList.Text));                      

                    OleDbCommand ca = new OleDbCommand("INSERT INTO children(childID, firstname, dob, gender, password) VALUES (@childID, @firstname, @dob, @gender, @password)", connect);
                    ca.Parameters.AddWithValue("@childID", userBox.Text);
                    ca.Parameters.AddWithValue("@firstname", firstNameBox.Text);
                    ca.Parameters.Add(dob);
                    ca.Parameters.AddWithValue("@gender", genderList.Text);
                    ca.Parameters.AddWithValue("@password", passwordBox.Text);

                    connect.Open();
                    ca.ExecuteNonQuery();
                    connect.Close();
                
                if (IsPostBack)
                
                    userBox.Text = "";
                    firstNameBox.Text = "";
                    dayDobList.Text = "";
                    monthDobList.Text = "";
                    yearDobList.Text = "";
                    genderList.Text = "";
                    passwordBox.Text = "";
                              
            
        
    

    protected void Button1_Click(object sender, EventArgs e)
    
        Response.Redirect("ViewDelete.aspx");
    

    protected void backBtn_Click(object sender, EventArgs e)
    
        Response.Redirect("Home.aspx");
    

    protected void emailBox_TextChanged(object sender, EventArgs e)
    

    

    protected void userBox_TextChanged(object sender, EventArgs e)
    

    

    protected void firstNameBox_TextChanged(object sender, EventArgs e)
    

    

【问题讨论】:

【参考方案1】:

来自MSDN:

OLE DB .NET 提供程序不支持传递命名参数 SQL 语句或存储过程的参数 当 CommandType 设置为 Text 时的 OleDbCommand。在这种情况下, 必须使用问号 (?) 占位符。因此,OleDbParameter 对象添加到 OleDbParameterCollection 中的顺序必须直接对应于命令文本中参数的问号占位符的位置。

默认情况下OleDbCommandType 设置为Text,因此您的父和子查询应更改为此(注意所有OleDbParameter 应按正确顺序声明):

// insert parent command
OleDbCommand pa = new OleDbCommand("INSERT INTO parent(parentID, firstname, surname, postcode, telephone, email, [password]) VALUES (?, ?, ?, ?, ?, ?, ?)", connect);

// insert children command
OleDbCommand ca = new OleDbCommand("INSERT INTO children(childID, firstname, dob, gender, [password]) VALUES (?, ?, ?, ?, ?)", connect);

注意:所有可能与 Access OLE DB 保留字冲突的列名都应放在方括号中,以防止意外使用Access JET SQL keyword 或Access ACE SQL keyword(即PASSWORD)。

SO 相关问题:

What's wrong with these parameters?

insert into access database

【讨论】:

非常感谢,将方括号放在密码上有效。我确实简要地查看了保留字,但浏览时没有看到密码!再次,谢谢你:)

以上是关于ASP.NET 在尝试插入 MS Access DB 时出错的主要内容,如果未能解决你的问题,请参考以下文章

针对 MS Access DB 设置此 asp.net 页面的最快方法。 .

ASP.NET - VB.NET - 从 MS-Access 数据库中检索记录

将 asp.net web 表单连接到 ms access 数据库

从 MS Access 批量导入并插入 Sql Server [关闭]

我的 MS Access 数据库不会更新 asp.net

ASP.NET - MS-ACCESS - VB.NET - SQL 语句错误