SqlBulkCopy.WriteToServer Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Aşırı Yüklemeler
| WriteToServer(DbDataReader) |
Sağlanan DbDataReader dizideki tüm satırları nesnenin DestinationTableName özelliği tarafından belirtilen hedef tabloya SqlBulkCopy kopyalar. |
| WriteToServer(DataRow[]) |
Sağlanan DataRow dizideki tüm satırları nesnenin DestinationTableName özelliği tarafından belirtilen hedef tabloya SqlBulkCopy kopyalar. |
| WriteToServer(DataTable) |
Sağlanan DataTable içindeki tüm satırları nesnenin DestinationTableName özelliği tarafından belirtilen hedef tabloya SqlBulkCopy kopyalar. |
| WriteToServer(IDataReader) |
Sağlanan IDataReader içindeki tüm satırları nesnenin DestinationTableName özelliği tarafından belirtilen hedef tabloya SqlBulkCopy kopyalar. |
| WriteToServer(DataTable, DataRowState) |
Yalnızca sağlanan DataTable içindeki sağlanan satır durumuyla eşleşen satırları nesnenin özelliği tarafından DestinationTableName belirtilen hedef tabloya SqlBulkCopy kopyalar. |
WriteToServer(DbDataReader)
Sağlanan DbDataReader dizideki tüm satırları nesnenin DestinationTableName özelliği tarafından belirtilen hedef tabloya SqlBulkCopy kopyalar.
public:
void WriteToServer(System::Data::Common::DbDataReader ^ reader);
public void WriteToServer (System.Data.Common.DbDataReader reader);
member this.WriteToServer : System.Data.Common.DbDataReader -> unit
Public Sub WriteToServer (reader As DbDataReader)
Parametreler
- reader
- DbDataReader
DbDataReader Satırları hedef tabloya kopyalanacak olan.
Özel durumlar
geçerli SqlBulkCopyColumnOrderHint bir hedef sütun adı belirtmedi.
Şunlara uygulanır
WriteToServer(DataRow[])
Sağlanan DataRow dizideki tüm satırları nesnenin DestinationTableName özelliği tarafından belirtilen hedef tabloya SqlBulkCopy kopyalar.
public:
void WriteToServer(cli::array <System::Data::DataRow ^> ^ rows);
public void WriteToServer (System.Data.DataRow[] rows);
member this.WriteToServer : System.Data.DataRow[] -> unit
Public Sub WriteToServer (rows As DataRow())
Parametreler
Özel durumlar
geçerli SqlBulkCopyColumnOrderHint bir hedef sütun adı belirtmedi.
Örnekler
Aşağıdaki konsol uygulaması, bir DataRow diziden toplu veri yükleme işlemini gösterir. Hedef tablo AdventureWorks veritabanındaki bir tablodur.
Bu örnekte, çalışma zamanında bir DataTable oluşturulur. Hedef tabloya kopyalamak için öğesinden DataTable tek bir satır seçilir.
Önemli
İş tablolarını Toplu Kopyalama Örneği Kurulumu'nda açıklandığı gibi oluşturmadığınız sürece bu örnek çalışmaz. Bu kod, yalnızca SqlBulkCopy kullanımına yönelik söz dizimini göstermek için sağlanır. Kaynak ve hedef tablolar aynı SQL Server örnekteyse, verileri kopyalamak için Transact-SQL INSERT … SELECT deyimi kullanmak daha kolay ve daha hızlıdır.
Açıklamalar
Toplu kopyalama işlemi devam ederken, ilişkili hedef SqlConnection onu sunmayla meşgul ve bağlantı üzerinde başka işlem gerçekleştirilemez.
Koleksiyon, ColumnMappings sütunlardan DataRow hedef veritabanı tablosuna eşler.
Şunlara uygulanır
WriteToServer(DataTable)
Sağlanan DataTable içindeki tüm satırları nesnenin DestinationTableName özelliği tarafından belirtilen hedef tabloya SqlBulkCopy kopyalar.
public:
void WriteToServer(System::Data::DataTable ^ table);
public void WriteToServer (System.Data.DataTable table);
member this.WriteToServer : System.Data.DataTable -> unit
Public Sub WriteToServer (table As DataTable)
Parametreler
Özel durumlar
geçerli SqlBulkCopyColumnOrderHint bir hedef sütun adı belirtmedi.
Örnekler
Aşağıdaki Konsol uygulaması, bir DataTable'den toplu veri yükleme işlemini gösterir. Hedef tablo AdventureWorks veritabanındaki bir tablodur.
Bu örnekte, çalışma zamanında bir DataTable oluşturulur ve işlemin kaynağıdır SqlBulkCopy .
Önemli
İş tablolarını Toplu Kopyalama Örneği Kurulumu'nda açıklandığı gibi oluşturmadığınız sürece bu örnek çalışmaz.
Bu kod, yalnızca SqlBulkCopy kullanımına yönelik söz dizimini göstermek için sağlanır. Kaynak ve hedef tablolar aynı SQL Server örnekteyse, verileri kopyalamak için Transact-SQL INSERT … SELECT deyimi kullanmak daha kolay ve daha hızlıdır.
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
string connectionString = GetConnectionString();
// Open a connection to the AdventureWorks database.
using (SqlConnection connection =
new SqlConnection(connectionString))
{
connection.Open();
// Perform an initial count on the destination table.
SqlCommand commandRowCount = new SqlCommand(
"SELECT COUNT(*) FROM " +
"dbo.BulkCopyDemoMatchingColumns;",
connection);
long countStart = System.Convert.ToInt32(
commandRowCount.ExecuteScalar());
Console.WriteLine("Starting row count = {0}", countStart);
// Create a table with some rows.
DataTable newProducts = MakeTable();
// Create the SqlBulkCopy object.
// Note that the column positions in the source DataTable
// match the column positions in the destination table so
// there is no need to map columns.
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
{
bulkCopy.DestinationTableName =
"dbo.BulkCopyDemoMatchingColumns";
try
{
// Write from the source to the destination.
bulkCopy.WriteToServer(newProducts);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
// 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 DataTable MakeTable()
// Create a new DataTable named NewProducts.
{
DataTable newProducts = new DataTable("NewProducts");
// Add three column objects to the table.
DataColumn productID = new DataColumn();
productID.DataType = System.Type.GetType("System.Int32");
productID.ColumnName = "ProductID";
productID.AutoIncrement = true;
newProducts.Columns.Add(productID);
DataColumn productName = new DataColumn();
productName.DataType = System.Type.GetType("System.String");
productName.ColumnName = "Name";
newProducts.Columns.Add(productName);
DataColumn productNumber = new DataColumn();
productNumber.DataType = System.Type.GetType("System.String");
productNumber.ColumnName = "ProductNumber";
newProducts.Columns.Add(productNumber);
// Create an array for DataColumn objects.
DataColumn[] keys = new DataColumn[1];
keys[0] = productID;
newProducts.PrimaryKey = keys;
// Add some new rows to the collection.
DataRow row = newProducts.NewRow();
row["Name"] = "CC-101-WH";
row["ProductNumber"] = "Cyclocomputer - White";
newProducts.Rows.Add(row);
row = newProducts.NewRow();
row["Name"] = "CC-101-BK";
row["ProductNumber"] = "Cyclocomputer - Black";
newProducts.Rows.Add(row);
row = newProducts.NewRow();
row["Name"] = "CC-101-ST";
row["ProductNumber"] = "Cyclocomputer - Stainless";
newProducts.Rows.Add(row);
newProducts.AcceptChanges();
// Return the new DataTable.
return newProducts;
}
private static string GetConnectionString()
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
{
return "Data Source=(local); " +
" Integrated Security=true;" +
"Initial Catalog=AdventureWorks;";
}
}
Açıklamalar
içindeki DataTable tüm satırlar, silinmiş olanlar dışında hedef tabloya kopyalanır.
Toplu kopyalama işlemi devam ederken, ilişkili hedef SqlConnection onu sunmayla meşgul ve bağlantı üzerinde başka işlem gerçekleştirilemez.
Koleksiyon, ColumnMappings sütunlardan DataTable hedef veritabanı tablosuna eşler.
Ayrıca bkz.
Şunlara uygulanır
WriteToServer(IDataReader)
Sağlanan IDataReader içindeki tüm satırları nesnenin DestinationTableName özelliği tarafından belirtilen hedef tabloya SqlBulkCopy kopyalar.
public:
void WriteToServer(System::Data::IDataReader ^ reader);
public void WriteToServer (System.Data.IDataReader reader);
member this.WriteToServer : System.Data.IDataReader -> unit
Public Sub WriteToServer (reader As IDataReader)
Parametreler
- reader
- IDataReader
IDataReader Satırları hedef tabloya kopyalanacak olan.
Özel durumlar
geçerli SqlBulkCopyColumnOrderHint bir hedef sütun adı belirtmedi.
Örnekler
Aşağıdaki konsol uygulaması, bir SqlDataReader'den toplu veri yükleme işlemini gösterir. Hedef tablo AdventureWorks veritabanındaki bir tablodur.
Önemli
İş tablolarını Toplu Kopyalama Örneği Kurulumu'nda açıklandığı gibi oluşturmadığınız sürece bu örnek çalışmaz. Bu kod, yalnızca SqlBulkCopy kullanımına yönelik söz dizimini göstermek için sağlanır. Kaynak ve hedef tablolar aynı SQL Server örnekteyse, verileri kopyalamak için Transact-SQL INSERT … SELECT deyimi kullanmak daha kolay ve daha hızlıdır.
using Microsoft.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();
// Set up the bulk copy object using a connection string.
// In the real world you would not use SqlBulkCopy to move
// data from one table to the other in the same database.
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(connectionString))
{
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çıklamalar
Kopyalama işlemi okuyucuda bir sonraki kullanılabilir satırda başlar. Çoğu zaman okuyucu yalnızca veya benzer bir çağrı tarafından ExecuteReader() döndürülür, bu nedenle sonraki kullanılabilir satır ilk satırdır. Birden çok sonucu işlemek için veri okuyucuyu arayın NextResult() ve yeniden çağırın WriteToServer .
kullanmanın WriteToServer okuyucunun durumunu değiştirdiğini unutmayın. yöntemi false döndürene, işlem durdurulana veya bir hata oluşana kadar çağrısı Read() yapacaktır. Bu, işlem tamamlandığında veri okuyucunun büyük olasılıkla sonuç kümesinin WriteToServer sonunda farklı bir durumda olacağı anlamına gelir.
Toplu kopyalama işlemi devam ederken, ilişkili hedef SqlConnection onu sunmayla meşgul ve bağlantı üzerinde başka işlem gerçekleştirilemez.
Koleksiyon, ColumnMappings veri okuyucu sütunlarından hedef veritabanı tablosuna eşler.
Şunlara uygulanır
WriteToServer(DataTable, DataRowState)
Yalnızca sağlanan DataTable içindeki sağlanan satır durumuyla eşleşen satırları nesnenin özelliği tarafından DestinationTableName belirtilen hedef tabloya SqlBulkCopy kopyalar.
public:
void WriteToServer(System::Data::DataTable ^ table, System::Data::DataRowState rowState);
public void WriteToServer (System.Data.DataTable table, System.Data.DataRowState rowState);
member this.WriteToServer : System.Data.DataTable * System.Data.DataRowState -> unit
Public Sub WriteToServer (table As DataTable, rowState As DataRowState)
Parametreler
- rowState
- DataRowState
Numaralandırmadan bir DataRowState değer. Yalnızca satır durumuyla eşleşen satırlar hedefe kopyalanır.
Özel durumlar
geçerli SqlBulkCopyColumnOrderHint bir hedef sütun adı belirtmedi.
Örnekler
Aşağıdaki Konsol uygulaması, yalnızca belirtilen durumla eşleşen bir DataTable içindeki satırların toplu olarak nasıl yüklendiğini gösterir. Bu durumda, yalnızca değişmeyen satırlar eklenir. Hedef tablo AdventureWorks veritabanındaki bir tablodur.
Bu örnekte, çalışma zamanında bir DataTable oluşturulur ve buna üç satır eklenir.
WriteToServer Yöntem yürütülmeden önce satırlardan biri düzenlenir.
WriteToServer yöntemi bir DataRowState.UnchangedrowState bağımsız değişkenle çağrılır, bu nedenle yalnızca iki değişmemiş satır hedefe toplu olarak kopyalanır.
Önemli
İş tablolarını Toplu Kopyalama Örneği Kurulumu'nda açıklandığı gibi oluşturmadığınız sürece bu örnek çalışmaz. Bu kod, yalnızca SqlBulkCopy kullanımına yönelik söz dizimini göstermek için sağlanır. Kaynak ve hedef tablolar aynı SQL Server örnekteyse, verileri kopyalamak için Transact-SQL INSERT … SELECT deyimi kullanmak daha kolay ve daha hızlıdır.
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
string connectionString = GetConnectionString();
// Open a connection to the AdventureWorks database.
using (SqlConnection connection =
new SqlConnection(connectionString))
{
connection.Open();
// Perform an initial count on the destination table.
SqlCommand commandRowCount = new SqlCommand(
"SELECT COUNT(*) FROM " +
"dbo.BulkCopyDemoMatchingColumns;",
connection);
long countStart = System.Convert.ToInt32(
commandRowCount.ExecuteScalar());
Console.WriteLine("Starting row count = {0}", countStart);
// Create a table with some rows.
DataTable newProducts = MakeTable();
// Make a change to one of the rows in the DataTable.
DataRow row = newProducts.Rows[0];
row.BeginEdit();
row["Name"] = "AAA";
row.EndEdit();
// Create the SqlBulkCopy object.
// Note that the column positions in the source DataTable
// match the column positions in the destination table so
// there is no need to map columns.
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
{
bulkCopy.DestinationTableName =
"dbo.BulkCopyDemoMatchingColumns";
try
{
// Write unchanged rows from the source to the destination.
bulkCopy.WriteToServer(newProducts, DataRowState.Unchanged);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
// 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 DataTable MakeTable()
// Create a new DataTable named NewProducts.
{
DataTable newProducts = new DataTable("NewProducts");
// Add three column objects to the table.
DataColumn productID = new DataColumn();
productID.DataType = System.Type.GetType("System.Int32");
productID.ColumnName = "ProductID";
productID.AutoIncrement = true;
newProducts.Columns.Add(productID);
DataColumn productName = new DataColumn();
productName.DataType = System.Type.GetType("System.String");
productName.ColumnName = "Name";
newProducts.Columns.Add(productName);
DataColumn productNumber = new DataColumn();
productNumber.DataType = System.Type.GetType("System.String");
productNumber.ColumnName = "ProductNumber";
newProducts.Columns.Add(productNumber);
// Create an array for DataColumn objects.
DataColumn[] keys = new DataColumn[1];
keys[0] = productID;
newProducts.PrimaryKey = keys;
// Add some new rows to the collection.
DataRow row = newProducts.NewRow();
row["Name"] = "CC-101-WH";
row["ProductNumber"] = "Cyclocomputer - White";
newProducts.Rows.Add(row);
row = newProducts.NewRow();
row["Name"] = "CC-101-BK";
row["ProductNumber"] = "Cyclocomputer - Black";
newProducts.Rows.Add(row);
row = newProducts.NewRow();
row["Name"] = "CC-101-ST";
row["ProductNumber"] = "Cyclocomputer - Stainless";
newProducts.Rows.Add(row);
newProducts.AcceptChanges();
// Return the new DataTable.
return newProducts;
}
private static string GetConnectionString()
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
{
return "Data Source=(local); " +
" Integrated Security=true;" +
"Initial Catalog=AdventureWorks;";
}
}
Açıklamalar
Yalnızca bağımsız değişkende belirtilen rowState durumlarda olan ve silinmemiş olan içindeki satırlar DataTable hedef tabloya kopyalanır.
Not
Belirtilirse Deleted , herhangi bir Unchanged, Addedve Modified satırı da sunucuya kopyalanır. Özel durum oluşturulmayacak.
Toplu kopyalama işlemi devam ederken, ilişkili hedef SqlConnection onu sunmayla meşgul ve bağlantı üzerinde başka işlem gerçekleştirilemez.
Koleksiyon, ColumnMappings sütunlardan DataTable hedef veritabanı tablosuna eşler.