.NET Forum / .NET Framework / ADO.NET / March 2008
Getbytes usage from VS 2003 to VS2005
|
|
Thread rating:  |
Jeff - 25 Mar 2008 21:31 GMT When accessing a BLOB from a DB using getbytes, 0 bytes are returned. The same thing worked with VS2003. The recent environmental change was from VS2003 to VS2005 and also from .NET 1.1 to .NET 2.0.
/* Here is the function that reads the blob from the db */
private bool GetImageSeq(ref byte[] SeqImage,string PatId,string AdmDate,int ImgSeq,int SeqSz) {
long RtnBytes=0,startIndex=0;
string MyStr="",SqlStr="";
SqlStr = "SELECT binimage FROM PUB.Images WHERE patid = '" + PatId + "' AND admdate = '" + AdmDate + "' AND imgseq = '" + ImgSeq.ToString() + "'";
OdbcConnection ImgConn = new OdbcConnection(Session["ConnStr"].ToString());
OdbcCommand ImgSel = new OdbcCommand(SqlStr,ImgConn);
ImgConn.Open();
OdbcDataReader ImgRead;
if (ImgConn.State.ToString() != "Open") return(false); else { try { ImgRead = ImgSel.ExecuteReader(CommandBehavior.SequentialAccess); MyStr = ImgRead.FieldCount.ToString(); //ImgRead.RecordsAffected.ToString(); ImgRead.Read(); /* with .net 1.1, RtnBytes equaled the blob size. In .net 2.0, it is zero bytes getting returned. */
RtnBytes = ImgRead.GetBytes(0,startIndex,SeqImage,0,SeqSz); ImgRead.Close();
} catch (Exception ex) { MyStr = ex.Message; }
}
ImgConn.Close();
return(true); }
Wen Yuan Wang [MSFT] - 26 Mar 2008 14:03 GMT Hello AIMDeveloper,
According to your description, you face an issue that ODBCDataReader.GetBytes(0,startIndex,SeqImage,0,SeqSz) returns Zero after you changed environment from .net 1.1 to .net 2.0, correct? If I misunderstood anything here, please don't hesitate to correct me.
As far as I know, all break-change in system.data in .net 2.0 runtime has been listed in the following article. But, I cannot getbytes method in it. http://msdn2.microsoft.com/en-us/netframework/aa497245.aspx [System.Data Run-Time Breaking Changes]
In general, OdbcDataReader.GetBytes method returns the actual number of bytes read. This should be the same value as SeqSz. Have you checked the SeqImage after GetBytes method? Is there any data in SeqImage?
RtnBytes = ImgRead.g.GetBytes(0, startIndex, SeqImage, 0, SeqSz); System.Console.Write(SeqImage.Length);
At last, could you let me know where your application gets data? Is it Excel or Access Database? I will try to reproduce the issue on my side.
Please feel free to let us know if you have any more concern. We are glad to assist you. Have a great day, Best regards, Wen Yuan
Microsoft Online Community Support Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msdnmg@microsoft.com. ================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif ications. Note: The MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 1 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions or complex project analysis and dump analysis issues. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/subscriptions/support/default.aspx. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights.
Jeff - 26 Mar 2008 15:31 GMT Thanks for your response Wen,
The SeqImage.Length for this instance would be 500,000. But that's what it is declared at in the a function called GetImageDb. The byte array itself,
is just a bunch of empty bytes.
The size of the blob data is stored in the database table in a field called SeqSz. We convert that to an int, and then define a new byte array of that size.
SeqSz = Convert.ToInt32(r["seqsz"].ToString());
byte[] SeqByte = new Byte[SeqSz];
The database we are using is a Progress database version 10.0B.
The odbc driver is:
DataDirect 4.20 32-BIT OpenEdge SQL v10.0B
This function (GetImageDb) calls the GetImageSeq function.
private bool GetImageDb(int ImageSize,ref byte[] PatImage,string PatId,string AdmDate)
{
DataSet dsImage = new DataSet();
string ConnStr;
int Result = 0;
int SeqSz=0,SeqNum,StartByte;
if (Session["ConnStr"] == null)
return(false);
ConnStr = Session["ConnStr"].ToString();
string SqlStr="";
SqlStr = "SELECT patid,admdate,imagesize,imgseq,seqsz FROM PUB.Images WHERE patid = '" + PatId +
"' AND admdate = '" + AdmDate + "'" ;
OdbcDataAdapter daApp = new OdbcDataAdapter(SqlStr,ConnStr);
daApp.SelectCommand.Parameters.Add("patid",OdbcType.VarChar);
daApp.SelectCommand.Parameters.Add("admdate",OdbcType.Date);
daApp.SelectCommand.Parameters["patid"].Value = PatId;
daApp.SelectCommand.Parameters["admdate"].Value = AdmDate;
try
{
Result = daApp.Fill(dsImage,"images");
}
catch (Exception ex)
{
lblError.Text = ex.Message;
return(false);
}
if (Result > 0)
{
foreach (DataRow r in dsImage.Tables["images"].Rows)
{
try
{
SeqSz = Convert.ToInt32(r["seqsz"].ToString());
SeqNum = Convert.ToInt16(r["imgseq"].ToString());
StartByte = (SeqNum - 1) * 500000;
byte[] SeqByte = new Byte[SeqSz];
GetImageSeq(ref SeqByte,PatId,AdmDate,SeqNum,SeqSz);
SeqByte.CopyTo(PatImage,StartByte);
//BuildImage(ref PatImage,SeqByte,StartByte,SeqSz);
}
catch (Exception ex)
{
lblError.Text = ex.Message;
return(false);
}
}
return(true);
}
else
return(false);
}
> Hello AIMDeveloper, > [quoted text clipped - 47 lines] > ================================================== > This posting is provided "AS IS" with no warranties, and confers no rights. davebythesea - 27 Mar 2008 15:18 GMT Does this work for you -
private bool GetImageSeq(ref byte[] SeqImage, string PatId, string AdmDate, int ImgSeq, int SeqSz) { string SqlStr = "";
SqlStr = "SELECT binimage FROM PUB.Images WHERE patid = '" + PatId + "' AND admdate = '" + AdmDate + "' AND imgseq = '" + ImgSeq.ToString() + "'";
OdbcConnection ImgConn = new OdbcConnection(Session["ConnStr"].ToString());
OdbcCommand ImgSel = new OdbcCommand(SqlStr, ImgConn);
try { ImgConn.Open();
byte[] bytes = (byte[])ImgSel.ExecuteScalar();
if (bytes.Length > 0) { SeqImage = bytes; return true; } else { return false; } } catch(Exception ex) { //
return false; } finally { ImgSel.Dispose(); ImgConn.Close(); ImgConn.Dispose(); } }
> Thanks for your response Wen, > [quoted text clipped - 184 lines] > > ================================================== > > This posting is provided "AS IS" with no warranties, and confers no rights. Jeff - 27 Mar 2008 15:43 GMT Thanks for your response, Dave
No, the bytes field is null. bytes is null after the ExecuteScalar call and then the bytes.Length > 0 throws an object reference has not been set.
> Does this work for you - > [quoted text clipped - 231 lines] > > > ================================================== > > > This posting is provided "AS IS" with no warranties, and confers no rights. davebythesea - 27 Mar 2008 16:22 GMT Well, some extra error handling at least -
try { ImgConn.Open();
byte[] bytes = (byte[])ImgSel.ExecuteScalar();
if (bytes != null) { if (bytes.Length > 0) { SeqImage = bytes; return true; } else { return false; } } else { return false; } }
Not sure why that fails. Casting the result to a byte[] works for me using the System.Data.OracleClient classes. Dunno if it would be different for OdBc. Is there definetly binary data being returned by the query?
> Thanks for your response, Dave > [quoted text clipped - 236 lines] > > > > ================================================== > > > > This posting is provided "AS IS" with no warranties, and confers no rights. Wen Yuan Wang [MSFT] - 28 Mar 2008 11:27 GMT Hello Jeff,
Thanks for Dave's tip. Odbccomand.ExecuteScalar() should return the bytes in the first filed. If you found it's null and the length of byte array is 0, this means odbcDataCommand cannot found any byte in underling datatable.
I'm sorry, but it's not possible for me to install Progress database version 10.0B on my machine. It's a 3rd party product. (I think it's Excel or Access database before...). We suggest you may check if there is any binary data in underling database table. odbcdatareader.GetBytes() returns the actual number of bytes read. If the value is zero, I believe this is because odbcdatareader cannot read data from table. This shouldn't be a breaking change...
Hope this helps. If you have any more concern, please feel free to let us know. We are glad to assist you. Have a great day, Best regards, Wen Yuan
Microsoft Online Community Support Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msdnmg@microsoft.com. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|