using System;
using System.Data;
using System.Data.SqlClient;
class Program
{
static void Main(string[] args)
{
string sqlConnectString = "Data Source=(local);Integrated security=SSPI;Initial Catalog=AdventureWorks;";
string sqlSelect = "SELECT ContactID, FirstName, LastName FROM Person.Contact";
SqlDataAdapter da = new SqlDataAdapter(sqlSelect, sqlConnectString);
DataTable dt1 = new DataTable();
da.Fill(dt1);
DataRow[] dra = new DataRow[dt1.Rows.Count];
dt1.Rows.CopyTo(dra, 0);
for (int i = 0; i < dra.Length; i++){
Console.WriteLine(
"ContactID = {0}\tFirstName = {1}\tLastName = {2}",
dra[i].Field<int>("ContactID"), dra[i].Field<string>("FirstName"),
dra[i].Field<string>("LastName"));
}
DataTable dt2 = dra.CopyToDataTable();
foreach (DataRow row in dt2.Rows)
{
Console.WriteLine(
"ContactID = {0}\tFirstName = {1}\tLastName = {2}",
row["ContactID"], row["FirstName"], row["LastName"]);
}
DataTable dt3 = dt1.Clone();
dra.CopyToDataTable(dt3, LoadOption.Upsert);
foreach (DataRow row in dt3.Rows)
{
Console.WriteLine("ContactID = {0}\tFirstName = {1}\tLastName = {2}",row["ContactID"], row["FirstName"], row["LastName"]);
}
}
}
|