oledbcommand的问题

houjianxun 2002-09-05 03:10:39
我想打开表中的一行之前,先根据返回的ID更新一下,然后再打开,应该怎么做呢?
这样放两个oledbcommand在一起是不行??

以下内容为程序代码,可是却有错误:

dim upcomm As New oledbcommand("update article set hits=hits+1 where ID="& id ,myconn)
upcomm.executenonquery()
upcomm.close()
Dim mycomm As New oledbcommand("select * from article where articleid="& id ,myconn)
Dim dr As oledbdatareader = mycomm.Executereader()
If dr.read() Then
.......
end if

...全文
209 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
houjianxun 2002-09-10
  • 打赏
  • 举报
回复
谢谢,我刚学习.net,只做了一个文章管理的

演示:http://lionsky.net/article/index.aspx
Tongls 2002-09-10
  • 打赏
  • 举报
回复
你的数据库Conn没有连接! 你必须在Comm之后,连接好Conn,才能用!


到管理里,分分了以后,你输入你的密码,就可以给分啦! OK!
houjianxun 2002-09-10
  • 打赏
  • 举报
回复
我刚来这里不久,我不知道该怎么给你们结分,能说一下吗?
houjianxun 2002-09-10
  • 打赏
  • 举报
回复
哦,我发现是我写错了
在mycomm.executenonquery();的后面多加了一个;去掉就可以了,谢谢你们
houjianxun 2002-09-10
  • 打赏
  • 举报
回复
但是二楼的那位说的方法不可以,执行到mycomm.executenonquery();这一句就
提示:

说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。

编译器错误信息: BC30037: 字符无效。

源错误:


行 13: Dim id As integer = request.querystring("id")
行 14: dim mycomm As New oledbcommand("update article set hits=hits+1 where id="& id,myconn)
行 15: mycomm.executenonquery();


下面是我的源代码:
myconn.open()
Dim id As integer = request.querystring("id")
dim mycomm As New oledbcommand("update article set hits=hits+1 where id="& id,myconn)
mycomm.executenonquery();
mycomm As New oledbcommand("select * from article where articleid="& id ,myconn)
Dim dr As oledbdatareader = mycomm.Executereader()
If dr.read() Then
...
else
...
end if
tojike 2002-09-09
  • 打赏
  • 举报
回复
asp.net可以同时执行n条SQL语句,这是与asp显著不同的地方
zhang_dz 2002-09-06
  • 打赏
  • 举报
回复
你好

其实不需要用两个command对象,不用关闭upcomm.只要重新New一下就行了,.NET garbage collection会自动清除reference不到的对象.请看如下代码示例:

//sqlConnection1是个已建立好的connection对象.
sqlConnection1.Open();
SqlCommand cmd1 = new SqlCommand("update article set hits=hits+1 where ID ="& id, sqlConnection1);
cmd1.ExecuteNonQuery();

cmd1 = new SqlCommand("select * from article where id="& id ,sqlConnection1);
...

希望有所帮助

- 微软全球技术中心

本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款
(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)
houjianxun 2002-09-06
  • 打赏
  • 举报
回复
ttt~~
用ASP.NET做的 using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.OleDb; /// /// Class1 的摘要说明 /// public class Class1 { private OleDbConnection m_Connection; private String m_brChar = "\n"; public Class1() { // // TODO: 在此处添加构造函数逻辑 // } private void ExecuteCommand(OleDbCommand cmd) { cmd.Connection=m_Connection; try { m_Connection.Open(); cmd.ExecuteNonQuery(); } finally { m_Connection.Close(); } } public DataSet GetNotes(DateTime fromDate, DateTime toDate) { DataSet ds=new DataSet(); OleDbDataAdapter da=new OleDbDataAdapter("select * from Notes where DateTime BETWEEN ? and ? ORDER BY DateTime DESC",m_Connection); da.SelectCommand.Parameters.Add("FromDate", OleDbType.Date).Value = fromDate.AddDays(1); m_Connection.Open(); da.Fill(ds,"MyNotes"); m_Connection.Close(); return ds; } public void InsertNote(string title,string contents) { OleDbCommand cmd =new OleDbCommand ("insert into Notes(Title,Contents) values(?,?)"); cmd.Parameters .Add ("Title",OleDbType .VarChar ).Value =title ; cmd.Parameters .Add ("Contents",OleDbType .LongVarChar ).Value =contents.Replace (m_brChar ,"
"); ExecuteCommand (cmd); } public void InsertComment(int noteid,string author,string email,string comment) { OleDbCommand cmd=new OleDbCommand ("insert into Comments(NoteID,Author,Email,Comment) values(?,?,?,?)"); cmd.Parameters .Add ("NoteID",OleDbType .VarChar ).Value =noteid; cmd.Parameters .Add ("Author",OleDbType .VarChar ).Value =author ; cmd.Parameters .Add ("Email",OleDbType .VarChar ).Value =email ; cmd.Parameters .Add ("Comment",OleDbType .LongVarChar ).Value =comment.Replace (m_brChar ,"
"); ExecuteCommand (cmd); } public void UpdaeNote(int noteid, string title, string contents) { OleDbCommand cmd = new OleDbCommand("UPDATE [Notes] SET [Title]=?,[Contents]=? WHERE [NoteID]=?"); cmd.Parameters.Add("Title", OleDbType.VarChar).Value = title; cmd.Parameters.Add("Contents", OleDbType.LongVarChar).Value = contents.Replace(m_brChar, "
"); cmd.Parameters.Add("NoteID", OleDbType.Integer).Value = noteid; ExecuteCommand(cmd); } public void UpdateComment(int commentid,string author,string email,string comment) { OleDbCommand cmd =new OleDbCommand ("UPDATE Comments SET Author =?,Email=?,Comment=? WHERE CommentID=?"); cmd.Parameters .Add ("Author",OleDbType .VarChar ).Value =author; cmd.Parameters .Add ("Comment",OleDbType .LongVarChar).Value =comment.Replace (m_brChar ,"
"); cmd.Parameters.Add("Email", OleDbType.VarChar).Value = email; cmd.Parameters.Add("CommentID", OleDbType.VarChar).Value = commentid; ExecuteCommand(cmd); } public void DeleteNote(int noteid) { OleDbCommand cmd =new OleDbCommand ("delete from Notes where NoteID="+noteid); ExecuteCommand(cmd); } public void DeleteComment(int commentid) { OleDbCommand cmd =new OleDbCommand ("delete from Comments where CommentID="+commentid); ExecuteCommand (cmd); } public void GetNoteData(int noteid,ref string title,ref string contents,ref string post_time) { OleDbCommand cmd=new OleDbCommand ("select * from Notes where NoteID="+ noteid ,m_Connection ); try { m_Connection .Open(); OleDbDataReader reader=cmd.ExecuteReader (CommandBehavior .CloseConnection ); if(reader .Read ()) { title =reader ["Title"].ToString (); contents =reader ["Contents"].ToString ().Replace ("
",m_brChar ); post_time =reader ["DataTime"].ToString (); reader .Close (); } } finally { m_Connection .Close (); } } public void GetCommentData(int commentid,ref string author,ref string email,ref string comment) { OleDbCommand cmd=new OleDbCommand ("select * from Comments where CommentID="+commentid ,m_Connection ); try { m_Connection .Open (); OleDbDataReader reader=cmd.ExecuteReader (CommandBehavior .CloseConnection ); if(reader.Read ()) { author =reader ["Author"].ToString (); email =reader ["Comment"].ToString ().Replace ("
",m_brChar ); reader .Close (); } } finally { m_Connection .Close (); } } } /// /// data 的摘要说明 ///

1,979

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧