class Program
{
static void Main(string[] args)
{
SqlConnection thisConnection = new SqlConnection(
@"Data Source=.\SQLEXPRESS;" +
@"AttachDbFilename='C:\SQL Server 2000 Sample Databases\NORTHWND.MDF';"
+
@"Integrated Security=true;Connect Timeout=30;User Instance=true");
SqlDataAdapter thisAdapter = new SqlDataAdapter(
"select CustomerID, CompanyName from Customers", thisConnection);
//将SqlCommandBuilder与thisAdapter关联,这样就不用自己写sql的更新语句了
SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);
//注:: DataSet、DataTable、DataRow、DataColumn是表中的数据在内存中的表示。为了更新数据库,需要调用Update()方法
DataSet thisDataSet = new DataSet();
thisAdapter.Fill(thisDataSet, "Customers");
Console.WriteLine("Name before change:{0}", thisDataSet.Tables["Customers"].Rows[9]["CompanyName"]);
thisDataSet.Tables["Customers"].Rows[9]["CompanyName"] = "Acme, Inc.";
thisAdapter.Update(thisDataSet, "Customers");
Console.WriteLine("name after change:{0}", thisDataSet.Tables["Customers"].Rows[9]["CompanyName"]);
thisConnection.Close();
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
}
{
static void Main(string[] args)
{
SqlConnection thisConnection = new SqlConnection(
@"Data Source=.\SQLEXPRESS;" +
@"AttachDbFilename='C:\SQL Server 2000 Sample Databases\NORTHWND.MDF';"
+
@"Integrated Security=true;Connect Timeout=30;User Instance=true");
SqlDataAdapter thisAdapter = new SqlDataAdapter(
"select CustomerID, CompanyName from Customers", thisConnection);
//将SqlCommandBuilder与thisAdapter关联,这样就不用自己写sql的更新语句了
SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);
//注:: DataSet、DataTable、DataRow、DataColumn是表中的数据在内存中的表示。为了更新数据库,需要调用Update()方法
DataSet thisDataSet = new DataSet();
thisAdapter.Fill(thisDataSet, "Customers");
Console.WriteLine("Name before change:{0}", thisDataSet.Tables["Customers"].Rows[9]["CompanyName"]);
thisDataSet.Tables["Customers"].Rows[9]["CompanyName"] = "Acme, Inc.";
thisAdapter.Update(thisDataSet, "Customers");
Console.WriteLine("name after change:{0}", thisDataSet.Tables["Customers"].Rows[9]["CompanyName"]);
thisConnection.Close();
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
}