.net后台如何对Access数据库多行数据同时更新

.Net技术 码拜 9年前 (2015-02-19) 1142次浏览 0个评论

项目需求后台要对数据库多行数据同时更新,但是在网上了解的资料说access不能同时操作多行数据
string strConn = @”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” + Server.MapPath(@”~/App_Data/Data.mdb”);
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            string uponelink = this.onelink1.Value;
            string uptwolink = this.twolink1.Value;
            string upimg = this.imgname1.Value;
            string upimgnum = this.imgnum1.Value;
            string upvideoname = this.videoname1.Value;
            string strsqlone = “update Blocktwo set Link=”” + uponelink + “” where id=1″;
            OleDbCommand cmdone = new OleDbCommand(strsqlone, conn);
            OleDbDataReader drone = cmdone.ExecuteReader(CommandBehavior.CloseConnection);
            string strsqltwo = “update Blocktwo set Link=”” + uptwolink + “”,Image=”” + upimg + “”,Imagenum=”” + upimgnum + “”,Video=”” + upvideoname + “” where id=2″;
            OleDbCommand cmdtwo = new OleDbCommand(strsqltwo, conn);
            OleDbDataReader drtwo = cmdtwo.ExecuteReader(CommandBehavior.CloseConnection);
            if (cmdone.ExecuteNonQuery() > 0)
            {
                if (cmdtwo.ExecuteNonQuery() > 0)
                {
                    Response.Write(“<script language=javascript>alert(“分块修改成功”);</script>”);
                }
            }

.net后台如何对Access数据库多行数据同时更新
上述代码不行,UPDATE 语句的语法错误,我改怎么设计代码呢?
.net后台如何对Access数据库多行数据同时更新
.net后台如何对Access数据库多行数据同时更新
在论坛里找了个多行操作的思路,好像那个人解决了,但是我的出现已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭
try
            {
                string[] shuzu = { strsqlone, strsqltwo };
                for (int n = 0; n < shuzu.Length; n++)
                {
                    string strsql = shuzu[n].ToString();
                    if (strsql.Trim().Length > 1)
                    {
                        cmdone.CommandText = strsql;
                        cmdone.ExecuteNonQuery();
                    }
                }
                cmdone.Transaction.Commit();  //提交事务
                Response.Write(“<script language=javascript>alert(“分块修改成功”);</script>”);
            }
            catch (OleDbException ex)
            {
                Response.Write(ex.ToString());
            }
            finally
            {
                conn.Close();
            }
.net后台如何对Access数据库多行数据同时更新
第一次就出现了,我又用调用方法的情况,打开一通道,更新一行数据后关闭,回到原方法,再打开一次,更新第二行,出现了
已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭。问题,第一执行方法遇到的
.net后台如何对Access数据库多行数据同时更新
OleDbCommand用完需要释放,用using块包起来
.net后台如何对Access数据库多行数据同时更新
引用 7 楼 Z65443344 的回复:

OleDbCommand用完需要释放,用using块包起来

“update Blocktwo set Link=”” + uponelink + “” where id=1″;
为啥这段对吗提示无效的 SQL语句呢,我在access里面执行的挺好啊?很不解

.net后台如何对Access数据库多行数据同时更新
引用 8 楼 guarentianxia11 的回复:
Quote: 引用 7 楼 Z65443344 的回复:

OleDbCommand用完需要释放,用using块包起来

“update Blocktwo set Link=”” + uponelink + “” where id=1″;
为啥这段对吗提示无效的 SQL语句呢,我在access里面执行的挺好啊?很不解

断点跟,把sql语句拿出来放access里执行,看到底怎么了
比如 uponelink 值到底是多少,是否应该有单引号还是应该没有

.net后台如何对Access数据库多行数据同时更新
10分
此外,你执行的是update语句,不是select语句,为什么要用OleDbDataReader
.net后台如何对Access数据库多行数据同时更新
10分
应该这样用:
int count=cmdone.ExcuteNonQuery();
count表示受影响的行数
.net后台如何对Access数据库多行数据同时更新
引用 10 楼 Z65443344 的回复:

此外,你执行的是update语句,不是select语句,为什么要用OleDbDataReader

对,OleDbDataReader是没有用的,现在出现了“UPDATE 语句的语法错误。”,开启断点后,我把sql语句放入access里面是可以更新的,但是在后台就是报错
pre class=”brush: csharp”>string strConn = @”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” + Server.MapPath(@”~/App_Data/Data.mdb”);
            string uptwolink = this.twolink1.Value;
            string upimg = this.imgname1.Value;
            string upimgnum = this.imgnum1.Value;
            string upvideoname = this.videoname1.Value;
            string upmark = “”;
            if (this.inpralink.Checked == true)
            {
                upmark = “Link”;
            }
            if (this.inpraimg.Checked == true)
            {
                upmark = “Image”;
            }
            if (this.inpravid.Checked == true)
            {
                upmark = “Video”;
            }
            string strsqltwo = “update Blocktwo set Link=”” + uptwolink + “”,Image=”” + upimg + “”,Imagenum=”” + upimgnum + “”,Video=”” + upvideoname + “”,Mark=”” + upmark + “” where id=2″;
            OleDbConnection conn = new OleDbConnection(strConn);
            OleDbCommand cmdtwo = new OleDbCommand();
            using (cmdtwo = new OleDbCommand(strsqltwo, conn))
            {
                conn.Open();
                try
                {
                    if (cmdtwo.ExecuteNonQuery() > 0)
                    {
                        Response.Write(“<script language=javascript>alert(“分块二修改成功”);</script>”);
                    }
                }
                catch (OleDbException ex)
                {
                    Response.Write(ex.ToString());
                }
                finally
                {
                    conn.Close();
                }
            }

.net后台如何对Access数据库多行数据同时更新
好了,问题解决了,12楼的update语法问题是在列名前面加上了表名.,//Blocktwo.Link
感谢Z65443344

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明.net后台如何对Access数据库多行数据同时更新
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!