Results 1 to 3 of 3

Thread: multiples fields to database problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    83

    multiples fields to database problem

    Hi,

    I have several textbox/combo box controls...from which I would be getting data and passing to methods to insert/update to the database....

    the thing is that these controls are by themselves...not inside a grid or anything......

    Since i have 38 fields to pass...!!!....any advice on the best way to do it would be appreciated....

    I usually have methods like for eg:

    InsertMethod(int someval1, string someval2, string someval3)
    {
    the insert sql stmnt
    }


    Around 25 fields go to the same table...and the rest would be going to other various tables.....I cant pass 25 values as parameters...would not be good I guess!

    Any ideas...please..?????????

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: multiples fields to database problem

    I cant pass 25 values as parameters
    Of course you can. That said, it may be prefereable to define a class that has a property for each of the values you want to pass. You then create an instance of this type and pass it to the method, which would then have a single argument.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: multiples fields to database problem

    You need to look into typed DataSets if you aren't already using them. Adding a DataSet to a project doesn't just create a generic DataSet object. It creates a strongly typed DataSet which you can add tables, primary keys, and relations. This will save you a TREMENDOUS amount of time.

    Just to get a feel for what you need to do, after you add a typed DataSet to your project, then add a new item and choose "Data Form Wizard". In the first step, choose the DataSet you created. Next, check to include an update button. Choose the default on the next two options. Then on "Choose the display type" pick "Single record in individual controls" and you can also choose which buttons appear on the form.

    This sets up all of your data binding automatically for you. The only thing you have to implement is your Load() and Update() methods.

    I've included an example that loads an XML file with set of customers from the Northwind Database.

    I've also included a simpler sample that connects to a sql server database.

    The key to updating a table easily is to use the SqlCommandBuilder (or OleDbCommandBuilder.) This automatically generates Insert, Update, and Delete statements for your DataAdapter so you don't have to create or fill any parameters.

    CSharp Code:
    1. this.sqlConnection = new SqlConnection(this.strConnection);
    2. this.sqlCommand = new SqlCommand("SELECT * FROM Notes", this.sqlConnection);
    3. this.sqlAdapter = new SqlDataAdapter(this.sqlCommand);
    4.  
    5. // automatically generate the delete, insert, and update commands
    6. // based on the select command
    7. SqlCommandBuilder commandBuilder = new SqlCommandBuilder(this.sqlAdapter);
    8.  
    9. // verify the delete, insert, update statements were generated
    10.  
    11. Debug.WriteLine(commandBuilder.GetDeleteCommand().CommandText);
    12. Debug.WriteLine(commandBuilder.GetInsertCommand().CommandText);
    13. Debug.WriteLine(commandBuilder.GetUpdateCommand().CommandText);
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width