Hi

This question has been asked b4 on these newsgroups. I can't find a clear example

I have a byte array called signature. I'd like to store this byte array in a SQL server varbinary column and then read it back again. I have the following code snippet

Assumptions: sqlCon is a valid SQL connection initialized elsewher

public void InsertSalesTransaction(string SalesPersonID, byte[] signature


string sCmd = "INSERT INTO SalesTransactions "
sCmd += "(SalespersonID,Signature)"
sCmd += " VALUES ('" + SalesPersonID + "'," + ???

SqlCommand sqlCmd = new SqlCommand (sCmd, sqlCon)
sqlCmd.ExecuteNonQuery() > 0



HOW do you insert the signature byte array

Can someone provide me with an example

Thanks a lo

Re: Store byte array into SQL Server by Miha

Miha
Tue Apr 27 15:29:03 CDT 2004

Hi,

See this post:
http://groups.google.com/groups?q=sql+server+image+group:*adonet*+author:miha+author:markic&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=O6Yq%23tECEHA.4080%40TK2MSFTNGP09.phx.gbl&rnum=1

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Opa" <anonymous@discussions.microsoft.com> wrote in message
news:B0FC7EEA-A673-4DA0-9A6B-15D14B586FBC@microsoft.com...
> Hi,
>
> This question has been asked b4 on these newsgroups. I can't find a clear
example.
>
> I have a byte array called signature. I'd like to store this byte array
in a SQL server varbinary column and then read it back again. I have the
following code snippet:
>
> Assumptions: sqlCon is a valid SQL connection initialized elsewhere
>
> public void InsertSalesTransaction(string SalesPersonID, byte[] signature)
> {
>
> string sCmd = "INSERT INTO SalesTransactions ";
> sCmd += "(SalespersonID,Signature)";
> sCmd += " VALUES ('" + SalesPersonID + "'," + ???)
>
> SqlCommand sqlCmd = new SqlCommand (sCmd, sqlCon);
> sqlCmd.ExecuteNonQuery() > 0;
>
> }
>
>
> HOW do you insert the signature byte array?
>
> Can someone provide me with an example?
>
> Thanks a lot
>
>



Re: Store byte array into SQL Server by Bob

Bob
Tue Apr 27 15:31:15 CDT 2004

You can use the sql command as a stored procedure type, and add it to the
parameter collection directly (see the docs for more info on how to use the
stored procedure type). But if you can't use a stored procedure and have to
assemble the SQL yourself, then use this:

'NOTE: don't quote this value or SQL Server will see this value as
varchar
Public Shared Function GetVarbinaryStringFromByteArray(ByVal Bytes() As
Byte) As String
Dim ret As String
If Bytes Is Nothing OrElse Bytes.GetUpperBound(0) < 0 Then
ret = "NULL"
Else
ret = "0x" & System.BitConverter.ToString(Bytes).Replace("-",
"")
End If
Return ret
End Function

Bob

"Opa" <anonymous@discussions.microsoft.com> wrote in message
news:B0FC7EEA-A673-4DA0-9A6B-15D14B586FBC@microsoft.com...
> Hi,
>
> This question has been asked b4 on these newsgroups. I can't find a clear
example.
>
> I have a byte array called signature. I'd like to store this byte array
in a SQL server varbinary column and then read it back again. I have the
following code snippet:
>
> Assumptions: sqlCon is a valid SQL connection initialized elsewhere
>
> public void InsertSalesTransaction(string SalesPersonID, byte[] signature)
> {
>
> string sCmd = "INSERT INTO SalesTransactions ";
> sCmd += "(SalespersonID,Signature)";
> sCmd += " VALUES ('" + SalesPersonID + "'," + ???)
>
> SqlCommand sqlCmd = new SqlCommand (sCmd, sqlCon);
> sqlCmd.ExecuteNonQuery() > 0;
>
> }
>
>
> HOW do you insert the signature byte array?
>
> Can someone provide me with an example?
>
> Thanks a lot
>
>


Re: Store byte array into SQL Server by anonymous

anonymous
Tue Apr 27 16:51:01 CDT 2004

The code that's in that post use a file stream. I'm not using a file, but a normatl byte array.

Re: Store byte array into SQL Server by Miha

Miha
Wed Apr 28 02:48:23 CDT 2004

The code in
http://support.microsoft.com/default.aspx?scid=kb;en-us;309158&Product=vcSnet
uses streams to read and write byte arrays.
For example,
FileStream fs = new FileStream(@"C:\winnt\Gone Fishing.BMP",
FileMode.OpenOrCreate, FileAccess.Read);
byte[] MyData= new byte[fs.Length];
fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();

change to:
byte[] MyData = yourBytes;

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"Opa" <anonymous@discussions.microsoft.com> wrote in message
news:9D1685B4-AFC9-451C-9FB7-79267231C71B@microsoft.com...
> The code that's in that post use a file stream. I'm not using a file,
but a normatl byte array.



Re: Store byte array into SQL Server by Jon

Jon
Wed Apr 28 02:51:42 CDT 2004

Opa <anonymous@discussions.microsoft.com> wrote:
> The code that's in that post use a file stream. I'm not using a file,
> but a normatl byte array.

Looking at the article "HOW TO: Read and Write BLOB Data by Using
ADO.NET with Visual C# .NET", it happens to read the byte array from a
file stream, but all the actual ADO.NET-related code is dealing with
byte arrays.

Also see my post in another group where you posted an identical
question. Please don't multi-post - it splits the discussion up.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too