以下代码都在xp系统下测试通过
环境:vs.net2005 \ sql server 2000\xp
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace sqltest
{
class Program
{
static void Main(string[] args)
{
string sConnectonString;
sConnectonString = "Password=sa;user id = sa;Initial Catalog = master;Data Source=(local)";
SqlConnection objConn = new SqlConnection(sConnectonString);
objConn.Open();
SqlDataAdapter daAuthors = new SqlDataAdapter("select * from city_users", objConn);
DataSet dsMaster = new DataSet("master");
daAuthors.FillSchema(dsMaster, SchemaType.Source, "city_users");//加载表的构架,这样在操作加载到dataset里的表时就不用重新设置主键了
daAuthors.Fill(dsMaster, "city_users");
dsMaster.Tables["city_users"].Rows[1]["user_name"] = "sql";//注意这里的Rows[1],这个1指得是在DataSet里的表里的行号,其与数据库里的行号不一致如图1
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(daAuthors);//构建sql命令
daAuthors.Update(dsMaster, "city_users");//更新
//daAuthors.UpdateCommand = updatecomm;
//daAuthors.UpdateCommand.ExecuteNonQuery();
objConn.Close();
Console.WriteLine("update was successful");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace sqltest
{
class Program
{
static void Main(string[] args)
{
string sConnectonString;
sConnectonString = "Password=sa;user id = sa;Initial Catalog = master;Data Source=(local)";
SqlConnection objConn = new SqlConnection(sConnectonString);
objConn.Open();
SqlDataAdapter daAuthors = new SqlDataAdapter("select * from city_users", objConn);
DataSet dsMaster = new DataSet("master");
daAuthors.FillSchema(dsMaster, SchemaType.Source, "city_users");//加载表的构架,这样在操作加载到dataset里的表时就不用重新设置主键了
daAuthors.Fill(dsMaster, "city_users");
dsMaster.Tables["city_users"].Rows[1]["user_name"] = "sql";//注意这里的Rows[1],这个1指得是在DataSet里的表里的行号,其与数据库里的行号不一致如图1
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(daAuthors);//构建sql命令
daAuthors.Update(dsMaster, "city_users");//更新
//daAuthors.UpdateCommand = updatecomm;
//daAuthors.UpdateCommand.ExecuteNonQuery();
objConn.Close();
Console.WriteLine("update was successful");
}
}
}
图1
方法二:手动添加sql语句,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace sqltest
{
class Program
{
static void Main(string[] args)
{
string sConnectonString;
sConnectonString = "Password=sa;user id = sa;Initial Catalog = master;Data Source=(local)";
SqlConnection objConn = new SqlConnection(sConnectonString);
objConn.Open();
SqlCommand updatecomm = new SqlCommand("update city_users set user_name='mysql' where user_id = 7", objConn);
daAuthors.UpdateCommand = updatecomm;
daAuthors.UpdateCommand.ExecuteNonQuery();
objConn.Close();
Console.WriteLine("update was successful");
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace sqltest
{
class Program
{
static void Main(string[] args)
{
string sConnectonString;
sConnectonString = "Password=sa;user id = sa;Initial Catalog = master;Data Source=(local)";
SqlConnection objConn = new SqlConnection(sConnectonString);
objConn.Open();
SqlCommand updatecomm = new SqlCommand("update city_users set user_name='mysql' where user_id = 7", objConn);
daAuthors.UpdateCommand = updatecomm;
daAuthors.UpdateCommand.ExecuteNonQuery();
objConn.Close();
Console.WriteLine("update was successful");
}
}
}
为什么我要写这一篇呢?原因是我在用MySql时,用的MySql驱动没有CommandBuilder对像,不能自动构建sql命令,所以就有了这篇文章。