Results 1 to 10 of 10

Thread: [RESOLVED]Batch Table Insert

  1. #1

    Thread Starter
    Member SeaCapnJ's Avatar
    Join Date
    Jun 2003
    Location
    Wilmington, NC
    Posts
    41

    Question [RESOLVED]Batch Table Insert

    I need to do batch inserts with my datatable. Is there a simple way to do this? I have filled my datatable with new rows, but How can I update the Oracle database? The Dataadapter properiy "update" only seems to do one line at a time. What is the best way to do this? Any links or resources would be much appreciated.

    Thanks
    Last edited by SeaCapnJ; May 27th, 2004 at 11:27 AM.

  2. #2
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477

    Cool

    Have you considered using the UpdateBatch method?

  3. #3
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Sorry for that, there is no UpdateBatch method.
    One of the problems with boucing between platforms is forgetting which methods apply to which platform.
    But in your post you stated that the update method only updates one row at a time. This is simply not true. The update method will update all changes made to a dataset or datatable.

  4. #4

    Thread Starter
    Member SeaCapnJ's Avatar
    Join Date
    Jun 2003
    Location
    Wilmington, NC
    Posts
    41
    I was searching for the batchupdate, thinking how could I have missed that! Thanks for such a quick reply and the correction.

    In regards the .update only updating one record at a time, let me be more specific.

    .update is not neccessarily the problem. The Data adapter takes a parameter of command. When I insert one line at a time I give the Command connection string the equivalent sql string ('insert into ....).

    However, Now that I need to insert more than one row, I am not sure how to have a dynamic command connection string. If that is even what I am supposed to do.

    I am just trying to figure out what the right direction to go is.

    Thanks!

  5. #5
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    I hope I cleaned this up completely, it is from a C# project I am working on.
    VB Code:
    1. 'Declarations Section
    2. Private m_daJobsInfo As SqlDataAdapter = New SqlDataAdapter
    3. Private m_cmdSelectJobsInfo As SqlCommand = New SqlCommand
    4. Private m_cmdInsertNewJob As SqlCommand = New SqlCommand
    5. Private m_cmdUpdateJobStatus As SqlCommand = New SqlCommand
    6. Private m_cmdDeleteJob As SqlCommand = New SqlCommand
    7. Private m_dtJobs As DataTable = New DataTable
    8.  
    9. Private Sub Form_Load()
    10.     Me.m_cmdSelectJobsInfo.CommandText = "SELECT Job_ID, Job_Type_ID, LocationCode, CountryName, Status, Server, EnteredBy, EnteredDate, FirstRunDate, Notes FROM FOR_Jobs_List WHERE MONTH(EnteredDate) = MONTH(GETDATE()) AND DAY(EnteredDate) = DAY(GETDATE()) AND YEAR(EnteredDate) = YEAR(GETDATE()) AND Status = 'Not Started' ORDER BY Job_ID"
    11.     Me.m_cmdSelectJobsInfo.Connection = new SqlConnection(string.Format(m_connectInfo,m_serverName,m_jobsDB))
    12.     Me.m_cmdInsertNewJob.CommandText = "INSERT INTO FOR_Jobs_List (Job_ID, Job_Type_ID, LocationCode, CountryName, Status, Server, EnteredBy, EnteredDate, FirstRunDate, Notes) VALUES(@Job_Type_ID,@LocationCode,@CountryName,@Status,@Server,@EnteredBy,@EnteredDate,@FirstRunDate,@Notes)"
    13.     Me.m_cmdInsertNewJob.Parameters.Add(new SqlParameter("@Job_Type_ID", SqlDbType.Int, 4, "Job_Type_ID"))
    14.     Me.m_cmdInsertNewJob.Parameters.Add(new SqlParameter("@LocationCode", SqlDbType.VarChar, 3, "LocationCode"))
    15.     Me.m_cmdInsertNewJob.Parameters.Add(new SqlParameter("@CountryName", SqlDbType.VarChar, 50, "CountryName"))
    16.     Me.m_cmdInsertNewJob.Parameters.Add(new SqlParameter("@Status", SqlDbType.VarChar, 100, "Status"))
    17.     Me.m_cmdInsertNewJob.Parameters.Add(new SqlParameter("@Server", SqlDbType.VarChar, 30, "Server"))
    18.     Me.m_cmdInsertNewJob.Parameters.Add(new SqlParameter("@EnteredBy", SqlDbType.VarChar, 50, "EnteredBy"))
    19.     Me.m_cmdInsertNewJob.Parameters.Add(new SqlParameter("@EnteredDate", SqlDbType.DateTime, 8, "EnteredDate"))
    20.     Me.m_cmdInsertNewJob.Parameters.Add(new SqlParameter("@FirstRunDate", SqlDbType.DateTime, 8, "FirstRunDate"))
    21.     Me.m_cmdInsertNewJob.Parameters.Add(new SqlParameter("@Notes", SqlDbType.VarChar, 200, "Notes"))
    22.     Me.m_cmdInsertNewJob.Parameters.Add(new SqlParameter("@Job_ID", SqlDbType.Int, 4).Direction=ParameterDirection.ReturnValue)
    23.     Me.m_cmdInsertNewJob.Connection = new SqlConnection(string.Format(m_connectInfo,m_serverName,m_jobsDB))
    24.     Me.m_cmdUpdateJobStatus.CommandText = "UPDATE FOR_Jobs_List SET Status = @Status WHERE (Job_ID = @Original_Job_ID)SELECT Job_ID, Job_Type_ID, LocationCode, CountryName, Status, Server, EnteredBy, EnteredDate, FirstRunDate, Notes FROM FOR_Jobs_List WHERE (Job_ID = @Job_ID)"
    25.     Me.m_cmdUpdateJobStatus.Parameters.Add(new SqlParameter("@Original_Job_ID", System.Data.SqlDbType.Int, 4, ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Job_ID", System.Data.DataRowVersion.Original, null))
    26.     Me.m_cmdUpdateJobStatus.Parameters.Add(new SqlParameter("@Job_ID", SqlDbType.Int, 4, "Job_Type_ID"))
    27.     Me.m_cmdUpdateJobStatus.Parameters.Add(new SqlParameter("@Status", SqlDbType.VarChar, 100, "Status"))
    28.     Me.m_cmdUpdateJobStatus.Connection = new SqlConnection(string.Format(m_connectInfo,m_serverName,m_jobsDB))
    29.     Me.m_cmdDeleteJob.CommandText = "DELETE FOR_Jobs_List WHERE (Job_ID = @Original_Job_ID)SELECT Job_ID, Job_Type_ID, LocationCode, CountryName, Status, Server, EnteredBy, EnteredDate, FirstRunDate, Notes FROM FOR_Jobs_List"
    30.     Me.m_cmdDeleteJob.Parameters.Add(new SqlParameter("@Original_Job_ID", System.Data.SqlDbType.Int, 4, ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Job_ID", System.Data.DataRowVersion.Original, null))
    31.     Me.m_cmdDeleteJob.Connection = new SqlConnection(string.Format(m_connectInfo,m_serverName,m_jobsDB))
    32.     Me.m_daJobsInfo.SelectCommand = Me.m_cmdSelectJobsInfo
    33.     Me.m_daJobsInfo.InsertCommand = Me.m_cmdInsertNewJob
    34.     Me.m_daJobsInfo.UpdateCommand = Me.m_cmdUpdateJobStatus
    35.     Me.m_daJobsInfo.DeleteCommand = Me.m_cmdDeleteJob
    36. End Sub
    37.  
    38. Public Sub CreateMulitplePostJob(_user As String,_jobTypeID As Int32)
    39.     Dim _cnTargetServer As SqlConnection = new SqlConnection(string.Format(m_connectInfo,m_serverName,m_dataDB))
    40.     Dim _cnTargetServer.Open
    41.     Dim _cmdActivePosts As SqlCommand = new SqlCommand("spSelActivePosts",_cnTargetServer)
    42.     Dim _drActivePosts As SqlDataReader
    43.     Try
    44.         m_daJobsInfo.Fill(m_dtJobs)
    45.         Dim _JobTypes As DataRow() = m_dtJobTypes.Select("Job_Type_ID = " + _jobTypeID)
    46.         Dim _description As String = _JobTypes(0)("Description").ToString
    47.         _drActivePosts = _cmdActivePosts.ExecuteReader
    48.         If _drActivePosts.HasRows = True Then
    49.             Do While _drActivePosts.Read
    50.                 Dim _loc As String = _drActivePosts["Location"].ToString
    51.                 Dim _country As String = _drActivePosts["Country"].ToString
    52.                 Dim _newJob As DataRow = m_dtJobs.NewRow
    53.                 _newJob["Job_Type_ID"] = _jobTypeID
    54.                 _newJob["LocationCode"] = _loc
    55.                 _newJob["CountryName"] = _country
    56.                 _newJob["Status"] = "Not Started"
    57.                 _newJob["Server"] = m_serverName
    58.                 _newJob["EnteredBy"] = _user
    59.                 _newJob["EnteredDate"] = DateTime.Now
    60.                 _newJob["Notes"] = "Testing"
    61.                 m_dtJobs.Rows.Add(_newJob)
    62.             Loop
    63.             m_daJobsInfo.Update(m_dtJobs)
    64.             m_dtJobs.Clear
    65.             dim _daJobs As SqlDataAdapter = New SqlDataAdapter("spselJobs",string.Format(m_connectInfo,m_serverName,m_jobsDB))
    66.             _daJobs.Fill(m_dtJobs)
    67.         End If
    68.     Catch(Exception ex)
    69.         MessageBox.Show(ex.ToString)
    70.     End Try
    71.     _cnTargetServer.Close
    72. End Sub

  6. #6
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    The above code works in my C# project and definitely allows you to Insert, Update, and Delete several records in your DataTable before calling the DataAdapter.Update method.

    If you have problems with this let me know, I'll see what I can do to get it working

  7. #7

    Thread Starter
    Member SeaCapnJ's Avatar
    Join Date
    Jun 2003
    Location
    Wilmington, NC
    Posts
    41
    With your help I was able to get the bulk insert working. Unfortunately UpdateCommand is giving me a problem.
    The error message complains of an invalid number.

    Any thoughts are greatly appreciated.

    VB Code:
    1. Dim da As New Oracle.DataAccess.Client.OracleDataAdapter
    2.  
    3.         da.InsertCommand = New Oracle.DataAccess.Client.OracleCommand _
    4.             ("INSERT INTO getap_input_values" _
    5.             & "(ID,CID,BID,UDID,PSID,GSID,USID,DIMENSION1,DIMENSION2,DIMENSION3,DIMENSION4,DATAVALUE,NAME,REASON)VALUES" _
    6.             & "(GETAP_INPUT_VALUES_SEQ.nextval,:iCID,:iBID,:iUDID,:iPSID,:iGSID,:iUSID" _
    7.             & ",:iDIMENSION1,:iDIMENSION2,:iDIMENSION3,:iDIMENSION4,:iDATAVALUE,:iNAME,:iREASON)", epromdevConnection)
    8.  
    9.         da.InsertCommand.Parameters.Add("iCID", Oracle.DataAccess.Client.OracleDbType.Int16, 4, "CID")
    10.         da.InsertCommand.Parameters.Add("iBID", Oracle.DataAccess.Client.OracleDbType.Int16, 0, "BID")
    11.         da.InsertCommand.Parameters.Add("iUDID", Oracle.DataAccess.Client.OracleDbType.Int16, 0, "UDID")
    12.         da.InsertCommand.Parameters.Add("iPSID", Oracle.DataAccess.Client.OracleDbType.Int16, 0, "PSID")
    13.         da.InsertCommand.Parameters.Add("iGSID", Oracle.DataAccess.Client.OracleDbType.Int16, 0, "GSID")
    14.         da.InsertCommand.Parameters.Add("iUSID", Oracle.DataAccess.Client.OracleDbType.Int16, 0, "USID")
    15.         da.InsertCommand.Parameters.Add("iDIMENSION1", Oracle.DataAccess.Client.OracleDbType.Int16, 0, "DIMENSION1")
    16.         da.InsertCommand.Parameters.Add("iDIMENSION2", Oracle.DataAccess.Client.OracleDbType.Int16, 0, "DIMENSION2")
    17.         da.InsertCommand.Parameters.Add("iDIMENSION3", Oracle.DataAccess.Client.OracleDbType.Int16, 0, "DIMENSION3")
    18.         da.InsertCommand.Parameters.Add("iDIMENSION4", Oracle.DataAccess.Client.OracleDbType.Int16, 0, "DIMENSION4")
    19.         da.InsertCommand.Parameters.Add("iDATAVALUE", Oracle.DataAccess.Client.OracleDbType.Varchar2, 200, "DATAVALUE")
    20.         da.InsertCommand.Parameters.Add("iNAME", Oracle.DataAccess.Client.OracleDbType.Varchar2, 50, "NAME")
    21.         da.InsertCommand.Parameters.Add("iREASON", Oracle.DataAccess.Client.OracleDbType.Varchar2, 500, "REASON")
    22.  
    23.  
    24.         da.UpdateCommand = New Oracle.DataAccess.Client.OracleCommand _
    25.             ("UPDATE getap_input_values SET DATAVALUE = :iDATAVALUE WHERE CID = :iCID", epromdevConnection)
    26.        
    27.  
    28.      da.UpdateCommand.Parameters.Add("iCID", Oracle.DataAccess.Client.OracleDbType.Int16, 0, "CID")
    29.      da.UpdateCommand.Parameters.Add("iDATAVALUE", Oracle.DataAccess.Client.OracleDbType.Varchar2, 200, "DATAVALUE")
    30.  
    31.  
    32.         Try
    33.             da.Update(tblIptVal)
    34.         Catch e As Exception
    35.             MsgBox(e.ToString)
    36.             MsgBox(e)
    37.         End Try

  8. #8

    Thread Starter
    Member SeaCapnJ's Avatar
    Join Date
    Jun 2003
    Location
    Wilmington, NC
    Posts
    41

    further weirdness

    Ok, to add more to the weirdness. I can have one variable (:iCID or :iDATAVALUE) in the command and it will work. For reason setting the variable and using a variable past where do not work.
    Last edited by SeaCapnJ; May 26th, 2004 at 03:20 PM.

  9. #9
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Ahhh.. I see your problem . . . Oracle, now you should know Microsoft doesn't play well with others
    (Bill please don't take my cert away)

    But seriously, the problem is you are not referring to your orginal row identity when updating your dataset.
    You see what actually happens when you perform an update is, the datatable creates a new row that references your original row, the new row contains the changes, the original row is marked for update. This is how rollbacks are supported in the event you don't want to commit your changes to your datasource.

    If you peruse my code a little more closely, you will see that part of the code in the parameter for the rowid makes a reference to the original rowid.

    I don't personally know the datatypes and references for the Oracle client, but I'm sure they are not much different from the one for the SqlClient.

    Let me know if this doesn't work, I do have a few Oracle servers laying around the office I can play with

  10. #10

    Thread Starter
    Member SeaCapnJ's Avatar
    Join Date
    Jun 2003
    Location
    Wilmington, NC
    Posts
    41

    Solved

    Order matters.

    I simply had to declare the DATAVALUE parameter before any of the other parameters, and BOOM succesful! I will keep an eye out for that row version stuff. Thanks so much Hawke!

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