H
Hotwheels
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();
}
}
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();
}
}