Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all visitors,
I have 4 tables(1 TransferMaster 2 TransferDetail 3 PartnerDeptMaster 4 PartnerDeptDetail).In my form,when i click Save Button, i want the code insert to 4 tables. Is it possible that i want to use only one SQLDadaAdapter and one DataSet?
Thanks
TONY
Posted

Hi Tony,

It is possible. But you have to reassign the select,insert,update and delete commands to the data Adapter each time.

the code should be something like this
VB
Imports System.Data.SqlClient
Public Class Form1
    Dim ds As DataSet 'which contains your tables.
    Dim adp As SqlDataAdapter 'create the connection accordingly
    Dim con As SqlConnection

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For Each tbl As DataTable In ds.Tables
            adp = New SqlDataAdapter
            If tbl.TableName = "TransferMaster" Then
                adp.SelectCommand = New SqlCommand("your select command", con)
                adp.InsertCommand = New SqlCommand("Your insert command", con)
                adp.UpdateCommand = New SqlCommand("Your update command", con)
                adp.DeleteCommand = New SqlCommand("Your delete command", con)
            ElseIf tbl.TableName = "TransferDetail" Then
                adp.SelectCommand = New SqlCommand("your select command", con)
                adp.InsertCommand = New SqlCommand("Your insert command", con)
                adp.UpdateCommand = New SqlCommand("Your update command", con)
                adp.DeleteCommand = New SqlCommand("Your delete command", con)
                'esleif --continue as many tables you have
            End If
            adp.Update(tbl)
        Next
    End Sub
End Class
 
Share this answer
 
v2
Hi Tony,

I think it possible. But u hav to open and close connection everytime.

I dont know why ur using 1 dataadpter n dataset. but it is good that to use multiple dataset n dataadpter for multiple query.
 
Share this answer
 
Hi,
Based on ur requirement, U should also use Transactions for ur sql commands while inserting into multiple tables. Below is the sample code might be useful for u.

C#
public void save(DataSet newdsEstimation){
sqlDataAdapter = (SqlDataAdapter)SqlDB.GetDataAdapter();
            if (sqlConnection.State == System.Data.ConnectionState.Closed)
                sqlConnection.Open();
            sqlTrans = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
foreach (DataTable dt in newdsEstimation.Tables)
                {
                    if (dt.TableName == "Material")
                    {
                        PrepareEstimationCommand(sqlTrans, sqlDataAdapter, dt.TableName);
                        sqlDataAdapter.Update(objdsEstimations.EstimationMaterial);
                    }
                    else if (dt.TableName == "Service")
                    {
                        PrepareEstimationCommand(sqlTrans, sqlDataAdapter, dt.TableName);
                        sqlDataAdapter.Update(objdsEstimations.EstimationService);
                    }
                   
                }
                sqlTrans.Commit();
}
void PrepareEstimationCommand(SqlTransaction sqlTrans, SqlDataAdapter sqlDataAdapter, string strTableName)
        {
            string strInsertSQL = string.Empty;
            string strUpdateSql = string.Empty;
            if (strTableName == "Material")
            {
                strInsertSQL = @"INSERT INTO Materials
                                        (
                                            ----
                                        )
                                        VALUES
                                        (
                                            ----
                                        )";
            }
            else{
                  strInsertSQL = @"INSERT INTO Services
                                        (
                                            ----
                                        )
                                        VALUES
                                        (
                                            ----
                                        )";
SqlCommand sqlCmdInsert = (SqlCommand)SqlDB.GetSqlStringCommand(strInsertSQL);
            sqlCmdInsert.Connection = sqlConnection;
            sqlCmdInsert.Transaction = sqlTrans;
            SetInsertParam_EstimationDetail(sqlCmdInsert, strTableName);
            sqlDataAdapter.InsertCommand = sqlCmdInsert;

           //U can write update qry also like this way.
}


Regards,
Santhosh Kumar G
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900