Updating and writing to Access Database in C#

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 1
Reputation: RemyDk is an unknown quantity at this point 
Solved Threads: 0
RemyDk RemyDk is offline Offline
Newbie Poster

Updating and writing to Access Database in C#

 
0
  #1
Sep 6th, 2008
I have looked through the forum for this information. But I did not find anything I could use in my little program.

I have this read code:
  1. ----------------------------------------------------------------------
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Data;
  6. using System.Data.OleDb;
  7.  
  8. namespace AddressBook
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. //Opret forbindelse til databasen.
  15. OleDbConnection oleConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=adresse_opslag.mdb");
  16. //Opret objekt og sql søgning.
  17. OleDbCommand oleCommand = new OleDbCommand("SELECT * FROM adresse_opslag", oleConnection);
  18.  
  19. try
  20. {
  21. //Åben database forbindelsen
  22. oleConnection.Open();
  23. //Opret "Datareader" som skal læse i tabelen.
  24. OleDbDataReader oleDataReader = oleCommand.ExecuteReader();
  25.  
  26. //"While loop" som skal søge hele data tabelen igennem.
  27. //"Console" som skal byttes ud med Windowsform!.
  28. while (oleDataReader.Read())
  29. {
  30. // Søge funktionen "Søgeord"
  31. if (oleDataReader.GetString(0) == "John")
  32. {
  33. Console.WriteLine(oleDataReader.GetString(0) + "\t" + oleDataReader.GetString(1) + "\t" + oleDataReader.GetString(2) + "\t" + oleDataReader.GetString(3));
  34. }
  35. }
  36.  
  37. //Luk forbindelsen.
  38. oleConnection.Close();
  39. //Hold console åben. Denne linje skal bare slettes.
  40. Console.Read();
  41. }
  42.  
  43. //Visning af fejl hvis den opstår.
  44. catch (OleDbException ex)
  45. {
  46. Console.WriteLine(ex.Message);
  47. Console.Read();
  48. }
  49. }
  50. }
  51. }
------------------------------------------------------------------

Now I need two codes to do the following:

Look through the entry “if (oleDataReader.GetString(0) == "John")” and if there is a match I need to overwrite the information:

oleDataReader.GetString(0) = ”Something New”
oleDataReader.GetString(1) = ”Something New”
oleDataReader.GetString(2) = ”Something New”
oleDataReader.GetString(3) = ”Something New”


I know that i should not use GetString but some thing like WriteString!

I also need a code to go to the end of the data and inset these information:

oleDataReader.GetString(0) = ”Something New”
oleDataReader.GetString(1) = ”Something New”
oleDataReader.GetString(2) = ”Something New”
oleDataReader.GetString(3) = ”Something New”

I know that i should not use GetString but some thing like WriteString!

I hope there is someone that could give med some code to look at. I am new at this and I am taking classes in C#, but I am working on a little projekt of my own!
Last edited by cscgal; Sep 6th, 2008 at 7:09 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 95
Reputation: gusano79 is on a distinguished road 
Solved Threads: 10
gusano79 gusano79 is offline Offline
Junior Poster in Training

Re: Updating and writing to Access Database in C#

 
0
  #2
Sep 7th, 2008
You can't update the database using OleDbDataReader or any other DbDataReader subclass. They provide forward-only, read-only access to data. You need something else.

There's more than one way to do this. Here's one:
  1. Create an OleDbDataAdapter using your SELECT statement.
  2. Create a OleDbCommandBuilder from your adapter.
  3. Set the adapter's UpdateCommand property using the command builder's GetUpdateCommand method.
  4. Use the adapter to fill a DataTable with your data.
  5. Modify the contents of the table's rows.
  6. Call the adapter's Update method to commit changes in the database.

This approach only requires you to write the SELECT statement; follow the links for more information on how to use these classes.

See also the System.Data.OleDb and System.Data namespace documentation.
--smg
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC