2012-10-04 112 views
0

當我試圖在MS Access DB中插入一些數據時,數據未被插入。是我的代碼中缺少的東西?執行插入查詢後沒有數據插入到MDB中

這是代碼

public void Toevoegen(
     int nummer, string voornaam, string achternaam, 
     string gsm, string nationaliteit, string adres, 
     DateTime geboorteDatum, string geboortePlaats, DateTime inschrijvingsDatum, 
     string sporten, int postCode, string gemeente, string klasse, 
     Boolean competitie, string ziektes, string email) 
    { 
     try 
     { 
      string query = @"INSERT INTO Lid " 
       + "VALUES (" 
       + "@Lid_ID, " 
       + "@Lid_VoorNaam, " 
       + "@Lid_AchterNaam, " 
       + "@Lid_Email, " 
       + "@Lid_GeboorteDatum, " 
       + "@Lid_InschrijvingsDatum, " 
       + "@Lid_Nationaliteit, " 
       + "@Lid_GeboortePlaats, " 
       + "@Lid_Adres, " 
       + "@Lid_PostCode, " 
       + "@Lid_Gemeente, " 
       + "@Lid_GSM, " 
       + "@Lid_BeoefendeSporten, " 
       + "@Lid_Competitie, " 
       + "@Lid_KickKlasse, " 
       + "@Lid_Ziektes)"; 

      OleDbCommand command = new OleDbCommand(query); 
      command.Parameters.Add("@Lid_ID", OleDbType.Numeric).Value = nummer; 
      command.Parameters.Add("@Lid_VoorNaam", OleDbType.Char).Value = voornaam; 
      command.Parameters.Add("@Lid_Achternaam", OleDbType.Char).Value = achternaam; 
      command.Parameters.Add("@Lid_Email", OleDbType.Char).Value = email; 
      command.Parameters.Add("@Lid_GeboorteDatum", OleDbType.Date).Value = geboorteDatum; 
      command.Parameters.Add("@Lid_InschrijvingsDatum", OleDbType.Date).Value = inschrijvingsDatum; 
      command.Parameters.Add("@Lid_Nationaliteit", OleDbType.Char).Value = nationaliteit; 
      command.Parameters.Add("@Lid_GeboortePlaats", OleDbType.Char).Value = geboortePlaats; 
      command.Parameters.Add("@Lid_Adres", OleDbType.Char).Value = adres; 
      command.Parameters.Add("@Lid_PostCode", OleDbType.Numeric).Value = postCode; 
      command.Parameters.Add("@Lid_Gemeente", OleDbType.Char).Value = gemeente; 
      command.Parameters.Add("@Lid_GSM", OleDbType.Char).Value = gsm; 
      command.Parameters.Add("@Lid_BeoefendeSporten", OleDbType.Char).Value = sporten; 
      command.Parameters.Add("@Lid_Competitie", OleDbType.Boolean).Value = competitie; 
      command.Parameters.Add("@Lid_KickKlasse", OleDbType.Char).Value = klasse; 
      command.Parameters.Add("@Lid_Ziektes", OleDbType.Char).Value = ziektes; 
      conn.executeInsertQuery(command); 
     } 
     catch (Exception error) 
     { 
      System.Windows.Forms.MessageBox.Show(error.Message); 
     } 
    } 

-

public bool executeInsertQuery(OleDbCommand _command) 
    { 
     OleDbCommand myCommand = new OleDbCommand(); 
     try 
     { 
      myCommand.Connection = openConnection(); 
      //myCommand.CommandText = _query; 
      //myCommand.Parameters.AddRange(dbParameter); 
      myAdapter.InsertCommand = _command; 
      myCommand.ExecuteNonQuery(); 
     } 
     catch (OleDbException e) 
     { 
      Console.Write("Error - Connection.executeInsertQuery - Query: " 
       + _command.CommandText + " \nException: \n" + e.StackTrace.ToString()); 
      return false; 
     } 
     finally 
     { 
     } 
     return true; 
    } 

預先感謝您。

回答

1

問題將在這裏:

myAdapter.InsertCommand = _command; 
myCommand.ExecuteNonQuery(); 

分配命令myAdapter但隨後執行myCommand裏面是空的。

+0

我不熟悉OleDb,但將_command分配給myCommand,然後執行查詢將工作? – SamekaTV

+0

爲什麼不直接執行原始**命令**? –

+0

這正是我所做的,但數據未插入:s – SamekaTV