C# 如何用C编写游标SQL代码
我有一个C应用程序中的代码,可以将特定记录从一个表传输到另一个表 但是我的第一个表有大约28000条记录,要将其传输到第二个表需要很多时间。。。 执行此传输的最佳方式是什么? 这是我的完整代码:C# 如何用C编写游标SQL代码,c#,sql-server,linq,windows-applications,C#,Sql Server,Linq,Windows Applications,我有一个C应用程序中的代码,可以将特定记录从一个表传输到另一个表 但是我的第一个表有大约28000条记录,要将其传输到第二个表需要很多时间。。。 执行此传输的最佳方式是什么? 这是我的完整代码: private void btnsend_Click (object sender, EventArgs e) { List<QC> lstQc = new List<QC>(); lstQc = db.QCs.ToList(); foreach (va
private void btnsend_Click (object sender, EventArgs e)
{
List<QC> lstQc = new List<QC>();
lstQc = db.QCs.ToList();
foreach (var temp in lstQc)
{
TEST_PACKAGE objtestpackage = new TEST_PACKAGE();
if (temp.Package != "")
{
objtestpackage.DocumentNO = temp.Document_No;
if (temp.UNIT != "")
{
objtestpackage.Unit = Convert.ToInt32(temp.UNIT);
}
objtestpackage.Test_Package___No = temp.Package;
db.TEST_PACKAGE.Add(objtestpackage);
db.SaveChanges();
}
}
MessageBox.Show("tarnsfer info to test package succsefully!");
}
您可以使用普通ADO.NET和SqlBulkCopy,这是SqlBulkCopy中的示例,它比逐个复制要快得多:
using System.Data.SqlClient;
class Program
{
static void Main()
{
string connectionString = GetConnectionString();
// Open a sourceConnection to the AdventureWorks database.
using (SqlConnection sourceConnection =
new SqlConnection(connectionString))
{
sourceConnection.Open();
// Perform an initial count on the destination table.
SqlCommand commandRowCount = new SqlCommand(
"SELECT COUNT(*) FROM " +
"dbo.BulkCopyDemoMatchingColumns;",
sourceConnection);
long countStart = System.Convert.ToInt32(
commandRowCount.ExecuteScalar());
Console.WriteLine("Starting row count = {0}", countStart);
// Get data from the source table as a SqlDataReader.
SqlCommand commandSourceData = new SqlCommand(
"SELECT ProductID, Name, " +
"ProductNumber " +
"FROM Production.Product;", sourceConnection);
SqlDataReader reader =
commandSourceData.ExecuteReader();
// Open the destination connection. In the real world you would
// not use SqlBulkCopy to move data from one table to the other
// in the same database. This is for demonstration purposes only.
using (SqlConnection destinationConnection =
new SqlConnection(connectionString))
{
destinationConnection.Open();
// Set up the bulk copy object.
// Note that the column positions in the source
// data reader match the column positions in
// the destination table so there is no need to
// map columns.
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(destinationConnection))
{
bulkCopy.DestinationTableName =
"dbo.BulkCopyDemoMatchingColumns";
try
{
// Write from the source to the destination.
bulkCopy.WriteToServer(reader);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
// Close the SqlDataReader. The SqlBulkCopy
// object is automatically closed at the end
// of the using block.
reader.Close();
}
}
// Perform a final count on the destination
// table to see how many rows were added.
long countEnd = System.Convert.ToInt32(
commandRowCount.ExecuteScalar());
Console.WriteLine("Ending row count = {0}", countEnd);
Console.WriteLine("{0} rows were added.", countEnd - countStart);
Console.WriteLine("Press Enter to finish.");
Console.ReadLine();
}
}
}
private static string GetConnectionString()
// To avoid storing the sourceConnection string in your code,
// you can retrieve it from a configuration file.
{
return "Data Source=(local); " +
" Integrated Security=true;" +
"Initial Catalog=AdventureWorks;";
}
}
您可以有一个存储过程,它接受主键列表以标识需要传输的记录。您可以从表A中获取记录,并通过相同的过程插入到表B中 从C代码中调用此存储过程。这将比从C传输记录快得多 下面是如何将列表传递给存储过程:
您还可以将逗号指定的PK字符串作为参数传递,并在存储过程中拆分。99次/100次,您实际上不需要使用光标…..纯旧sql:insert table1从表2中选择字段1、字段2、字段3。您可以将其打包到SP中,然后从c代码执行它。