谁可以用SqlDataAdapter和dataset做一个数据库更新实列来看看...谢谢啦
解决方案 »
- ★ ★ ★ ★ 小白请教:学习PetShop4.0,关于连接字符串?★ ★ ★ ★
- 请问下,我把C#里的一个字符串数组放到javascript里可以读取吗?
- 租用的ASP.NET2.0的空间能否使用ASP.NET AJAX Control ToolKits?
- 望高手指点
- 知道起始时间,如何把这个时间段的日期罗列出来呢?
- t-sql语句的存储过程不能用top+变量吗?
- 怎样在初始化页面时,将DropDownList中特定的项目已选中?
- 请问,我的VS.NET为什么会自动退出?高分求教!
- 急用!在线等待,在服务器端如何做到下载某一网站一个文件而不弹出下载对话框?
- 我想请问下后台能调用ajax嘛?
- GridView的头样式问题
- 请问大家是如何管理asp.net的网站的啊
adapter.InsertCommand = new SqlCommand(
"INSERT INTO Customers (dept, itemcode,seqitem) " +
"VALUES (@dept, @itemcode,@seqitem)");adapter.InsertCommand.Parameters.Add("@dept",
SqlDbType.String, 50, "dept");
adapter.InsertCommand.Parameters.Add("@itemcode",
SqlDbType.String, 50, "itemcode");
adapter.InsertCommand.Parameters.Add("@seqitem",
SqlDbType.String, 50, "seqitem");foreach(DataRow dr in dt.Rows)
{
dr.SetAdded ();//此方法要.net 2.0以上才有
}
adapter.Update(dt);我的foreach是把dt的行状态置成added这样da.update的时候就会去调insert的语句.
你可以把da的update,insert,command全设置好了.这样调用da.update会自动根据行状态去执行相应的update,insert命令.
可省去foreach的过程.
学习学的是学习方法思路很重要
private void btnUpdate_Click(object sender, System.EventArgs e)
{
SqlConnection nwindConn = new SqlConnection( "server=(local);uid=sa;pwd=111;database=Northwind" ); SqlDataAdapter catDA = new SqlDataAdapter("SELECT * FROM Categories", nwindConn); catDA.UpdateCommand = new SqlCommand("UPDATE Categories SET CategoryName = @CategoryName " +
"WHERE CategoryID = @CategoryID" , nwindConn); catDA.UpdateCommand.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15, "CategoryName"); SqlParameter workParm = catDA.UpdateCommand.Parameters.Add("@CategoryID", SqlDbType.Int);
workParm.SourceColumn = "CategoryID";
workParm.SourceVersion = DataRowVersion.Original; DataSet catDS = new DataSet();
catDA.Fill(catDS, "Categories"); DataRow cRow = catDS.Tables["Categories"].Rows[0];
cRow["CategoryName"] = "NewName"; catDA.Update(catDS,"Categories");
Bind();
} private void btnAdd_Click(object sender, System.EventArgs e)
{
SqlConnection nwindConn = new SqlConnection( "server=(local);uid=sa;pwd=111;database=Northwind" ); SqlDataAdapter catDA = new SqlDataAdapter("SELECT * FROM Categories", nwindConn); catDA.InsertCommand = new SqlCommand("Insert into Categories(CategoryName,Description) values"
+" (@CategoryName,@Description)", nwindConn); catDA.InsertCommand.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15, "CategoryName");
catDA.InsertCommand.Parameters.Add("@Description", SqlDbType.NText, 16, "Description"); DataSet catDS = new DataSet();
catDA.Fill(catDS, "Categories"); DataRow dr = catDS.Tables["Categories"].NewRow();
dr["CategoryName"] = "Added New Name";
dr["Description"] = "my Description";
catDS.Tables["Categories"].Rows.Add(dr); catDA.Update(catDS,"Categories");
Bind();
}
catDA.UpdateCommand.Parameters.Add("@CName", SqlDbType.NVarChar, 15, "CName");SqlParameter workParm = catDA.UpdateCommand.Parameters.Add("@CID", SqlDbType.Int);workParm.SourceColumn = "CID";workParm.SourceVersion = DataRowVersion.Original;DataSet catDS = new DataSet();catDA.Fill(catDS, "table"); DataRow cRow = catDS.Tables["table"].Rows[0];cRow["CName"] = "New C";catDA.Update(catDS);
http://www.cnblogs.com/sytwss/archive/2007/07/05/806567.html
http://www.cnblogs.com/Jamedy/archive/2006/04/12/372953.html
你觉得我有必要抄书吗?
看我底下写的几行文字,如果你连这些都看不懂.那你真的该好好看看书了
搞不清楚datatable的行状态和dataadapter的update方法.还是回去好好看看书吧.
更新数据。
public static SqlDataAdapter CreateCustomerAdapter(
SqlConnection connection)
{
SqlDataAdapter adapter = new SqlDataAdapter(); //设置SelectCommand.
SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", connection); // 给SelectCommand添加参数.
command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
command.Parameters.Add("@City", SqlDbType.NVarChar, 15); adapter.SelectCommand = command; // 设置InsertCommand.
command = new SqlCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)", connection); //给InsertCommand添加参数.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName"); adapter.InsertCommand = command; //设置UpdateCommand.
command = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection); // 给UpdateCommand添加参数.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
SqlParameter parameter = command.Parameters.Add(
"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original; adapter.UpdateCommand = command; // 设置DeleteCommand.
command = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection); // 给DeleteCommand添加参数.
parameter = command.Parameters.Add(
"@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original; adapter.DeleteCommand = command; return adapter;
}
Updata函数在往数据库更新数据时,是根据table里的各行的“行状态”来进行insert,delete,或者updata操作的。
行状态是一个枚举,
unchanged(不做任何操作)
added(insert操作)
modified(好像是这个,updata操作)
deleted(delete操作)
AcceptChanges()以后table里所有行的状态全部变成Unchanged,所以数据库没反应。
插入新行可以用 table.Rows.InsertAt(dr,index)
行状态可以DataTable.Rows[i].RowState查看,可以通过DataTable.Rows[i].SetAdded();
DataTable.Rows[i].SetModified()(不知道是不是这么写的)几个函数设置。
//更新
public void UpdateCreateCustomerAdapter(System.Data.SqlClient.SqlConnection connection)
{
SqlDataAdapter adapter = new SqlDataAdapter(); //设置SelectCommand.
SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", connection); // 给SelectCommand添加参数.
command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
command.Parameters.Add("@City", SqlDbType.NVarChar, 15); adapter.SelectCommand = command; DataSet Det = new DataSet(); adapter.Fill(Det); foreach (DataRow row in Det.Tables[0].Rows)
{
row["CustomerID"] = "0";
row["CompanyName"] = row["CompanyName"] + "-1";
} //设置UpdateCommand.
command = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection); // 给UpdateCommand添加参数.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
SqlParameter parameter = command.Parameters.Add(
"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original; adapter.UpdateCommand = command;
adapter.Update(Det.Tables[0]); // 设置DeleteCommand.
command = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection); // 给DeleteCommand添加参数.
parameter = command.Parameters.Add(
"@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original; adapter.DeleteCommand = command;
}
//删除
public void DeleteCreateCustomerAdapter(System.Data.SqlClient.SqlConnection connection)
{
SqlDataAdapter adapter = new SqlDataAdapter(); //设置SelectCommand.
SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", connection); // 给SelectCommand添加参数.
command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
command.Parameters.Add("@City", SqlDbType.NVarChar, 15); adapter.SelectCommand = command; DataSet Det = new DataSet(); adapter.Fill(Det); // 设置DeleteCommand.
command = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection); // 给DeleteCommand添加参数.
parameter = command.Parameters.Add(
"@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original; adapter.DeleteCommand = command;
adapter.Update(Det); }
要找到需要修改的行,比较方便的做法就是根据记录中的某个值进行查找,这样比用“列号”“行号”什么的方便多了。要做到这一点,首先就是要给数据库指定一个主键,然后即可按照主键进行查找。要注意的是这个主键必须要是在程序中指定的,并且这个主键不一定和你数据库原来有的主键一样。
然后就是最后的更新,当然是用DataAdapter的Update()方法借助CommandBuilder来实现,要注意的是,如果你的数据库一开始没有定义主键,那进行更新的时候会出错,返回的错误将是“对于不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成。”这是因为我们用的Update()实际上是通过CommandBuilder动态生成sql语句,然后才进行的数据库操作。但如果你在一开始创建DataAdapter时的那个sql语句没有包含有主键的列的话,那CommandBuilder将不会发生作用。这一点是必须要牢记的。
说了这么多,如果有不明白的就看下面的程序,这个是我练习时写的,里面有一些个人的设定,比如记录名什么的,相信大家一看就明白。
<%@import namespace="System.Data"%>
<%@import namespace="System.Data.OleDb"%>
<script language="c#" runat="server">
//定义处理更新的方法
//因为是简单示例,所以这个程序已经简化,直接进行更新
//这里有两个参数,第一个用来查找需要更新的行,第二个是要修改的值
private void up_date(int ids,string names){
//连接字符串,不明白的可以看本专题第二篇文章
string connstr=ConfigurationSettings.AppSettings["color"];
OleDbConnection conn=new OleDbConnection(connstr);
string sql="select * from member";
OleDbDataAdapter ada=new OleDbDataAdapter(sql,conn);//建立一个DataAdapter对象
//这里的CommandBuilder对象一定不要忘了,一般就是写在DataAdapter定义的后面
OleDbCommandBuilder cb=new OleDbCommandBuilder(ada);
DataSet ds=new DataSet();//建立DataSet对象
conn.Open();//打开连接
ada.Fill(ds,"mems");//填充DataSet
conn.Close();//注意及时关闭连接
DataTable dt=ds.Tables["mems"];//建立一个DataTable对象,方便操作
dt.PrimaryKey=new DataColumn[]{dt.Columns["id"]};//建立一个主键
DataRow dr=dt.Rows.Find(ids);//根据参数查找到需要修改的行
dr["name"]=names;//对需要修改的记录赋新值
ada.Update(ds,"mems");//用DataAdapter的Update()方法进行数据库的更新
}
//定义用来响应按钮单击事件的方法
private void b1_Click(object sender,System.EventArgs e){
if(Page.IsValid){
int ids=Int32.Parse(idst.Text);//取得要修改的行的id值,并转换成int类型
string names=namest.Text;//取得新值
up_date(ids,names);//调用我们定义的方法处理
lb1.Text="ok!";
</script><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>用dataset更新记录</title>
</head>
<body>
<asp:Label id="lb1" runat="server"/>
<form runat="server">
<asp:TextBox id="idst" runat="server"/>
<asp:RequiredFieldValidator id="rfv1" ControlToValidate="idst" ErrorMessage="error!" runat="server"/>
<asp:TextBox id="namest" runat="server"/>
<asp:RequiredFieldValidator id="rfv2" ControlToValidate="namest" ErrorMessage="error!" runat="server"/>
<asp:Button id="b1" Text="here!!" OnClick="b1_Click" runat="server"/>
</form>
</body>
</html>
补充一点,如果想设多于两个的主键的话,可以用这个
dt.PrimaryKey=new DataColumn[]{dt.Columns["id"],dt.Columns["id2"]};
就是给数组添加两个值,然后使用的时候:
Object[] keyValues=new object[1]; //这里必须实例化否则会向你要初值
//而且需要后面方括号中的这个索引范围
keyValues[0]=(object)key1;
keyValues[1]=(object)key2;
...... 另外一种写法
Object[] keyValues={(object)key1,(object)key2};//以楣举方式赋初值
很明白了吧 :)
private SqlCommand GetUpdateCommand()
{
SqlConnection conn = new SqlConnection(Connstring);
string sql = "update......";
//可以用参数来拼接sql
SqlCommand cmd = new SqlCommand(sql,conn);
return cmd;
} public void UpdateInfo()
{
SqlDataAdapter dar = new SqlDataAdapter();
dar.UpdateCommand = GetUpdateCommand();
dar.Update();
}你说的应该是 duwamish框架中的固定写法你可以参考一下这个 应该是你想要的
http://download.csdn.net/source/862984