老师上课讲的如何使用update方法更新回数据库的问题,并且附带讲了ado.net的事务处理,但我写了段代码怎么也更新不回数据库。请高手指点!DataTable dt = (DataTable)Session["Authors"];
DataTable d = dt.GetChanges(); string constr = System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();
SqlConnection mycon=new SqlConnection(constr);
SqlCommand mycom =  new SqlCommand();
mycom.CommandText = "GetAllAuthors";
mycom.CommandType = CommandType.StoredProcedure;
mycom.Connection = mycon;
           
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = mycom; SqlCommandBuilder cb = new SqlCommandBuilder();
cb.DataAdapter = da; da.InsertCommand = cb.GetInsertCommand();
da.UpdateCommand = cb.GetUpdateCommand();
da.DeleteCommand = cb.GetDeleteCommand();

SqlConnection  con = da.InsertCommand.Connection;
mycon.Open();
SqlTransaction  tran = mycon.BeginTransaction();

da.UpdateCommand.Transaction = tran;
                           da.DeleteCommand.Transaction=tran;
da.InsertCommand.Transaction = tran;
try
{
da.Update(dt);
tran.Commit();
}
catch (Exception e1)
{
string s = e1.Message;
tran.Rollback();
}
finally
{
mycon.Close();
}
请高手看看我的命令是不是出问题了

解决方案 »

  1.   

    确定你登录的用户,有对数据库update操作的权限。
      

  2.   

    确定你登录的用户,有对数据库update操作的权限。
      

  3.   

    确定你登录的用户,有对数据库update操作的权限。
      

  4.   

    mycom.CommandText = "GetAllAuthors";
    mycom.CommandType = CommandType.StoredProcedure;//指定的存储过程返回的可能不是单表数据查询,而使用
    SqlCommandBuilder cb = new SqlCommandBuilder();仅能自动处理单表查询对应的InsertCommand,UpdateCommand,DelteCommand没有贴 异常信息 ,以上供参考。请把异常贴出来,才有分析根据
      

  5.   

    有啊,administrator。郁闷,就是不知道哪个地方出的错,他不报错,但又更新不回数据库
      

  6.   

    使用StoredProcedure的时候最好自己写Command不要使用CommandBuilder还有就是你是否能保证你的数据表包含了你的更改状态 一般你可以将DataAdaper和你的DataTable或着DataSet保存起来 特别是Web
      

  7.   

    存储过程是没有问题的。里面就是一条 select * from authors   session里的值是有的,没有就报错了。我在程序的前面已经使用fill()方法把数据库的表存到session,但经过更改后就是就是无法更新回数据库。
      

  8.   

    你确定authors 表已经有主键或者定义了唯一列了么?如果没有是无法生成InsertCommand,UpdateCommand,DelteCommand的
    另外dt是否已经AcceptChanges()
    还有da.Update(dt);是否应该换成da.Update(d);
      

  9.   

    我只是修改了其他字段,并没有涉及到主键。
    我使用不使用AcceptChanges()方法都没有关系的,他根本就没有返回到数据库
    是否使getchanges()方法进行过滤,只是提高ado.net的效率问题。我都快哭死了。