Login or Sign Up
Logging in...
Remember me
Log in
Or
Sign Up
Forgot password or user name?
Log in with
Search in titles only
Search in C Sharp only
Search
Advanced Search
Forums
BYTES
Product Launch
Updates
Developer Toolkit
Today's Posts
Member List
Calendar
Home
Forum
Topic
C Sharp
System.Byte[] in vs2005
Collapse
This topic is closed.
X
X
Collapse
Posts
Latest Activity
Photos
Page
of
1
Filter
Time
All Time
Today
Last Week
Last Month
Show
All
Discussions only
Photos only
Videos only
Links only
Polls only
Events only
Filtered by:
Clear All
new posts
Previous
template
Next
=?Utf-8?B?Unlhbg==?=
#1
System.Byte[] in vs2005
Sep 22 '08, 11:25 PM
How do I read a blob field of a database using System.Byte[]?
A sample code would be appreciated.
Paul Musso
#2
Sep 22 '08, 11:25 PM
Re: System.Byte[] in vs2005
Ryan avait énoncé :
How do I read a blob field of a database using System.Byte[]?
A sample code would be appreciated.
Hi Ryan,
This is an example
Byte[] blob = null;
FileStream fs = null;
const string sConn = "server=(local) ;Initial
Catalog=Northwi nd;UID=ctester; PWD=password";
try {
SqlConnection conn = new SqlConnection(s Conn);
SqlCommand cmd = new SqlCommand("SEL ECT Picture FROM Categories WHERE
CategoryName='B uilder'", conn);
cn.Open();
SqlDataReader sdr = cmd.ExecuteRead er();
sdr.Read();
blob = new Byte[(sdr.GetBytes(0 , 0, null, 0, int.MaxValue))];
sdr.GetBytes[0, 0, blob, 0, blob.Length);
sdr.Close();
conn.Close();
fs = new FileStream("c:\ \Builder.doc", FileMode.Create ,
FileAccess.Writ e);
fs.Write(blob, 0, blob.Length);
fs.Close();
} catch (SqlException e){
Console.WriteLi ne("SQL Exception: " + e.Message);
} catch (Exception e) {
Console.WriteLi ne("Exception: "+ e.Message);
}
Sources :
Com.com - Your Search Starts Here
http://articles.techrepublic.com.com/5100-10878_11-5766889.html
--
Paul Musso
Comment
Post
Cancel
DrewCE
#3
Sep 23 '08, 04:05 AM
Re: System.Byte[] in vs2005
Building on Paul's example; I'd prefer to make use of the "using" statement
to make sure we don't leave anything open...
const string sConn =
"server=(local) ;InitialCatalog =Northwind;UID= ctester;PWD=pas sword";
try
{
Byte[] blob = null;
using (SqlConnection conn = new SqlConnection(s Conn))
{
conn.Open();
SqlCommand cmd = new SqlCommand("SEL ECT Picture FROM
Categories WHERE CategoryName='B uilder'", conn);
using (SqlDataReader sdr = cmd.ExecuteRead er())
{
sdr.Read();
blob = new Byte[(sdr.GetBytes(0 , 0, null, 0,
int.MaxValue))];
sdr.GetBytes(0, 0, blob, 0, blob.Length);
}
}
using (FileStream fs = new FileStream("c:\ \Builder.doc",
FileMode.Create , FileAccess.Writ e))
{
fs.Write(blob, 0, blob.Length);
}
}
catch (SqlException e)
{
Console.WriteLi ne("SQL Exception: " + e.Message);
}
catch (Exception e)
{
Console.WriteLi ne("Exception: " + e.Message);
}
Keep in mind, I didn't verfiy the code is correct. Just added use of
"using".
-Drew
"Paul Musso" <paulm@exakis.c omwrote in message
news:%23ugtcmQH JHA.2156@TK2MSF TNGP05.phx.gbl. ..
Ryan avait énoncé :
>How do I read a blob field of a database using System.Byte[]?
>A sample code would be appreciated.
>
Hi Ryan,
>
This is an example
>
Byte[] blob = null;
FileStream fs = null;
const string sConn = "server=(local) ;Initial
Catalog=Northwi nd;UID=ctester; PWD=password";
try {
SqlConnection conn = new SqlConnection(s Conn);
SqlCommand cmd = new SqlCommand("SEL ECT Picture FROM Categories WHERE
CategoryName='B uilder'", conn);
cn.Open();
SqlDataReader sdr = cmd.ExecuteRead er();
sdr.Read();
>
blob = new Byte[(sdr.GetBytes(0 , 0, null, 0, int.MaxValue))];
sdr.GetBytes[0, 0, blob, 0, blob.Length);
sdr.Close();
conn.Close();
fs = new FileStream("c:\ \Builder.doc", FileMode.Create , FileAccess.Writ e);
>
fs.Write(blob, 0, blob.Length);
fs.Close();
} catch (SqlException e){
Console.WriteLi ne("SQL Exception: " + e.Message);
} catch (Exception e) {
Console.WriteLi ne("Exception: "+ e.Message);
}
>
>
Sources :
Com.com - Your Search Starts Here
http://articles.techrepublic.com.com/5100-10878_11-5766889.html
>
--
Paul Musso
>
>
Comment
Post
Cancel
Marc Gravell
#4
Sep 23 '08, 07:15 AM
Re: System.Byte[] in vs2005
Further to the other replies; if the BLOB is large, you should use
sequential access:
http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/msg/fcd173f1db2951f1
Marc
Comment
Post
Cancel
=?Utf-8?B?Unlhbg==?=
#5
Sep 23 '08, 04:45 PM
Re: System.Byte[] in vs2005
Thanks to you all; i will try these examples and update you soon.
"Marc Gravell" wrote:
Further to the other replies; if the BLOB is large, you should use
sequential access:
>
http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/msg/fcd173f1db2951f1
>
Marc
>
Comment
Post
Cancel
Previous
template
Next
Working...
Yes
No
OK
OK
Cancel
👍
👎
☕
Comment