Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. inserting data into Access using C#??

inserting data into Access using C#??

Scheduled Pinned Locked Moved C#
csharpdatabasewindows-adminsecurityquestion
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Azel Low

    Hi, I am trying to learn ADO.net using C# and I keep running into problems trying to insert data into my Access Database: data.mdb. here is my code: // Database Variables private string connectionStr = @"Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Data Source=""C:\data.mdb"";Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1"; private string selectStr = "Select id, date, day, time, comments, timeout, status, process from data"; private System.Data.DataSet myDataSet; private System.Data.OleDb.OleDbConnection myConnection; private System.Data.OleDb.OleDbDataAdapter myDataAdapter; private System.Data.OleDb.OleDbCommandBuilder myCmdBuilder; private System.Data.OleDb.OleDbCommand mySelectCmd; public Schedule() { this.InitilizeDb(); this.PopulateDataSet(); } private OleDbConnection ConnectDb() { return new OleDbConnection(connectionStr); } public void InitilizeDb() { myDataAdapter = new OleDbDataAdapter(); myCmdBuilder = new OleDbCommandBuilder(myDataAdapter); myDataSet = new DataSet(); } private void PopulateDataSet() { // get the connection object myConnection = (OleDbConnection) this.ConnectDb(); // Initilize the Select Command mySelectCmd = new OleDbCommand(selectStr, myConnection); // Define that the Select Command is an SQL statement mySelectCmd.CommandType = CommandType.Text; try { myConnection.Open(); myDataAdapter.SelectCommand = mySelectCmd; // Populate the DataSet from the "data" table myDataAdapter.Fill(myDataSet,"data"); } finally { myConnection.Close(); } } public string InsertSchedule(string date, string day, string time, string process, string comments, string timeout, string status) { // get the connection object myConnection = (OleDbConnection) this.ConnectDb(); try { myConnection.Open(); // create a new row to insert the data into DataRow newRow = myDataSet.Tables["data"].NewRow(); // initilize the new row newRow["id"] = 3; newRow["date"] = date;

    H Offline
    H Offline
    Heath Stewart
    wrote on last edited by
    #2

    Because you haven't specified a INSERT SqlCommand. The SqlDataAdapter isn't magical - you need to provide SELECT, INSERT, UPDATE, and DELETE SqlCommands for full functionality. Specifying just a SELECT SqlCommand doesn't generate the others. If you're unsure about how to do this, read about the SqlCommand and SqlDataAdapter classes in the SDK documentation, or use drop a SqlDataAdapter onto your form or control in VS.NET and run through the wizard. Be sure to look at what source it generates though to fully understand what is going on.

    -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

    A 1 Reply Last reply
    0
    • H Heath Stewart

      Because you haven't specified a INSERT SqlCommand. The SqlDataAdapter isn't magical - you need to provide SELECT, INSERT, UPDATE, and DELETE SqlCommands for full functionality. Specifying just a SELECT SqlCommand doesn't generate the others. If you're unsure about how to do this, read about the SqlCommand and SqlDataAdapter classes in the SDK documentation, or use drop a SqlDataAdapter onto your form or control in VS.NET and run through the wizard. Be sure to look at what source it generates though to fully understand what is going on.

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      A Offline
      A Offline
      Azel Low
      wrote on last edited by
      #3

      Oic, I thought the commandbuilder class is suppose to auto generate the insert, update and delete commands? so even if i have set the commandbuilder to my data adapter, I must still initilize my insert, update and delete commands? I will also take a look at the SDK documentation.. thanks.

      H 1 Reply Last reply
      0
      • A Azel Low

        Oic, I thought the commandbuilder class is suppose to auto generate the insert, update and delete commands? so even if i have set the commandbuilder to my data adapter, I must still initilize my insert, update and delete commands? I will also take a look at the SDK documentation.. thanks.

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #4

        I missed that line, sorry. The SqlCommandBuilder may have trouble with your SELECT statement. Another possibility is with the table mappings of the SqlDataAdapter. You're setting your table name as "data". Does that actually map to the name of the table from which you're selecting records? These should match and you might have to set up a table mapping using the SqlDataAdapter.TableMappings property. This fairly straight-forward.

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        A 1 Reply Last reply
        0
        • H Heath Stewart

          I missed that line, sorry. The SqlCommandBuilder may have trouble with your SELECT statement. Another possibility is with the table mappings of the SqlDataAdapter. You're setting your table name as "data". Does that actually map to the name of the table from which you're selecting records? These should match and you might have to set up a table mapping using the SqlDataAdapter.TableMappings property. This fairly straight-forward.

          -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

          A Offline
          A Offline
          Azel Low
          wrote on last edited by
          #5

          the dataset table name is "data" which is the same as the one from the source table name. i got the idea from MSDN that table mapping is use when your data source and dataset names are different? So if I have used the same name do I still need tablemapping? thanks

          H 1 Reply Last reply
          0
          • A Azel Low

            the dataset table name is "data" which is the same as the one from the source table name. i got the idea from MSDN that table mapping is use when your data source and dataset names are different? So if I have used the same name do I still need tablemapping? thanks

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #6

            Yes, unless you use a strongly-typed DataSet. In most cases, the SELECT SqlCommand (with one or multiple result sets) loads tables by default named "Table", "Table1", "Table2", ..., "TableN". Your TableMappings should map these table names to your actual tables in your database.

            -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

            A 1 Reply Last reply
            0
            • H Heath Stewart

              Yes, unless you use a strongly-typed DataSet. In most cases, the SELECT SqlCommand (with one or multiple result sets) loads tables by default named "Table", "Table1", "Table2", ..., "TableN". Your TableMappings should map these table names to your actual tables in your database.

              -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

              A Offline
              A Offline
              Azel Low
              wrote on last edited by
              #7

              humm.. still wondering y i got the exception thrown when i try to perform an update to the database. just wondering if you are able to point me or share with me some sample that has insert, delete and update without the use of stored procedure?

              H J 2 Replies Last reply
              0
              • A Azel Low

                humm.. still wondering y i got the exception thrown when i try to perform an update to the database. just wondering if you are able to point me or share with me some sample that has insert, delete and update without the use of stored procedure?

                H Offline
                H Offline
                Heath Stewart
                wrote on last edited by
                #8

                I did actually. Drag a SqlDataAdapter to your form or control and go through the wizard, specifying that new commands should be created instead of using new or existing stored procs. Take a look at what's generated in your source file. You can also read the SDK documentation for SqlDataAdapter, which does have examples in various class and method documentation.

                -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                1 Reply Last reply
                0
                • A Azel Low

                  humm.. still wondering y i got the exception thrown when i try to perform an update to the database. just wondering if you are able to point me or share with me some sample that has insert, delete and update without the use of stored procedure?

                  J Offline
                  J Offline
                  Jerry Hammond
                  wrote on last edited by
                  #9

                  If you do have to browse to find the assemblies and all you find are xml files of the managed assemblies then go here for the latest DirectX 9 managed download: http://msdn.microsoft.com/library/default.asp?url=/downloads/list/directx.asp[^] Just follow the instructs and viola you should have the assemblies. You'll know right away if you've got them installed correctly because when you fire up VS.Net you'll see the green directx logo along with your other languages on the splash screen. Have fun! Best, Jerry

                  The only way of discovering the limits of the possible is to venture a little past them into the impossible.--Arthur C. Clark

                  Toasty0.com

                  H 1 Reply Last reply
                  0
                  • J Jerry Hammond

                    If you do have to browse to find the assemblies and all you find are xml files of the managed assemblies then go here for the latest DirectX 9 managed download: http://msdn.microsoft.com/library/default.asp?url=/downloads/list/directx.asp[^] Just follow the instructs and viola you should have the assemblies. You'll know right away if you've got them installed correctly because when you fire up VS.Net you'll see the green directx logo along with your other languages on the splash screen. Have fun! Best, Jerry

                    The only way of discovering the limits of the possible is to venture a little past them into the impossible.--Arthur C. Clark

                    Toasty0.com

                    H Offline
                    H Offline
                    Heath Stewart
                    wrote on last edited by
                    #10

                    Wrong thread :)

                    -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                    J 1 Reply Last reply
                    0
                    • H Heath Stewart

                      Wrong thread :)

                      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                      J Offline
                      J Offline
                      Jerry Hammond
                      wrote on last edited by
                      #11

                      Oh damn! Sorry!

                      The only way of discovering the limits of the possible is to venture a little past them into the impossible.--Arthur C. Clark

                      Toasty0.com

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups