mình có 1 database ,trong đó có 1 table là customer,Trong table có các cột là ID, Name, Country. và cũng có dữ liệu trong table Customer. ID đc đánh thứ tự
1.Giờ mình muốn bổ xung cho table Customer từ tập tin .CSV Thì làm như thế nào?
2.nếu trong tập tin .CSV có mã ID trùng với mã ID table Customer thì mã ID mới sẽ ghi đè lên ID cũ
-đây là code cửa mình. nhưng mình chưa làm đc phần 2.
Mã:
SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Test-ImportCSV;Integrated Security=True");
string filepath = @"C:\Users\Administrator.6BLWDJYY704TJLS\Desktop\SqlBulkCopy_CSV_Insert\Files\Sample.csv";
StreamReader sr = new StreamReader(filepath);
string line = sr.ReadLine();
string[] value = line.Split(',');
DataTable dt = new DataTable();
DataRow row;
foreach (string dc in value)
{
dt.Columns.Add(new DataColumn(dc));
}
dataGridView1.DataSource = dt;
while (!sr.EndOfStream)
{
value = sr.ReadLine().Split(',');
if (value.Length == dt.Columns.Count)
{
row = dt.NewRow();
row.ItemArray = value;
dt.Rows.Add(row);
}
}
SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
bc.DestinationTableName = "Customer";
bc.BatchSize = dt.Rows.Count;
con.Open();
bc.WriteToServer(dt);
bc.Close();
con.Close();
View more random threads:
Bất chấp những lầm tưởng phổ quát, hồ hết những người có âm đạo đều khó lên đỉnh khi bị kích thích âm đạo. Tuy nhiên, điều đó không có tức thị nó không thể vui được! Việc xâm nhập vào âm đạo bằng...
Quý bà giải tỏa bằng việc kích...