|
|
2008年11月22日
 Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection("Server=zhuobin;uid=sa;pwd=zhuobin;database=Northwind");
string qry = @"select * from employees where country='UK'";
string del = @"delete from employees where employeeid=@employeeid";
try
{
//Create the adapter
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(qry,conn);
//Create the DataSet
DataSet ds = new DataSet();
da.Fill(ds,"Employees");
//get data Table reference
DataTable dt=ds.Tables["employees"];
//Create the Command
SqlCommand cmd = new SqlCommand(del,conn);
cmd.Parameters.Add("@employeeid",SqlDbType.Int,4,"employeeid");
string filter = @"firstname='Roy' and lastname='Beatty'";
//delete employees
foreach (DataRow row in dt.Select(filter))
{
row.Delete();
}
da.DeleteCommand = cmd;
da.Update(ds,"employees");
foreach (DataRow row in dt.Rows)
{
Console.WriteLine("{0}{1}{2}",row["firstname"].ToString().PadRight(15),row["lastname"].ToString().PadLeft(25),row["city"]);
}
}
catch (SqlException ex)
{
Console.WriteLine("The error:{0}", ex.Message);
}
finally
{
conn.Close();
} Console.ReadLine();
}
}
}
Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection("Server=zhuobin;uid=sa;pwd=zhuobin;database=Northwind");
string qry = "select * from employees where country='UK'";
string ins = @"insert into employees(firstname,lastname,titleofcourtesy,city,country)values(@firstname,@lastname,@titleofcourtesy,@city,@country)";
try
{
//create the Adapter
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(qry,conn);
//create the DataSet
DataSet ds = new DataSet();
da.Fill(ds,"employees");
//get the table reference
DataTable dt = ds.Tables["employees"];
//Add a new row
DataRow newRow = dt.NewRow();
newRow["Firstname"] = "Zhuo";
newRow["lastname"] = "bin";
newRow["titleofcourtesy"] = "Sir";
newRow["city"] = "Tengzhou";
newRow["country"] = "China";
dt.Rows.Add(newRow);
//display the data
foreach (DataRow row in dt.Rows)
{
Console.WriteLine("{0}{1}{2}",row["firstname"].ToString().PadRight(15),row["lastname"].ToString().PadLeft(25),row["city"]);
}
//insert into employees
//create command
SqlCommand cmd = new SqlCommand(ins,conn);
//map parameters
cmd.Parameters.Add("@firstname",SqlDbType.NVarChar,10,"firstname");
cmd.Parameters.Add("@lastname",SqlDbType.NVarChar,10,"lastname");
cmd.Parameters.Add("@titleofcourtesy",SqlDbType.NVarChar,10,"titleofcourtesy");
cmd.Parameters.Add("@city",SqlDbType.NVarChar,15,"city");
cmd.Parameters.Add("@country",SqlDbType.NVarChar,15,"country");
//insert into employees
da.InsertCommand = cmd;
da.Update(ds,"employees");
}
catch (SqlException ex)
{
Console.WriteLine("The error:{0}", ex.Message);
}
finally
{
conn.Close();
}
Console.ReadLine();
}
}
}
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection("Server=zhuobin;uid=sa;pwd=zhuobin;database=Northwind");
string sql = "select * from employees where country='UK'";
string upd = @"update employees set city=@city where employeeid=@employeeid";
try
{
//Create the DataAdapter
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sql,conn);
//crate the dataset and fill it
DataSet ds = new DataSet();
da.Fill(ds,"employees");
//get the datatable reference
DataTable dt=ds.Tables["employees"];
//modify the city in the first row
dt.Rows[0]["city"] = "Xuzhou";
//Dislay the rows
foreach (DataRow row in dt.Rows)
{
Console.WriteLine("{0} {1} {2}",row["firstname"].ToString().PadRight(15),row["lastname"].ToString().PadLeft(25),row["city"]);
}
//update Employees
SqlCommand cmd = new SqlCommand(upd,conn);
//City
cmd.Parameters.Add("@city",SqlDbType.VarChar,10,"city");
//Employees id
SqlParameter parm = cmd.Parameters.Add("@employeeid",SqlDbType.Int,4,"employeeid");
parm.SourceVersion = DataRowVersion.Original;
//Update database
da.UpdateCommand = cmd;
da.Update(ds,"employees");
}
catch (SqlException ex)
{
Console.WriteLine("The Error:{0}", ex.Message);
}
finally
{
conn.Close();
}
Console.ReadLine();
}
}
}
Code
//注意如何给DataTable添加新行
DataRow newRow=dt.NewRow();//调用方法
.......
dt.Rows.Add(newRow);

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection("Server=zhuobin;uid=sa;pwd=zhuobin;database=Northwind");
string sql =@"select * from employees where country='UK'";
try
{
//Create the DataAdapter
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sql,conn);
//Create dataSet and fill it
DataSet ds = new DataSet();
da.Fill(ds,"employees");
//get the table reference
DataTable dt=ds.Tables["employees"];
//first name to be nullable
dt.Columns["firstname"].AllowDBNull = true;
//modify city in the first row
dt.Rows[0]["city"] = "Xuzhou";
//Add a row
DataRow newRow =dt.NewRow();
newRow["firstname"] = "Zhuo";
newRow["lastname"] = "bin";
newRow["titleofcourtesy"] = "123";
newRow["city"] = "Tengzhou";
newRow["country"] = "China";
dt.Rows.Add(newRow);
//display the data
foreach (DataRow row in dt.Rows)
{
Console.WriteLine("{0} {1} {2}",row["firstname"].ToString().PadRight(15),row["lastname"].ToString().PadLeft(25),row["city"]);
}
}
catch (SqlException ex)
{
Console.WriteLine("The error:{0}", ex.Message);
}
finally
{
conn.Close();
}
Console.ReadLine();
}
}
}
|