简体   繁体   English

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

[英]ALTER TABLE in Access database from C#

I need to add datagridview data to a MS Access table, at the same time add a column into that table and update the column as well. 我需要将datagridview数据添加到MS Access表中,同时在该表中添加一列并更新该列。 please help me, I keep getting an error that says Syntax error in field definition and then says No value given for one or more required parameters. 请帮助我,我不断收到错误消息,指出字段定义中的语法错误,然后提示未为一个或多个必需参数提供值。

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

I keep getting an error that says Syntax error in field definition 我不断收到一个错误,指出字段定义中的语法错误

That's probably because the ALTER TABLE statement 那可能是因为ALTER TABLE语句

ALTER TABLE Attendance ADD COLUMN SignIn

does not specify the column type. 不指定列类型。 Based on the UPDATE command that follows, you probably meant 根据下面的UPDATE命令,您可能是说

ALTER TABLE Attendance ADD COLUMN SignIn DATETIME

However, there are other problems with your code, too: 但是,您的代码也存在其他问题:

  1. Why are you trying to ALTER TABLE after every button click? 为什么每次单击按钮后尝试更改表? Once you've changed the structure of the [Attendance] table you don't need to do it again. 更改[出勤]表的结构后,无需再次进行此操作。

  2. You are trying to pass a date/time value delimited with single quotes ( ' ). 您正在尝试传递以单引号( ' )分隔的日期/时间值。 Access SQL normally expects date/time values to be delimited with hash marks ( # ). Access SQL通常期望日期/时间值用井号( # )分隔。

  3. Instead of "gluing SQL statements together" you should use parameterized queries to help you avoid delimiter problems (see above) and protect you from SQL Injection. 而不是“将SQL语句粘合在一起”,您应该使用参数化查询来帮助您避免定界符问题(请参见上文)并保护您免受SQL注入的侵害。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM