Wrox Programmer Forums
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 20th, 2008, 01:26 PM
mlw mlw is offline
Registered User
 
Join Date: Jun 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Guids

I have a character 36 column in a data table that contains a guid value imported via a data adapter from another database and am trying to use bulk copy class to write table out to server, but I get an error when bcp tries to convert the char column to a uniqueidentifier data type.

What can I do to make this work? The bcp class does not have any options on casting columns and what not it is all seamless. Do I need to query the incoming column as any specific data type? Bad news is the source database is foxpro.

 
Old June 23rd, 2008, 11:53 AM
mlw mlw is offline
Registered User
 
Join Date: Jun 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Anyone?

 
Old June 23rd, 2008, 12:47 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Perhaps you could provide us with the exact error you are getting. Also, what class exactly are you using? System.???

-Peter
compiledthoughts.com
 
Old June 23rd, 2008, 02:50 PM
mlw mlw is offline
Registered User
 
Join Date: Jun 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is the code that executes. I get an error on the WriteToServer()call. The error is: "The given value of type Byte[] from the data source cannot be converted to type uniqueidentifier of the specified target column."

The source column is either a Character(36 ) type or a binary type ( I have tried both and get same error ). The column holds a GUID value generated in Foxpro and I want to move that value to a uniqueidentifier column in bulk with other columns.


using (SqlBulkCopy bcp = new SqlBulkCopy(sqlConn))
                    {
                        bcp.DestinationTableName = TableName;
                        bcp.BulkCopyTimeout = 120;
                        bcp.ColumnMappings.Clear();

                        // get alist of fields to ensure column mapping
                        var query =
                            from fields in dtFields.AsEnumerable()
                            where PcsPublics.JustStem(fields.Field<String>("cobjectn am").Trim()) == TableName
                            select new
                            {
                                FieldName = PcsPublics.JustExt(fields.Field<String>("cobjectna m"))
                            };


                        //create a var named fieldDefs that is a collection ( query result ) stored in query
                        foreach (var fieldDefs in query)
                            bcp.ColumnMappings.Add(fieldDefs.FieldName.Trim(), fieldDefs.FieldName.Trim());

                        log.WriteLine("Importing data into Sql table " + TableName);
                        log.WriteLine("---");
                        log.WriteLine("");
                        log.Flush();
                        // label3.Text = "Importing data into Sql table " + TableName + "...";
                        // label3.Refresh();
                        try
                        {
                            bcp.WriteToServer(dsPcs.Tables[TableName]);

                            dsPcs.Tables.Remove(TableName);
                            dsPcs.AcceptChanges();
                        }
                        catch (InvalidOperationException oex)
                        {
                            log.WriteLine("Bulk Copy error: " + oex.Message);
                        }

 
Old June 23rd, 2008, 03:31 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I think that SQL Server expects incoming values for guids to be strings. You'll probably need to convert the character byte to a string. It appears the .NET constructors for System.Guid can accept a byte array, but it's 16 bytes, versus 32 chars.

-Peter
compiledthoughts.com
 
Old June 23rd, 2008, 03:52 PM
mlw mlw is offline
Registered User
 
Join Date: Jun 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks.

I have tried bringing the original data in as a character 36 in the data table and still get the error, so I'll try manipulating the data table in the dataset before I call the Bulk Copy method.






Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I populate large table with unique Guids? ExDb BOOK: Beginning VB.NET Databases 9 August 23rd, 2007 04:18 AM
Where are the GUIDS Bob Bedell Pro VB 6 2 January 2nd, 2006 10:49 AM
Discovering ProgId’s and/or GUIDs from unregistere jlick Pro VB 6 0 November 13th, 2003 01:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.