Concerning Add multiple data
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.
精彩评论