OleDbCommand command = new OleDbCommand(); //command.CommandText = "INSERT INTO products (id, name, price) values(addMusic();)"; command.CommandText = "INSERT INTO products (id, name, price) values('6','M.J.','150 kr.')"; command.Connection = connection;
command.ExecuteNonQuery(); connection.Close(); } catch (Exception e) { Console.WriteLine("Feeeel? Error in inserting new data!"); }
}
//method not working! public static void addMusic (OleDbConnection int, string, int connection) { //the id should be attributed automatically Console.WriteLine("Enter an id for the new product entry: "); int id = Console.ReadLine();
//Enter artist and album name separeted by a comma: ",". Console.WriteLine("Enter artist and album name: "); string name = Console.ReadLine();
Console.WriteLine("Enter the price of the product: " + "kr."); int price = Console.ReadLine(); }
Med udgangspunkt i din egen kode forestiller jeg mig noget i stil med flg. (som er helt og aldeles utestet):
public static void addInDB () { try { string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data source=C:\temp\products.mdb"; using(OleDbConnection connection = new OleDbConnection(connectionString)) { connection.Open();
OleDbCommand command = new OleDbCommand(); command.CommandText = "INSERT INTO products (id, name, price) values(?,?,?)"; addMusic(command); command.Connection = connection;
command.ExecuteNonQuery(); } } catch (Exception e) { Console.WriteLine("Error in inserting new data! " + e.ToString()); } }
public static void addMusic (OleDbCommand cmd) { //the id should be attributed automatically Console.WriteLine("Enter an id for the new product entry: "); int id = Console.ReadLine(); cmd.Parameters.AddWithValue("@id", id);
//Enter artist and album name separeted by a comma: ",". Console.WriteLine("Enter artist and album name: "); string name = Console.ReadLine(); cmd.Parameters.AddWithValue("@name", name);
Console.WriteLine("Enter the price of the product in kr.: "); int price = Console.ReadLine(); cmd.Parameters.AddWithValue("@price", price); }
Jeg får nedestående fejl. Ved ikke rigtigt hvad det betyder har ikke så meget erfaring med programmering...
'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\Ekesh\Desktop\Plushogskolan\C#\Lab3_2.1\Lab3_2.1\bin\Debug\Lab3_2.1.vshost.exe', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. The thread 'vshost.NotifyLoad' (0x14a0) has exited with code 0 (0x0). The thread 'vshost.LoadReference' (0x454) has exited with code 0 (0x0). 'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\Ekesh\Desktop\Plushogskolan\C#\Lab3_2.1\Lab3_2.1\bin\Debug\Lab3_2.1.exe', Symbols loaded. 'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Transactions\v4.0_4.0.0.0__b77a5c561934e089\System.Transactions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.Wrapper.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'Lab3_2.1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. The thread '<No Name>' (0xa08) has exited with code 0 (0x0). A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll The thread 'vshost.RunParkingWindow' (0xb1c) has exited with code 0 (0x0). The thread '<No Name>' (0x278) has exited with code 0 (0x0). The program '[5292] Lab3_2.1.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).
Har fået dataen gemt i databasen og e.Tostring virker også ret godt, men kan stadig ikke få den puttet ind fra cmd'en, men det mest essentielle virker, og resten virker også som det er ved at være på plads, her er dine points, tak for hjælpen.
Har fået dataen gemt i databasen og e.Tostring virker også ret godt, men kan stadig ikke få den puttet ind fra cmd'en, men det mest essentielle virker, og resten virker også som det er ved at være på plads, her er dine points, tak for hjælpen.
Jeg er klar over det Arne, men for ikke at rode for meget i den eksisterende kode (og dermed gør den for forskellig fra spørgerens oprindelige kode), valgte jeg blot at tilpasse koden, så den (tilnærmelsesvis) fungerede.
Det at en Access database anvendes, giver mig et hint om, at det nok ikke har den store betydning, om det er optimalt kodet, men at det er mere vigtigt, at der blot er tale om mindre justeringer til spørgers egen kode...
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.