Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Passing binary data into database
Message
 
 
À
11/04/2016 23:53:41
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 5.0
OS:
Windows 10
Database:
MS SQL Server
Divers
Thread ID:
01634652
Message ID:
01634686
Vues:
36
>>I also asked a related question here
>>
>>https://social.msdn.microsoft.com/Forums/sqlserver/en-US/bfc4fd55-a215-4f18-b7b2-09185abecd78/dataadapter-doesnt-seem-to-read-the-data?forum=sqldataaccess
>>
>>and here
>>
>>https://social.msdn.microsoft.com/Forums/sqlserver/en-US/a80db55a-ae29-44d3-b295-11aede6a907a/sending-binary-data-to-sql-server?forum=sqldataaccess
>
>If the data is binary - stay away from chars/string - use a byte array
>
>
>parameter.Value = parameterValue // I expect parameterValue  will be a byte[]
>
parameterValue is a string. Here is a sample of what we're passing:
String input = @"<func>ModifyRec</func><ReturnLog>Yes</ReturnLog><tcoperator>ADMIN</tcoperator>
                           <tcsalespoint>AUTK50017001</tcsalespoint><tctablename>CONTACTPHOTOS</tctablename>
<tvPrimaryKey>5000001</tvPrimaryKey><tvprimarykeytype>N</tvprimarykeytype>
<tcrecorddata>DATEADDED 0001908/04/2009 17:12:58"+

         "PHOTO     00010\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09PHOTOID   000075000001</tcrecorddata>";
I'm not 100% sure I constructed the value correctly for the tests (I want to pass in binary 0123456789).

The program reads the tcRecordData using column's name followed by the size of the column into a string.

This is the code (all of this was programmed by my colleague):
public Dictionary<String, String> GetColumnDictionary(String tableName, String rawText, Boolean ignoreNonexistentTableColumns = false)
        {
            // Parse out the 10/5/Length pairs and add spaces
            Dictionary<String, String> columnDictionary = new Dictionary<String, String>(StringComparer.OrdinalIgnoreCase);

            if (String.IsNullOrWhiteSpace(rawText))
                return columnDictionary;

            // Loop through the raw text and create key value pairs for the column name and value
            int indexPointer = 0;
            Boolean includeColumn = true;
            String messageText = "";
            while (indexPointer < rawText.Length)
            {
                String columnName = rawText.Substring(indexPointer, 10).TrimEnd();
                String columnLength = rawText.Substring(indexPointer + 10, 5);
                int columnLengthValue = Convert.ToInt32(columnLength);
                String columnValue = rawText.Substring(indexPointer + 15, columnLengthValue);
                indexPointer += 15;
                indexPointer += columnLengthValue;

                includeColumn = (ignoreNonexistentTableColumns) ? this.database.GetColumnType(tableName, columnName, ref messageText) != null : true;
                if (includeColumn)
                    columnDictionary.Add(columnName, columnValue);
            }

            return columnDictionary;
        }
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform