Store byte array into SQL Server

G

Guest

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
 
M

Miha Markic [MVP C#]

Hi,

See this post:
http://groups.google.com/groups?q=s...=O6Yq#[email protected]&rnum=1

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

Opa said:
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
 
B

Bob

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 said:
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
 
G

Guest

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

Miha Markic [MVP C#]

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 said:
The code that's in that post use a file stream. I'm not using a file,
but a normatl byte array.
 
J

Jon Skeet [C# MVP]

Opa said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top