您的位置:首页 > 数据库

利用DataSet对数据库进行修改

2012-04-20 13:01 369 查看
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();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐