【发布时间】:2014-11-21 11:41:01
【问题描述】:
我的代码出了什么问题...我尝试了几件事,但一次又一次地遇到同样的错误。
有什么帮助吗?? 代码是:
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();
【问题讨论】:
-
请用代码做一些事情并描述问题
-
什么是错误?在哪条线上?我们需要更多细节..
-
显然“数据库中已经有一个对象”。您不能创建与已经存在的对象同名的对象。所以要么摆脱一个已经存在的,要么不要尝试创建一个新的。
-
获取您的内联 SQL 并使用 SQL 管理工作室中的值执行它 - 我猜您的合并事务有问题。我也会把它移到一个存储过程中。
-
只有一行试图创建任何东西,这是第三行。
Tmptablenew已经存在。
标签: c# mysql sqlbulkcopy