0

我正在尝试按照 Insert Command 进行操作。但它不起作用,也没有显示任何异常。谁能告诉我我在哪里做错事?

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();
        }
    }
}
4

1 回答 1

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只是把代码弄乱了。

于 2012-10-09T08:38:02.210 回答