|
|
Question : bulkCopy.WriteToServer(<Da<wbr />taTable>) problem
|
|
HI Guys I am trying to do a bulk insrert using the System.Data.SqlClient BulkCopy class Bulk Copy is done against a BL_CHAT table created by ----start----- CREATE TABLE [dbo].[BL_CHAT]( [COMMUNITY_ID] Bigint NOT NULL, [MSG_ID] Bigint IDENTITY(1,1) NOT NULL, [SENT_AT] DateTime NULL DEFAULT (getdate()), [SENDER] Bigint NOT NULL, [CHAT_MSG] nvarchar(max) NOT NULL, PRIMARY KEY NONCLUSTERED ( [COMMUNITY_ID] ASC, [MSG_ID] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY]
GO ----end----- Datatable is initialized by : dataTable = new DataTable(ChatConstants.CHAT_TABLE); // initialize data table DataColumn communityId = new DataColumn (ChatConstants.ChatTable.COMMUNITY_ID); DataColumn sentAt = new DataColumn (ChatConstants.ChatTable.SENT_AT); DataColumn sender = new DataColumn (ChatConstants.ChatTable.SENDER); DataColumn message = new DataColumn (ChatConstants.ChatTable.CHAT_MSG); dataTable.Columns.Add(communityId); dataTable.Columns.Add(sentAt); dataTable.Columns.Add(sender); dataTable.Columns.Add(message);
where constants are :
public static string CHAT_TABLE = "BL_CHAT"; public class ChatTable { // Chat table columns public static string COMMUNITY_ID = "COMMUNITY_ID"; public static string SENT_AT = "SENT_AT"; public static string SENDER = "SENDER"; public static string CHAT_MSG = "CHAT_MSG";
}
Now, when I am calling bulkCopy.WriteToServer on that DataTable , I have got the following error : ---error strat--- The given value of type String from the data source cannot be converted to type datetime of the specified target column. ---error end---
It seems that the fact that I am not mapping all the columns ( since the MSG_ID is IDENTITY column), something is getting wrong and the CHAT_MSG value is mapped to the SENT_AT
Is there a way to map explicitly DataTable columns to DB columns ??? Any suggestions ???
|
Answer : bulkCopy.WriteToServer(<Da<wbr />taTable>) problem
|
|
Try using column mappings as defined in this example:
SqlBulkCopy BCP = new SqlBulkCopy(conTarget); BCP.DestinationTableName = "CustomersShort"; BCP.ColumnMappings.Add("CustomerID", "ID"); BCP.ColumnMappings.Add("CompanyName", "Company"); BCP.ColumnMappings.Add("ContactName", "Contact");
Bob
|
|
|
|
|