我想添加多个数据,是否有更快的方式。
我的意思是系统的速度。
感谢
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
块引用
答案 0 :(得分:0)
输入多个数据的最佳方法是使用XML。将xml文件传递给存储过程到DB并通过sp插入而不是代码中的内联查询。你可以看到完整example here
答案 1 :(得分:0)
在前端,创建一个DataTable并将要插入的记录添加到DataTable中。然后执行以下步骤:
DataSet ds = new DataSet();
dt.TableName = "AddTable"; //dt is the DataTable you have Created to Insert
ds.Tables.Add(dt);
将此DataSet作为参数传递给Insert SP,如
ds.GetXml();
此方法将以更快的方式插入记录。