使用C#在ms访问中创建列时,字段定义中出现语法错误

本文关键字:定义 字段 错误 语法 ms 访问 使用 创建 | 更新日期: 2023-09-27 18:28:28

使用C#在ms访问中创建列时遇到问题。当我运行我的程序时,它会出错,并在buattabel1.ExecuteNonQuery()中显示"字段定义中的语法错误"。我只想在表"Coba"中制作一些列,并在cnttbl<班加罗尔。有人能帮我解决这个问题吗?

private void JmlhGarisBtn_Click(object sender, EventArgs e)
    {          
        if (PilihKameraComboBox.SelectedItem == "Proses Semua Kamera")
        {
            //do nothing
        }
        else if (PilihKameraComboBox.SelectedItem == "")
        {
            //do nothing
        }
        else
        {
            string jmlhgaris = JmlhGarisBox.Text;
            OleDbConnection kon = new OleDbConnection(koneksi);
            OleDbCommand simpangaris = kon.CreateCommand();
            OleDbCommand buattabel1 = kon.CreateCommand();
            OleDbCommand buattabel2 = kon.CreateCommand();
            OleDbCommand buattabel3 = kon.CreateCommand();
            OleDbCommand buattabel4 = kon.CreateCommand();
            OleDbCommand bacagaris = kon.CreateCommand();
            kon.Open();
            string simpanbanyakgaris = "update koordinatkamera set jmlhgaris='" + jmlhgaris + "' where namakamera = '" + PilihKameraComboBox.Text + "'";
            string bacadata = "select * from koordinatkamera where namakamera = '" + PilihKameraComboBox.Text + "'";
            simpangaris.CommandText = simpanbanyakgaris;
            bacagaris.CommandText = bacadata;
            simpangaris.ExecuteNonQuery();
            OleDbDataReader bacabanyakgaris = bacagaris.ExecuteReader();
            while (bacabanyakgaris.Read())
            {
                int banyakgaris = int.Parse(bacabanyakgaris["jmlhgaris"].ToString());
                for (int cnttbl = 0; cnttbl < banyakgaris; cnttbl++ )
                {
                    string bikintabel1 = "ALTER TABLE Coba ADD COLUMN halo" + ((cnttbl * 4) + 1) + "Smallint NOT NULL";
                    buattabel1.CommandText = bikintabel1;
                    string bikintabel2 = "ALTER TABLE Coba ADD COLUMN halo" + ((cnttbl * 4) + 2) + "Smallint NOT NULL";
                    buattabel2.CommandText = bikintabel2;
                    string bikintabel3 = "ALTER TABLE Coba ADD COLUMN halo" + ((cnttbl * 4) + 3) + "Smallint NOT NULL";
                    buattabel3.CommandText = bikintabel3;
                    string bikintabel4 = "ALTER TABLE Coba ADD COLUMN halo" + ((cnttbl * 4) + 4) + "Smallint NOT NULL";
                    buattabel4.CommandText = bikintabel4;

                    buattabel1.ExecuteNonQuery();
                    buattabel2.ExecuteNonQuery();
                    buattabel3.ExecuteNonQuery();
                    buattabel4.ExecuteNonQuery();
                }
            }
            kon.Close();
            JmlhGarisBox.Text = "";
        }

使用C#在ms访问中创建列时,字段定义中出现语法错误

"ALTER TABLE Coba ADD COLUMN halo" + ((cnttbl * 4) + 1) + "Smallint NOT NULL"将读取ALTER TABLE Coba ADD COLUMN halo1Smallint NOT NULL

您必须在Smalllint之前添加一个额外的空间:"ALTER TABLE Coba ADD COLUMN halo" + ((cnttbl * 4) + 1) + " Smallint NOT NULL"