简体   繁体   中英

OLEDB Command not generating a valid query

In my ASP.NET MVC application I have a form there the user can enter multiple texts. I want to save those inputs as OleDbCommand Parameters to generate a SQL Script. Unfortunately it's not working and I don't know why as I don't know what exact script is generated.

I worked with OleDbCommand before and it worked fine for a simple script (just an Update with a where clause). But now I want to Insert a new row in my DB.

//Create OleDbCommand and add parameters
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
myCommand.Parameters.AddWithValue("", myNewMassnahme.Name); //This is a string
myCommand.Parameters.AddWithValue("", myUser.myLoginUser._MySpAppS.Encrypt_AES(myNewMassnahme.Projektleiter.A_ID)); //this is a string
myCommand.Parameters.AddWithValue("", myNewMassnahme.MassnahmenartID); //this is an int
myCommand.Parameters.AddWithValue("", myNewMassnahme.Umsetzungsweg); //this is a string
myCommand.Parameters.AddWithValue("", myNewMassnahme.Status_ID); //this is an int
myCommand.Parameters.AddWithValue("", myNewMassnahme.Massnahmenstart); //this is a DateTime
myCommand.Parameters.AddWithValue("", myNewMassnahme.Painpoint); //This is a string
myCommand.Parameters.AddWithValue("", myNewMassnahme.Loesung); //This is a string
myCommand.Parameters.AddWithValue("", myNewMassnahme.Beteiligte_User); //This is a string

//This is just to check what command is generated (but it only shows the query with the '?'
myCommand.CommandText = String.Format("INSERT INTO MASSNAHMEN (NAME, PROJEKTLEITER_AID, MASSNAHMENART_ID, UMSETZUNGSWEG, STATUS_ID, MASSNAHMENSTART, PAINPOINT, LOESUNG, BETEILIGTE_USER) VALUES (?, ?, ?, ?, ?, TO_DATE(?, 'dd/mm/yyyy hh24:mi:ss'), ?, ?, ?);", myCommand.Parameters);


DataSet myDS = new DataSet();

//Make query on DB
//myUser.myLoginUser._MySpAppS.RunSQL simply runs a query on my DB. It expects a string and optional OleDbCommand.Parameters. It return a DataSet
myDS = myUser.myLoginUser._MySpAppS.RunSQL("INSERT INTO MASSNAHMEN (NAME, PROJEKTLEITER_AID, MASSNAHMENART_ID, UMSETZUNGSWEG, STATUS_ID, MASSNAHMENSTART, PAINPOINT, LOESUNG, BETEILIGTE_USER) VALUES (?, ?, ?, ?, ?, TO_DATE(?, 'dd/mm/yyyy hh24:mi:ss'), ?, ?, ?);", myCommand.Parameters);

I'm not getting any error message but I can see that no record is entered in my DB. I'm expecting a query like this:

INSERT INTO MASSNAHMEN
  (NAME, PROJEKTLEITER_AID, MASSNAHMENART_ID, UMSETZUNGSWEG, STATUS_ID, MASSNAHMENSTART, PAINPOINT, LOESUNG, BETEILIGTE_USER)
  VALUES( 'Test', 'Test_AID', 1, 'TEST_UMSETZUNGSWEG', 1, TO_DATE('15.08.2019 00:00:00', 'dd/mm/yyyy hh24:mi:ss'), 'Nothing works', 'Hopefully its getting better', 'Help!');

This is an example that worked totally fine:

System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
myCommand.Parameters.AddWithValue("", role.Description);
myCommand.Parameters.AddWithValue("", role.ID);

DataSet myDS = new DataSet();
myDS = myUser.myLoginUser._MySpAppS.RunSQL("UPDATE Rollen SET DESCRIPTION=? WHERE ID=?;", myCommand.Parameters);

After that myDS contains one table and I can see the changes in my DB

If I were doing it, I'd do this:

System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand( SET A OLEDBCONNECTION HERE );
myCommand.CommandText = "INSERT INTO MASSNAHMEN (NAME, PROJEKTLEITER_AID, MASSNAHMENART_ID, UMSETZUNGSWEG, STATUS_ID, MASSNAHMENSTART, PAINPOINT, LOESUNG, BETEILIGTE_USER) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
myCommand.Parameters.AddWithValue("", myNewMassnahme.Name); //This is a string
myCommand.Parameters.AddWithValue("", myUser.myLoginUser._MySpAppS.Encrypt_AES(myNewMassnahme.Projektleiter.A_ID)); //this is a string
myCommand.Parameters.AddWithValue("", myNewMassnahme.MassnahmenartID); //this is an int
myCommand.Parameters.AddWithValue("", myNewMassnahme.Umsetzungsweg); //this is a string
myCommand.Parameters.AddWithValue("", myNewMassnahme.Status_ID); //this is an int
myCommand.Parameters.AddWithValue("", myNewMassnahme.Massnahmenstart); //this is a DateTime
myCommand.Parameters.AddWithValue("", myNewMassnahme.Painpoint); //This is a string
myCommand.Parameters.AddWithValue("", myNewMassnahme.Loesung); //This is a string
myCommand.Parameters.AddWithValue("", myNewMassnahme.Beteiligte_User); //This is a string

int numRecordsInserted = myCommand.ExecuteNonQuery();

Things you need to fix, that I can't know:

  • the name of your oledbconnection, should be passed to the command constructor
  • the name of your db table: you change your mind as to what it is as you go through the code

Actually, saying "i'd do this" is a bit of a lie. I'd either use strongly typed datasets or an ORM like dapper, entity framework or nhibernate.. but once upon a time before I knew of the wonders of these data access helpers, I'd have gone this laborious route :)

I solved my Problem: The DateTime was making problems. I've changed:

myCommand.Parameters.AddWithValue("", myNewMassnahme.Massnahmenstart);

To:

myCommand.Parameters.AddWithValue("", myNewMassnahme.Massnahmenstart.ToShortDateString()); 

Now it works as expected

PS: And I also deleted the hours part in

TO_DATE(?, 'dd/mm/yyyy')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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