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..?????????
Re: multiples fields to database problem
Quote:
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.
2 Attachment(s)
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:
this.sqlConnection = new SqlConnection(this.strConnection);
this.sqlCommand = new SqlCommand("SELECT * FROM Notes", this.sqlConnection);
this.sqlAdapter = new SqlDataAdapter(this.sqlCommand);
// automatically generate the delete, insert, and update commands
// based on the select command
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(this.sqlAdapter);
// verify the delete, insert, update statements were generated
Debug.WriteLine(commandBuilder.GetDeleteCommand().CommandText);
Debug.WriteLine(commandBuilder.GetInsertCommand().CommandText);
Debug.WriteLine(commandBuilder.GetUpdateCommand().CommandText);