来自C#的Access数据库中的ALTER TABLE

时间:2013-09-18 20:34:38

标签: c# ms-access datagridview ms-access-2010 alter

我需要将datagridview数据添加到MS Access表中,同时在该表中添加一列并更新列。请帮助我,我不断收到错误,在字段定义中显示语法错误,然后说没有给出一个或多个必需参数的值。

private void button1_Click(object sender, EventArgs e)
    {
        string myConnectionString = " ";

        myConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/game/Desktop/3rd year/2nd Semester/INYM 328/EmployeeAttendanceRegister/EmployeeAttendanceRegister/EmployeeAttendanceRegister.accdb";
        OleDbConnection myConnection = new OleDbConnection(myConnectionString);

        string col1 = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
        string col2 = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();
        string col3 = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString();
        string col4 = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
        string col5 = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString();
        string col6 = dataGridView1[5, dataGridView1.CurrentCell.RowIndex].Value.ToString();
        string col7 = dataGridView1[6, dataGridView1.CurrentCell.RowIndex].Value.ToString();
        string myInsertQuery = "INSERT INTO Attendance VALUES('" + col1 + "','" + col2 + "','" + col3 + "','" + col4 + "','" + col5 + "','" + col6 + "','" + col7 + "')";

        OleDbCommand myCommand = new OleDbCommand(myInsertQuery);
        myCommand.Connection = myConnection;

        myConnection.Open();
        try
        {
            myCommand.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
        myConnection.Close();
        {
            string myConn = " ";

            myConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/game/Desktop/3rd year/2nd Semester/INYM 328/EmployeeAttendanceRegister/EmployeeAttendanceRegister/EmployeeAttendanceRegister.accdb";
            OleDbConnection myCon = new OleDbConnection(myConn);

            string myInsert = "ALTER TABLE Attendance ADD COLUMN SignIn";
            OleDbCommand myCom = new OleDbCommand(myInsert);
            myCom.Connection = myCon;

            myCon.Open();
            try
            {
                myCom.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            myCon.Close();
        }
        {
            string myString = " ";

            myString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/game/Desktop/3rd year/2nd Semester/INYM 328/EmployeeAttendanceRegister/EmployeeAttendanceRegister/EmployeeAttendanceRegister.accdb";
            OleDbConnection myConnect = new OleDbConnection(myString);
            string Insert = "UPDATE Attendance SET SignIn = '" + dateTimePicker1.Value + "'";

            OleDbCommand myComm = new OleDbCommand(Insert);
            myComm.Connection = myConnect;

            myConnect.Open();
            try
            {
                myComm.ExecuteNonQuery();

                MessageBox.Show("Successfully Signed IN");
                frmhomepage previousForm = new frmhomepage();
                previousForm.Show();
                this.Hide();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

            myConnect.Close();
        }
    }

1 个答案:

答案 0 :(得分:1)

  

我一直收到一个错误,指出字段定义中的语法错误

这可能是因为ALTER TABLE语句

ALTER TABLE Attendance ADD COLUMN SignIn

未指定列类型。根据后面的UPDATE命令,您可能需要

ALTER TABLE Attendance ADD COLUMN SignIn DATETIME

但是,您的代码还存在其他问题:

  1. 为什么在按下每个按钮后尝试ALTER TABLE?一旦你改变了[考勤]表的结构,你就不需要再做了。

  2. 您正在尝试传递用单引号(')分隔的日期/时间值。 Access SQL通常要求使用井号(#)分隔日期/时间值。

  3. 您应该使用参数化查询来帮助您避免分隔符问题(参见上文)并保护您免受SQL注入。