尝试插入MSAccess DB时ASP.NET出错

时间:2017-03-02 07:42:11

标签: c# mysql asp.net database ms-access

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

附加信息: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 个答案:

答案 0 :(得分:3)

来自MSDN

  

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

默认情况下,OleDbCommandType设置为Text,因此您的父母&amp;应将子查询更改为此(请注意,所有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 keywordAccess ACE SQL keyword(即PASSWORD)。

SO相关问题:

What's wrong with these parameters?

insert into access database