我的代码有什么问题......我尝试了几件事,但一次又一次地得到同样的错误。
任何帮助? 守则是:
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();
答案 0 :(得分:1)
您正在创建表Tmptablenew
,看起来这个表alredy存在于数据库中。
我认为,您正在尝试创建一个临时表,在这种情况下,为MySQL创建CREATE TEMPORARY TABLE
或为MS SQL使用表名#Tmptablenew
。同样在这种情况下,会话关闭时会自动删除。
修改强>
因此,如果您使用 MySQL (根据您的问题标记),则必须更改string temptable = "CREATE TEMPORARY TABLE [dbo].[Tmptablenew]([TicketID]...
等等。或者,如果它是 MS SQL (我怀疑),那么string temptable = "CREATE TABLE [dbo].[#Tmptablenew]([TicketID]...
等等(在这种情况下,您应该在代码中的任何位置命名为#Tmptablenew
。)