有什么方法可以效率比较高点的解决啊?
用循环insert与adapter.Update() 对比的话,
哪一个的效率会稍微好点?
有没有其他的解决方案?
用循环insert与adapter.Update() 对比的话,
哪一个的效率会稍微好点?
有没有其他的解决方案?
解决方案 »
- ASP.NET 网页数据抓取
- Xml中的 xmltextwrite,xmlwrite区别
- 望高手解决,文章自动分页问题,不分割HTML标签。。。。
- 自己开发的表格控件,支持Web页面上精确票据套打和智能脚本计算,高人进来鉴定
- 在.NET 中的一个小问题。。。。帮解决一下!!!
- 请教一个一直未解决的问题
- Repeater调用Sql存储过程+数字分页怎么这么慢
- ASP.NET网站发布问题
- 我想用一个BUTTON打开一个链接,但是出现XueGong为空不是对象 是提示错误
- 有人有ASP.NET Documentation Tool的注册码吗??
- 发布我的个人网站源码:MyHome2009
- 在SQL2005中附加数据库失败
//插入操作
}using能及时释放‘稀缺’资源,如数据库链接资源,提高资源利用率
Private Sub CopyData()
Dim OriginCon, DestinationCon As New SqlConnection()
Dim OriginCmd, DestinationCmd As New SqlCommand()
Dim Reader As SqlDataReader OriginCon.ConnectionString = _conStr
DestinationCon.ConnectionString = _conStr OriginCmd.Connection = OriginCon
OriginCmd.CommandText = "select * from customers"
OriginCmd.CommandType = CommandType.Text OriginCon.Open() Dim NorthwindBulk As SqlBulkCopy
NorthwindBulk = New SqlBulkCopy(_conStr, SqlBulkCopyOptions.UseInternalTransaction)
NorthwindBulk.DestinationTableName = "Cu"
NorthwindBulk.ColumnMappings.Add("CustomerID", "CustomerID")
NorthwindBulk.ColumnMappings.Add("CompanyName", "CompanyName")
NorthwindBulk.ColumnMappings.Add("ContactName", "ContactName")
NorthwindBulk.ColumnMappings.Add("ContactTitle", "ContactTitle")
NorthwindBulk.ColumnMappings.Add("Address", "Address")
NorthwindBulk.ColumnMappings.Add("City", "City") NorthwindBulk.ColumnMappings.Add("Region", "Region")
NorthwindBulk.ColumnMappings.Add("PostalCode", "PostalCode")
NorthwindBulk.ColumnMappings.Add("Country", "Country")
NorthwindBulk.ColumnMappings.Add("Phone", "Phone")
'NorthwindBulk.ColumnMappings.Add("Fax", "Fax") Dim Map As SqlBulkCopyColumnMapping = New SqlBulkCopyColumnMapping("Fax", "Fax") NorthwindBulk.ColumnMappings.Add(Map)
NorthwindBulk.BulkCopyTimeout = 99999999
NorthwindBulk.NotifyAfter = 1000 Reader = OriginCmd.ExecuteReader() Try
NorthwindBulk.WriteToServer(Reader)
Catch ex As Exception End Try End Sub Private Sub BatchUpdate()
'Submit the updates in batches by setting the UpdateBatchSize property 'Retrieve all products in a category
'Discount the unit pice 10% for all products that are not discontinued
'Submit all changes (in a System.Transaction that will roll back) Dim tbl As New DataTable() Dim strSQL As String = "SELECT ProductID, ProductName, UnitPrice, Discontinued FROM Products"
Dim da As New SqlDataAdapter()
Dim cb As New SqlCommandBuilder(da) Using cn As New SqlConnection(_conStr)
da.SelectCommand = New SqlCommand(strSQL, cn)
da.Fill(tbl)
End Using For Each row As DataRow In tbl.Select("Discontinued = 0")
row("UnitPrice") = CDec(row("UnitPrice")) * 0.9
Next strSQL = "UPDATE Products SET ProductName = @ProductName_New, UnitPrice = @UnitPrice_New " & _
" WHERE @ProductID_Old = ProductID AND @ProductName_Old = ProductName AND " & _
" (@UnitPrice_Old = UnitPrice OR (@UnitPrice_Null = 1 AND UnitPrice IS NULL))"
da.UpdateCommand = New SqlCommand(strSQL)
da.UpdateCommand.Parameters.Add("@ProductName_New", SqlDbType.NVarChar, 40, "ProductName")
da.UpdateCommand.Parameters.Add("@UnitPrice_New", SqlDbType.Money, 0, "UnitPrice")
da.UpdateCommand.Parameters.Add("@ProductID_Old", SqlDbType.Int, 0, "ProductID").SourceVersion = DataRowVersion.Original
da.UpdateCommand.Parameters.Add("@ProductName_Old", SqlDbType.NVarChar, 40, "ProductName").SourceVersion = DataRowVersion.Original
da.UpdateCommand.Parameters.Add("@UnitPrice_Old", SqlDbType.Money, 0, "UnitPrice").SourceVersion = DataRowVersion.Original
With da.UpdateCommand.Parameters.Add("@UnitPrice_Null", SqlDbType.Int, 0, "UnitPrice")
.SourceVersion = DataRowVersion.Original
.SourceColumnNullMapping = True
End With
da.UpdateCommand.UpdatedRowSource = UpdateRowSource.None da.UpdateBatchSize = 25
da.ContinueUpdateOnError = True 'Wrap the changes in a transaction that will be rolled back implicitly
'This is done to make sure you can run the sample without actually changing the database
Using txn As New TransactionScope()
Using cn As New SqlConnection(_conStr)
da.SelectCommand.Connection = cn
da.UpdateCommand.Connection = cn
da.Update(tbl)
End Using
'Uncomment this line to commit the transaction
'txn.Complete()
End Using
End Sub
Dim strSQL As String
'Clean out the contents of the destination table
Using cnDestination As New SqlConnection(_conStr)
cnDestination.Open()
Dim cmdDestinationPrepTable As New SqlCommand("DELETE FROM BCP_Demo", cnDestination)
Try
cmdDestinationPrepTable.ExecuteNonQuery()
Catch
strSQL = "CREATE TABLE BCP_Demo (EmployeeID int identity PRIMARY KEY, LastName nvarchar(40) NOT NULL, FirstName nvarchar(10) NOT NULL, Title nvarchar(30), ReportsTo int REFERENCES BCP_Demo(EmployeeID))"
cmdDestinationPrepTable.CommandText = strSQL
cmdDestinationPrepTable.ExecuteNonQuery()
End Try
cnDestination.Close()
End Using
Using cnSource As New SqlConnection(_conStr), _
cnDestination As New SqlConnection(_conStr) 'Execute a query against the source database
'Close the connection as soon as the reader is closed
strSQL = "SELECT EmployeeID, Title, LastName, FirstName, ReportsTo FROM Employees"
Dim cmdSource As New SqlCommand(strSQL, cnSource)
cnSource.Open()
Using rdrSource As SqlDataReader = cmdSource.ExecuteReader(CommandBehavior.CloseConnection)
Dim dt As New DataTable()
dt.Load(rdrSource)
'Wrap the work in a transaction
cnDestination.Open()
Using txn As SqlTransaction = cnDestination.BeginTransaction() 'Console.WriteLine(" Writing data to server...")
'Create a BCP object that will send rows to the destination table
Using bulkcopy As New SqlBulkCopy(cnDestination, SqlBulkCopyOptions.KeepIdentity Or SqlBulkCopyOptions.CheckConstraints, txn)
'Specify the name of the destination table
bulkcopy.DestinationTableName = "BCP_Demo" 'Columns are mapped from the source stream to the destination table by ordinal
'In this case, the order of columns in the source query does not match the order of columns in the destination table
'So, we can use the ColumnMappings to map based on their names, as shown here
bulkcopy.ColumnMappings.Add("EmployeeID", "EmployeeID")
bulkcopy.ColumnMappings.Add("LastName", "LastName")
bulkcopy.ColumnMappings.Add("FirstName", "FirstName")
bulkcopy.ColumnMappings.Add("Title", "Title")
bulkcopy.ColumnMappings.Add("ReportsTo", "ReportsTo") 'Mapping columns based on their ordinals is also an option, as shown here
'bulkcopy.ColumnMappings.Add(0, 0)
'bulkcopy.ColumnMappings.Add(2, 1)
'bulkcopy.ColumnMappings.Add(3, 2)
'bulkcopy.ColumnMappings.Add(1, 3)
'bulkcopy.ColumnMappings.Add(4, 4) 'Pass the source data reader to the BCP object to write the data to the server
bulkcopy.WriteToServer(dt) bulkcopy.Close()
End Using 'Commit the transaction
txn.Commit()
End Using rdrSource.Close()
End Using
'Now display the contents of the destination table
Response.Write(" Contents of the destination table <br />")
Dim cmdDestination As New SqlCommand("SELECT EmployeeID, LastName + ', ' + FirstName, Title, ReportsTo FROM BCP_Demo", cnDestination)
Using rdrDestination As SqlDataReader = cmdDestination.ExecuteReader()
While rdrDestination.Read
Dim objValues(3) As Object
rdrDestination.GetValues(objValues)
Response.Write(String.Format(" ID:{0} Name:{1,-17} Title:{2,-24} ReportsTo:{3}", objValues(0), objValues(1), objValues(2), IIf(objValues(3) Is DBNull.Value, "<Null>", objValues(3))))
End While
End Using cnDestination.Close()
End Using
End Sub