C#2010 Ms = Access 2003

时间:2015-03-20 12:06:27

标签: c# visual-studio-2010 ado.net ms-access-2003

我的应用程序使用C#2010和Access2003中的数据库,我想将新的recrord插入到我的表中,它有一个错误:

  

"插入语句"

中的语法错误
    private void button3_Click(object sender, EventArgs e)
    {
         String ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\New.mdb";
         OleDbConnection myconection = new OleDbConnection(ConnectionString);
         try
         {      
                 OleDbCommand mycomand = new OleDbCommand();
                 mycomand.CommandText = "INSERT INTO Refugees Characteristic(Nr,First Name,Last Name,Birthday,Country,City,Insurance Nr,Gander,Marital status,Nr of Children,Address,Mother Tongue,Other Languages,Phone Nr,Enter to Austria,Education,Skills,Picture) VALUES (@p1,@p2,@p3,@p4,@p5,@p6,@p7,@p8,@p9,@p10,@p11,@p12,@p13,@p14,@p15,@p16,@p17,@p18)";
                 mycomand.Parameters.Clear();
                 mycomand.Parameters.AddWithValue("@p1", IdTxt.Text);
                 mycomand.Parameters.AddWithValue("@p2", FirstTxt.Text);
                 mycomand.Parameters.AddWithValue("@p3", LasttextBox.Text);
                 mycomand.Parameters.AddWithValue("@p4", BirthdayTxt.Text);
                 mycomand.Parameters.AddWithValue("@p5", CountryTxt.Text);
                 mycomand.Parameters.AddWithValue("@p6", CityTxt.Text);
                 mycomand.Parameters.AddWithValue("@p7", InsuranceTxt.Text);
                 mycomand.Parameters.AddWithValue("@p8", GanderBox.Text);
                 mycomand.Parameters.AddWithValue("@p9", marriedTxt.Text);
                 mycomand.Parameters.AddWithValue("@p10", ChildnumTxt.Text);
                 mycomand.Parameters.AddWithValue("@p11", AddressTxt.Text);
                 mycomand.Parameters.AddWithValue("@p12", MotherTongTxt.Text);
                 mycomand.Parameters.AddWithValue("@p13", OtherlanTxt.Text);
                 mycomand.Parameters.AddWithValue("@p14", phonNumberTxt.Text);
                 mycomand.Parameters.AddWithValue("@p15", EnterTxt.Text);
                 mycomand.Parameters.AddWithValue("@p16", EducationTxt.Text);
                 mycomand.Parameters.AddWithValue("@p17", SkillsTxt.Text);
                 mycomand.Parameters.AddWithValue("@p18", PicLocationtxt.Text);
                // mycomand.Connection = null;
                 mycomand.Connection = myconection;
                 myconection.Open();
                 mycomand.ExecuteNonQuery();
                 MessageBox.Show("New Record is Added");
                 myconection.Close();
                 myconection.Dispose();
                 mycomand.Dispose();
             }
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }

2 个答案:

答案 0 :(得分:0)

如果您的表名和列名是两个字,则需要使用方括号,例如[Refugees Characteristic][First Name][Last Name][Insurance Nr],{{1 }},[Marital status]等。作为一般用途,如果您必须使用两个单词作为列名,您可以将您的作品写在邻近。

同时使用using statement处理您的[Nr of Children]OleDbConnection,而不是手动调用他们的OleDbCommand.Close()方法。

.Dispose()

并且不要使用using(var myconection = new OleDbConnection(ConnectionString)) using(var mycomand = myconection.CreateCommand()) { // } 方法。 It may generate upexpected results sometimes。请改用AddWithValue重载来指定.Add()和参数大小。

最后,OleDbType似乎是MS Access中的表单和报表属性。这意味着它是reserved word。您还需要使用Picture

答案 1 :(得分:0)

将列名放在方括号内。

[First Name],[Marital status],[Nr of Children],[Address],[Mother Tongue],[Other Languages],[Phone Nr]

所以它会像

mycomand.CommandText = @"INSERT INTO [Refugees Characteristic]([Nr],[First Name],[Last Name],[Birthday],[Country],[City],[Insurance Nr],[Gander],[Marital status],[Nr of Children],[Address],[Mother Tongue],[Other Languages],[Phone Nr],[Enter to Austria],[Education],[Skills],[Picture]) VALUES (@p1,@p2,@p3,@p4,@p5,@p6,@p7,@p8,@p9,@p10,@p11,@p12,@p13,@p14,@p15,@p16,@p17,@p18)";