开发者

Concerning Add multiple data

开发者 https://www.devze.com 2023-03-13 19:14 出处:网络
I want to add multiple data,whether there is a faster way. I mean the speed of the system. thank Dim table = Dt

I want to add multiple data,whether there is a faster way.

I mean the speed of the system.

thank

    Dim table = Dt
    Dim strSqlInsert As String = ""

    If table.Rows.Count > 0 Then
        For i As Integer = 0 To table.Rows.Count - 1
           strSqlInsert += "INSERT INTO " & TableName & " VALUES ('" & Convert.ToString(table.Rows(i)("Name")) & "' , " & Convert.ToString(table.Rows(i)("Number")) & ") ; "
        Next
    End If

    Dim conn As SqlConnection = New SqlConnection(Web.Configuration.WebConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString)
    Dim cmd As New SqlCommand(strSqlInsert, conn)

    Dim odt As SqlTransaction = Nothing
    odt = conn.BeginTransaction(IsolationLevel.ReadUncommitted)
    cmd.Transaction = odt

    Try
        cmd.ExecuteNonQuery()
        odt.Commit()
    Catch ex As SqlException
        odt.Rollback()
        Response.Write(ex.ToString())
    Finally
        If cmd IsNot Nothing Then
            cmd.Dispose()
       开发者_开发技巧 End If
        If odt IsNot Nothing Then
            odt.Dispose()
            odt = Nothing
        End If
        If conn.State = ConnectionState.Open Then
            conn.Close()
        End If
        conn.Dispose()
    End Try

Blockquote


The best way to input multiple data is using XML. Pass xml files to stored procedure to DB and insert it through sp instead of inline query in code. You can see complete example here .


In the Front End, Create one DataTable and add the Records do you want to Insert into the DataTable. Then do the Steps below:

  DataSet ds = new DataSet();
  dt.TableName = "AddTable";        //dt is the DataTable you have Created to Insert
  ds.Tables.Add(dt);

And pass this DataSet as a Parameter to the Insert SP like

 ds.GetXml();

This method will insert the records in faster manner.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号