none
Help with C# Database Connection RRS feed

  • Question

  • My boss is deadset on doing data connection to forms through code and not through the supplied data environment with VS2005, I have managed to get the code to open an SQL connection through C# but the rest is alluding me. I need to be able to connect table columns to text fields and have them load their data automatically, but I also need it to be able to correspond to a button code that allows me to scroll up and down the rows of the table.

     

    Thank you in advance for any help.

    Tuesday, July 31, 2007 4:21 PM

Answers

  • Peculiar.  The easiest thing to do is to add a typed dataset to your project that retrieves and updates the information you want.  Once you've compiled the code you can grab the generated dataset class and use it like you would any other class.  You can even remove the dataset designer file but you then have to manually update the code for any changes.  The generated code will be identical to the code that you need to generate.  You can either use the dataset code as is (since you should isolate it from the rest of your code anyway) or copy the relevant code to your custom classes. 

     

    In summary, though, here are steps to manually build the code:

     

    1) Create a connection.

    2) Create an empty data set.

    3) Create a new data adapter.

    4) Create a new command object representing the command to execute to select the data into the data set.

    5) Create a new command object representing the command to execute to insert data into the database.

    6) Create a new command object representing the command to execute to update data in the database.

    7) Create a new command object representing the command to execute to delete data from the database.

    8) Associate each command with the connection and data adapter.

    9) Open the connection.

    10) Fill the data set through the data adapter.

    11) Create a binding source that wraps the data set.

    12) For each control associate the binding source and the specific table/column with the appropriate property of the control.

     

    As you can see this is a lot of work.  The wizard will generate the exact same code and be a lot quicker.  I would question the logic of your manager's decision.  Perhaps they just don't understand how things work and how much time you'd save by using the dataset wizard.  Writing and maintaining the above code will be time consuming.

     

    Michael Taylor - 7/31/07

    http://p3net.mvps.org

     

    Tuesday, July 31, 2007 5:40 PM