Access想返回新记录自动编号ID值时,有什么好办法,我这样做出错原因在哪?
string sql, sql1;int newid=0;
string title, contents, author,copyfrom;
title = Title.Text.ToString();
contents = HTMLcode(Content.Text.ToString());
string StrCnn;
StrCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("jiceng.htm");
OleDbConnection Cnn = new OleDbConnection(StrCnn);
sql = "insert into art_info(CLASSID,Author,Copyfrom,TITLE,Content,Flag,addtime,uptime) values(3,'" + title + "','" + author + "','"+copyfrom+"','" + contents + "'," + flag + ",#" + Addtime.Text.Trim() + "#,date())";
Cnn.Open();
OleDbCommand Cmd = new OleDbCommand(sql, Cnn);
Cmd.ExecuteNonQuery();
//以上为插入新数据
//下面为取出新记录ID
sql1 = "SELECT id,title FROM art_info where title='" + title + "'";
OleDbDataAdapter catDA = new OleDbDataAdapter(sql1, Cnn);
DataSet DS1 = new DataSet();
catDA.Fill(DS1,"asd");
newid = Convert.ToInt32(DS1.Tables[0].Rows[0]["id"].ToString());//调试时这里提示 未将对象引用设置到对象的实例. 将Tables[0]改为Tables["asd"]也不行
Cnn.Close();
请帮助解决,取回新记录ID还有什么好办法?
--------------------编程问答-------------------- 如果是自动编号,取出新记录ID 就没有必要用条件where title='" + title + "'"; ,用 order by ID desc,第一条记录ID就是最新的。 --------------------编程问答-------------------- 要是有另一个并发数据会有问题吧
补充:.NET技术 , ASP.NET