Re: SqlDataReader.GetBytes Results in an System.InvalidCastExcepti
- From: "Cor Ligthert[MVP]" <Notmyfirstname@xxxxxxxxx>
- Date: Thu, 7 May 2009 18:29:21 +0200
Hotwheels,
For the bytes arrays you need a memorystreem to convert them to images
http://msdn.microsoft.com/en-us/library/system.io.memorystream(VS.80).aspx
Cor
"Hotwheels" <Hotwheels@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:6513E2BB-B3CF-49A4-B0CC-3EF9F933B194@xxxxxxxxxxxxxxxx
What I am trying to do is download the Attachments dirctly from Microsoft CRM
Interaction Table ActivityMimeAttachment. The code below does work for 70%
of the Attachments that have a type of 'application/octet-stream'. But it
does not work for the other 30% that have a type of image/jpeg,
application/pdf, image/gif, application/vnd.ms-excel, application/msword,
text/plain. Is there someplace where these MimeTypes are Defined So I can
convert them.
Help
private void SqlBlob2File(string DestFilePath, string id)
{
SqlConnection cn = new SqlConnection("server=localhost;integrated
security=yes;database=RelevantSales_MSCRM");
SqlCommand cmd = new SqlCommand("SELECT Body FROM ActivityMimeAttachment
WHERE ActivityMimeAttachmentID = '852161B4-D911-DB11-9EE5-0011434DCFE4'", cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
bool bResult = dr.Read();
FileStream fs = new FileStream(DestFilePath, System.IO.FileMode.Create,
System.IO.FileAccess.Write);
try
{
byte[] value;
string s8Data;
s8Data = dr.GetString(0);
value = System.Convert.FromBase64String(s8Data);
fs.Write(value, 0, value.Length);
dr.Close();
cn.Close();
fs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
dr.Close();
cn.Close();
fs.Close();
}
}
"Cor Ligthert[MVP]" wrote:
Hi,
Maybe it is easier as you tell the goal you want to achieve too.
Do you want to read a part of text, do you want to retrieve an image etc.
Cor
"Hotwheels" <Hotwheels@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:ADDD20B4-30D9-4818-A489-B56B7E46B9EB@xxxxxxxxxxxxxxxx
> Trying to write a routine to take bytes from a text field and put them
> into a
> file.
>
> I am using VS 2003 and SQL 2005. I created a table called TestText and
> added two fields ID as a UID and MyText of type Text.
>
> I then added one row with the ID 8ac11006-26ad-da11-9ec8-00059a3c7800 > and
> MyText value of "1234".
>
> If I put dr.GetValue(0) in the watch area it show the correct result of
> "1234".
>
> So the DataReader is working correct.
>
> What am I doing wrong with SqlDataReader.GetBytes Method?
>
> Also how would I wirte the command if I just wanted return the length > of
> bytes
> example maybe "long retval = dr.GetBytes(0, 0, null, 0, 0);"
>
> Thanks
> Mike
>
> private void SqlBlob2File(string DestFilePath, string id)
> {
>
> SqlConnection cn = new SqlConnection("server=localhost;integrated
> security=yes;database=RelevantSales_MSCRM");
> SqlCommand cmd = new SqlCommand("SELECT MyText FROM TestText WHERE ID > =
> '8ac11006-26ad-da11-9ec8-00059a3c7800'", cn);
> cn.Open();
> SqlDataReader dr = cmd.ExecuteReader();
> bool bResult = dr.Read();
> FileStream fs = new FileStream(DestFilePath, > System.IO.FileMode.Create,
> System.IO.FileAccess.Write);
> try
> {
> byte[] value = new byte[8];
> long retval = dr.GetBytes(0, 0, value, 0, value.Length);
> fs.Write(value, 0, value.Length);
>
> dr.Close();
> cn.Close();
>
> fs.Close();
> }
> catch (Exception ex)
> {
> MessageBox.Show(ex.Message);
> dr.Close();
> cn.Close();
>
> fs.Close();
> }
> }
.
- References:
- SqlDataReader.GetBytes Results in an System.InvalidCastException E
- From: Hotwheels
- Re: SqlDataReader.GetBytes Results in an System.InvalidCastException E
- From: Cor Ligthert[MVP]
- Re: SqlDataReader.GetBytes Results in an System.InvalidCastExcepti
- From: Hotwheels
- SqlDataReader.GetBytes Results in an System.InvalidCastException E
- Prev by Date: Re: Running stored procedure that has default values
- Next by Date: Re: Datagridview and collection in VB.net
- Previous by thread: Re: SqlDataReader.GetBytes Results in an System.InvalidCastExcepti
- Next by thread: Datagridview and collection in VB.net
- Index(es):
Relevant Pages
|