我的代碼有什麼問題......我嘗試了幾件事情,但一次又一次地得到相同的錯誤。數據庫中已有一個對象異常
任何幫助? 的代碼是:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[SubDomainName].ToString());
conn.Open();
string temptable = "CREATE TABLE [dbo].[Tmptablenew]([TicketID] [int] NULL,[TicketDescription][varchar](max) NULL,[TicketAssignedTo] [varchar](100) NULL,[TicketCreatedDate] [datetime] NULL,[TicketStatus][varchar](50),CRMConnectionID [int] NULL,[TicketUpdatedDate] [Datetime] NULL,img [varchar](500) NULL)";
SqlCommand cmd = new SqlCommand(temptable, conn);
cmd.ExecuteNonQuery();
SqlCommand cmmd = new SqlCommand("select * from Tickets", conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmmd);
DataTable dt1 = new DataTable("dt1");
adapter.Fill(dt1);
cmmd.ExecuteNonQuery();
//BulkCopy the data in the DataTable to the temp table
using (SqlBulkCopy bulk = new SqlBulkCopy(conn))
{
bulk.DestinationTableName = "Tmptablenew";
bulk.WriteToServer(result);
conn.Close();
}
conn.Open();
string mergeSql = "merge into Tickets as Target " +
"using Tmptablenew as Source " +
"on " +
"Target.TicketID= Source.TicketID " +
"and Target.CRMConnectionID = Source.CRMConnectionID " +
"when not matched then " +
"insert (TicketID,TicketDescription,TicketAssignedTo,TicketCreatedDate,TicketStatus,CRMConnectionID,TicketUpdatedDate,img) values (Source.TicketID,Source.TicketDescription,Source.TicketAssignedTo,Source.TicketCreatedDate,Source.TicketStatus,Source.CRMConnectionID,Source.TicketUpdatedDate,Source.img);";
string mergesql1 = "Update Tickets SET TicketDescription=S.TicketDescription, TicketAssignedTo = S.TicketAssignedTo, TicketStatus = S.TicketStatus,TicketUpdatedDate = S.TicketUpdatedDate,img = S.img FROM Tickets t JOIN Tmptablenew AS S ON t.TicketID = S.TicketID and T.CRMConnectionID = S.CRMConnectionID";
cmd.CommandText = mergeSql;
cmd.ExecuteNonQuery();
cmmd.CommandText = mergesql1;
cmmd.ExecuteNonQuery();
cmd.CommandText = "drop table Tmptablenew";
cmd.ExecuteNonQuery();
//Clean up the temp table
conn.Close();
做一些代碼並描述問題,請 – lenden 2014-09-26 12:46:43
什麼是_error_?在哪一行?我們需要更多的細節.. – 2014-09-26 12:49:03
很明顯,「數據庫中已有一個對象」。您無法創建與已存在的對象同名的對象。因此,要麼擺脫已經存在的或不要嘗試創建一個新的。 – David 2014-09-26 12:51:03