不知道如何更新数据库还望高手教我

lihzsonny 2016-02-28 11:06:34
比如下面,我本想用OleDbDataAdapter的UpdateCommnad属性更新数据库的。
本人是不懂用这个方法更新数据库的,不知道哪里错了。针对这种更新方法,还期望高手分享技术文章。
疑问都写在标注里了:
            OleDbCommand updateCmd = new OleDbCommand();
//不知道where后面的id如何添加参数
updateCmd.CommandText = "update articles set [title]=?,[author]=?,[content]=? where id = ?";
updateCmd.CommandType = CommandType.Text;
updateCmd.Connection = conn;
updateCmd.Parameters.Add("?", OleDbType.VarWChar, 100, "title");
updateCmd.Parameters.Add("?", OleDbType.VarWChar, 10, "author");
updateCmd.Parameters.Add("?", OleDbType.VarWChar, 10000, "content");
adapter.UpdateCommand = updateCmd;

conn.Open();
using (OleDbTransaction tran = conn.BeginTransaction(IsolationLevel.ReadCommitted))
{
adapter.SelectCommand.Transaction = tran;
adapter.UpdateCommand.Transaction = tran;
try
{
DataRow updateRow = ds.Tables["articles"].Rows[id];//通过断点诊断,在这出错了:在位置216处没有任何行。
//以下没有执行----中间的行在诊断时跳过,直接catch错误了
updateRow["title"] = n_title;
updateRow["author"] = n_author;
updateRow["content"] = n_content;
adapter.Update(ds, "articles"); //我保证有articles这个表和ds这个DataSet
tran.Commit();
//以上没有执行
}
catch (Exception ex) //直接到这里了
{
try
{
tran.Rollback();//回滚了
}
catch (Exception exc)
{
JS.JScript.Alert(exc.Message);
}

JS.JScript.Alert(ex.Message);//抛出:在位置216处没有任何行,216行在access中,有数据的。

}
}
conn.Close();
JS.JScript.AlertAndRedirect("修改成功!", "news_mg.aspx");
...全文
213 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihzsonny 2016-03-01
  • 打赏
  • 举报
回复
通过各位高手的帮助,这个问题终于得到圆满解决,谢谢大家。 以下是调试通过的烂码,没有删减,希望对其他人有所帮助:
			OleDbCommand updateCmd = new OleDbCommand();
			updateCmd.CommandText = "update articles set [title]=@title,[author]=@author,[quotedfrom]=@quotedfrom,[quotedlink]=@quotedlink,[subject]=@subject,[update]=@update,[content]=@content,[channel]=@channel,[lanmu]=@lanmu where id = @id";
			updateCmd.CommandType = CommandType.Text;
			updateCmd.Connection = conn;
//					OleDbParameter parmUpdPk = adapter.UpdateCommand.Parameters.Add("@id", OleDbType.Integer, 4, "id");
//					parmUpdPk.SourceVersion = DataRowVersion.Original;
			updateCmd.Parameters.Add("@title", OleDbType.VarWChar, 100).Value=n_title;
			updateCmd.Parameters.Add("@author", OleDbType.VarWChar, 10).Value=n_author;
			updateCmd.Parameters.Add("@quotedfrom", OleDbType.VarWChar, 20).Value=quotedfrom;
			updateCmd.Parameters.Add("@quotedlink", OleDbType.VarWChar, 100).Value=quotedlink;
			updateCmd.Parameters.Add("@subject", OleDbType.VarWChar, 10).Value=subject;
			updateCmd.Parameters.Add("@update", OleDbType.DBTimeStamp, 40).Value=string.IsNullOrEmpty(n_date) ? DateTime.Now.ToString() : n_date;
			updateCmd.Parameters.Add("@content", OleDbType.VarWChar, 10000).Value=n_content;
			updateCmd.Parameters.Add("@channel", OleDbType.VarChar, 10).Value=channel;
			updateCmd.Parameters.Add("@lanmu", OleDbType.VarChar, 20).Value=lanmu;
			updateCmd.Parameters.Add("@id", OleDbType.Integer).Value=id;
			
			adapter.UpdateCommand = updateCmd;
            conn.Open();
            using (OleDbTransaction tran = conn.BeginTransaction(IsolationLevel.ReadCommitted))
            {
                adapter.SelectCommand.Transaction = tran;
                adapter.UpdateCommand.Transaction = tran;
                try
                {
					adapter.UpdateCommand.ExecuteNonQuery();

                    tran.Commit();
                }
                catch (Exception ex)
                {
                    try
                    {
                        tran.Rollback();
                    }
                    catch (Exception exc)
                    {
                        JS.JScript.Alert(exc.Message);
                    }

                    JS.JScript.Alert(ex.Message);

                }
            }
            conn.Close();

            JS.JScript.AlertAndRedirect("修改成功!", "news_mg.aspx");
guanyelong 2016-02-29
  • 打赏
  • 举报
回复
id 的赋值参数呢。
江南小鱼 2016-02-29
  • 打赏
  • 举报
回复
            OleDbCommand updateCmd = new OleDbCommand();
           //不知道where后面的id如何添加参数
            updateCmd.CommandText = "update articles set [title]=@title,[author]=@author,[content]=@content where id = @id";
            updateCmd.CommandType = CommandType.Text;
            updateCmd.Connection = conn;
            updateCmd.Parameters.Add("@title", OleDbType.VarWChar, 100, "title");
            updateCmd.Parameters.Add("@author", OleDbType.VarWChar, 10, "author");
            updateCmd.Parameters.Add("@content", OleDbType.VarWChar, 10000, "content");
            updateCmd.Parameters.Add("@id", OleDbType.Int, 4, 123);
            adapter.UpdateCommand = updateCmd;

            conn.Open();
            using (OleDbTransaction tran = conn.BeginTransaction(IsolationLevel.ReadCommitted))
            {
                adapter.SelectCommand.Transaction = tran;
                adapter.UpdateCommand.Transaction = tran;
                try
                {
                    DataRow updateRow = ds.Tables["articles"].Rows[id];//通过断点诊断,在这出错了:在位置216处没有任何行。
                    //以下没有执行----中间的行在诊断时跳过,直接catch错误了
                    updateRow["title"] = n_title;
                    updateRow["author"] = n_author;
                    updateRow["content"] = n_content;
                    adapter.Update(ds, "articles");  //我保证有articles这个表和ds这个DataSet
                    tran.Commit();
                    //以上没有执行
                }
                catch (Exception ex) //直接到这里了
                {
                    try
                    {
                        tran.Rollback();//回滚了
                    }
                    catch (Exception exc)
                    {
                        JS.JScript.Alert(exc.Message);
                    }

                    JS.JScript.Alert(ex.Message);//抛出:在位置216处没有任何行,216行在access中,有数据的。

                }
            }
            conn.Close();
            JS.JScript.AlertAndRedirect("修改成功!", "news_mg.aspx");
  • 打赏
  • 举报
回复
你的id对应的参数呢?你设置了四个参数,结果只传了三个
正怒月神 版主 2016-02-29
  • 打赏
  • 举报
回复
updateCmd.Parameters.Add("?", OleDbType.VarWChar, 100, "title"); updateCmd.Parameters.Add("?", OleDbType.VarWChar, 10, "author"); updateCmd.Parameters.Add("?", OleDbType.VarWChar, 10000, "content"); 不知道是不是这种写法有问题?
Justin-Liu 2016-02-29
  • 打赏
  • 举报
回复
调试的时候也看不懂吗?
lihzsonny 2016-02-29
  • 打赏
  • 举报
回复
引用 5 楼 guanyelong 的回复:
id 的赋值参数呢。
id的赋值我是这样写的(如下),但总是不对。
            OleDbParameter parmUpdPk = adapter.UpdateCommand.Parameters.Add("?", OleDbType.Integer, 4, "id");
            parmUpdPk.SourceVersion = DataRowVersion.Original;
我参考了如下链接,如法炮制的,看各位能否再给指条明路? http://blog.csdn.net/gyming/article/details/17533237 https://msdn.microsoft.com/zh-cn/library/a94b9hed%28v=vs.80%29.aspx
秋的红果实 2016-02-29
  • 打赏
  • 举报
回复
没见过你这种带?的写法,一般是这样的 updateCmd.CommandText = "update articles set [title]=@p_title,[author]=@p_author,[content]=@p_content where id = @p_id"; updateCmd.Parameters.Add("@p_title", OleDbType.VarWChar, 100).value=更新后的标题值; …… updateCmd.Parameters.Add("@p_id", OleDbType.VarWChar, 10).value=要更新记录的id; //这样就传进具体值了 只要能查到数据,后面代码估计可以执行
insus 2016-02-29
  • 打赏
  • 举报
回复
当参数很多时,特别在维护时,很难找到匹配与对应的,是哪个对应哪一个。
  • 打赏
  • 举报
回复
把你的多余的 try...catch 注释掉,只保留必要的代码。然后在调试器进入调试状态时,调试当前环境 id 值是多少、ds.Tables["articles"].Rows.Count 的值是多少,以及调用堆栈是什么。 贴出来!

62,270

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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