|
蓝森林 http://www.lslnet.com 2006年8月25日 8:28
[.net]无法添加数据.进来帮帮忙吧.
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.oledb" %>
............
........
.........
dim conn as new oledbconnection
dim adpt as new oledbdataadapter
dim ds as new dataset
dim provider="provider=microsoft.jet.oledb.4.0"
dim database="data source="&server.mappath("XXX.mdb")
conn=new oledbconnection(provider &";"& database)
conn.open
dim sql="select * from YYY"
adpt=new oledbdataadapter(sql,conn)
adpt.fill(ds)
dim row1 as datarow=ds.tables.item(0).newrow
row1.item("AAA")=aaa.text
row1.item("BBB")=bbb.text
ds.tables.item(0).add(row1)
adpt.update(ds)
conn.close
...........
..............
结果不能运行.错误如下:
[size=5]Server Error in '/' Application.[/size]
[size=4]Update requires a valid InsertCommand when passed DataRow collection with new rows. [/size]
Source Error:
Line 114:row1.item("homepage")=homepage.text
Line 115:Ds.tables.item(0).rows.add(row1)
[color=Red]Line 116:adpt.update(ds)[/color]
Line 117:conn.close
Line 118:msg.text = "ok"
请指点一下. |
UP.一下吧... |
requires a valid InsertCommand
使用adapter的时候需要一个插入命令,要不然怎么知道你把数据交上去做什么
这个命令可以使用commandbuilder来自动构造,也可以自己指定command来完成
最简单的就是这样
adpt=new oledbdataadapter(sql,conn)
//加这一句就可以
OleDbCommandBuilder cb=new OleDbCommandBuilder(adpt);
adpt.fill(ds)
commandbuilder会给你完成command的创建工作,然后把这个command给adapter用,adapter使用这个命令完成update工作。
我的站上第13篇是关于这个的,可以去看看,因为是以前写的,不知里面有没有错误,呵呵,当时也是和你一样的迷惑 |
不行的啊.直接加你那句不行.后来我自加.
dim bd as new oledbcommandbuilder
..........
bd = new oledbcommandbilder(adpt)
这样才可以.但新的问题又来.这回到:
INSERT INTO 语句的语法错误。
Line 116:row1.item("homepage")=homepage.text
Line 117:ds.tables.item(0).rows.add(row1)
[color="red"]Line 118:adpt.update(ds)[/color]
Line 119:conn.close
Line 120:msg.text = "ok" |
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[0]);
SqlDataAdapter ada=new SqlDataAdapter("select * from jobs",conn);
SqlCommandBuilder cb=new SqlCommandBuilder(ada);
DataSet ds=new DataSet();
ada.Fill(ds);
DataTable dt=ds.Tables[0];
dt.PrimaryKey=new DataColumn[]{dt.Columns[0]};
DataRow dr=dt.Rows.Find(1);
Response.Write(dr[1].ToString());
dr[1]="New Hire - Job not specified";
DataSet us=ds.GetChanges();
ada.Update(us);
Response.Write(dr[1].ToString());
ds.Dispose();
us.Dispose(); |
|