C#中的DataAdapter.Update()方法出错

时间:2012-10-08 11:51:55

标签: c#

我正在尝试执行以下插入命令。但它不工作既不显示任何异常。谁能告诉我我做错了什么?

using System;
using System.Data;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Security.Principal;

public partial class StoredProcedures
{
   [Microsoft.SqlServer.Server.SqlProcedure]
   public static void USP_tempSP()
   {   
       DataTable objDataTable = new DataTable();
       using (SqlConnection objConn = new SqlConnection("context connection=true"))
       {
           objConn.Open();
           SqlCommand objCmd = new SqlCommand();
           objCmd.CommandType = CommandType.Text;

           objCmd.Connection = objConn;
           objCmd.CommandText = "select * from temp"; 
           /*table Structure
            *CREATE TABLE [dbo].[temp]([empname] [varchar](20)  NULL,[createdon] [datetime] NULL,[emp_id] [varchar](4) NULL
           ) 
           */
           SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCmd.CommandText, objConn);
           objDataAdapter.Fill(objDataTable);

           SqlCommand objCmd1 = new SqlCommand();
           objCmd1.CommandType = CommandType.Text;
           objCmd1.UpdatedRowSource = UpdateRowSource.None;
           objCmd.CommandText = "Insert into tempCopy " +
                               " values(@empname,@createdon)";
           /*table Structure
            *CREATE TABLE [dbo].[tempCopy]([empname] [varchar](20)  NULL,[createdon] [datetime] NULL
            ) 
           */
           objCmd.Parameters.Add("@empname", SqlDbType.VarChar, 20, objDataTable.Columns["empname"].ColumnName);
           objCmd.Parameters.Add("@createdon", SqlDbType.DateTime, 8, objDataTable.Columns["createdon"].ColumnName);

           SqlDataAdapter adpt = new SqlDataAdapter();
           adpt.InsertCommand = objCmd;
           adpt.UpdateBatchSize = objDataTable.Rows.Count;
           try
           {
               int recordsInserted = adpt.Update(objDataTable);
           }
           catch(Exception ex)
           {
               //
           }

           objConn.Close();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我真的认为这是因为您在跟踪其自身更改的表上使用更新方法。问题是没有任何改变。设置AcceptChangesDuringFill = false,以便将行标记为添加到表中。

       objConn.Open();
       /* This doesn't really do anything.
       SqlCommand objCmd = new SqlCommand();
       objCmd.CommandType = CommandType.Text;

       objCmd.Connection = objConn;
       objCmd.CommandText = "select * from temp"; */
       /*table Structure
        *CREATE TABLE [dbo].[temp]([empname] [varchar](20)  NULL,[createdon] [datetime] NULL,[emp_id] [varchar](4) NULL
       ) 
       */
       SqlDataAdapter objDataAdapter = new SqlDataAdapter("select * from temp", objConn);
       //Make sure we don't mark the new rows as unchanged
       objDataAdapter.AcceptChangesDuringFill = false;
       objDataAdapter.Fill(objDataTable);

       SqlCommand objCmd1 = new SqlCommand();
       objCmd1.CommandType = CommandType.Text;
       objCmd1.UpdatedRowSource = UpdateRowSource.None;
       //This is supposed to be objCmd1 !!
       objCmd1.CommandText = "Insert into tempCopy " +
                           " values(@empname,@createdon)";
       /*table Structure
        *CREATE TABLE [dbo].[tempCopy]([empname] [varchar](20)  NULL,[createdon] [datetime] NULL
        ) 
       */
       //This is supposed to be objCmd1 !!
       objCmd1.Parameters.Add("@empname", SqlDbType.VarChar, 20, objDataTable.Columns["empname"].ColumnName);
       objCmd1.Parameters.Add("@createdon", SqlDbType.DateTime, 8, objDataTable.Columns["createdon"].ColumnName);

       SqlDataAdapter adpt = new SqlDataAdapter();
       //This is supposed to be objCmd1 !!
       adpt.InsertCommand = objCmd1;
       adpt.UpdateBatchSize = objDataTable.Rows.Count;
       try
       {
           int recordsInserted = adpt.Update(objDataTable);
       }
       catch(Exception ex)
       {
           //
       }

       objConn.Close();

我认为这应该有效,你需要对变量命名更加小心。 objCmd毫无意义。如果命令是cmdSelectData和cmdInsertData,那么它会更加清晰。几乎所有东西都是一个对象,所以 obj 只会使代码混乱。