OLE Object to float array

P

Peter

I have OLE Object field in Access Database. This field contains an array of
floats. The array was moved from memory into a string and the string was
saved as OLE Object in a database (That was written in VB6). This data was
used to create graphs, instead creating a record for each point the entire
array was saved in one field.

How do I move the same data from database field in to float array suing C# ?

Thank You


Peter
 
M

MIH

I have tried the following code.
Let me know whether it works for you
-----------------------
database:
C:\OLEToFloatArray.mdb
-----------------------
table definiton:
create table tblFloatArray(
FloatArray OLEObject
)
-----------------------
private void button1_Click(object sender, EventArgs e)
{
insertIntotblFloatArray();

float []arr=selectFromtblFloatArray();

}
public void insertIntotblFloatArray()
{
OleDbCommand cmd = new OleDbCommand("insert into tblFloatArray
(FloatArray) values('1.5,3.5,6.7,3.2');", new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\OLEToFloatArray.mdb"));
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
}

public float[] selectFromtblFloatArray()
{
float[] retValue = null;
OleDbCommand cmd = new OleDbCommand("select FloatArray from
tblFloatArray ", new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\OLEToFloatArray.mdb"));

cmd.Connection.Open();
OleDbDataReader dr = cmd.ExecuteReader();

if (dr.Read())
{
byte[] data = (byte[])dr["FloatArray"];
string temp =
System.Text.UnicodeEncoding.Unicode.GetString(data);
char[]Separator={','};
string [] arrStr= temp.Split(Separator);
retValue = new float[arrStr.Length];
int index = 0;
foreach (string str in arrStr)
{
retValue[index++] = float.Parse(str);
}


}
cmd.Connection.Close();
return retValue;
}
 
J

Jialiang Ge [MSFT]

Good morning Peter,

First of all, please help to check whether I understand the question
rightly or not:

***** PROBLEM DESCRIPTION *****
We used VB6 to store a float array into an OLE Object field of a Access DB.
VB6: float array -> string (or a byte array) -> OLE Object field of a
Access DB.
Our need is to retrieve the float array from the OLE Object field using C#.
C#: OLE Object field of the Access DB -> string (or a byte array) -> float
array.

Am I right?

*** PHASE 1. OLE Object field of the Access DB -> string (or a byte array)
***

In order to get the byte array (or the string) from the DB using C#, we
need to use the System.Data.OleDb namespace. Here is an example:

// the connection string of the access database.
string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\db3.mdb";
OleDbConnection conn = new OleDbConnection(connStr);
conn.Open(); // open the connection

// the sql command
string query = "SELECT * FROM tblTest";
OleDbCommand cmd = new OleDbCommand(query, conn);

// read the data
while (rdr.Read())
{
byte[] arr;
// the ole object field returns as a byte array.
arr = rdr["ObjValue"] as byte[];
// if you need the string value, we can use Encoding.GetString to
parse the byte array to a string.
string val = System.Text.Encoding.Unicode.GetString(arr);
}
// close the reader and the connection.
rdr.Close();
conn.Close();

The first line of the example sets the connection string of the access
database. If you are using Access 2007, please refer to
http://www.connectionstrings.com/?carrier=access2007, otherwise, see
http://www.connectionstrings.com/?carrier=access.

The code line arr = rdr["ObjValue"] as byte[]; and string val =
System.Text.Encoding.Unicode.GetString(arr); retrieves the value of the ole
object and put it in a byte array or a string object.

*** PHASE 2. string (or a byte array) -> float array ***

This phase of conversion depends on your algorithm of "float array ->
string (or a byte array)" in VB6. Peter, if you do not mind telling me your
code of converting a float array to a string in VB6, I'd like to write some
examples of the reverse conversion for your reference. You may also want to
read this article:
http://www.codeproject.com/KB/vb/CopyMemory_in_Net.aspx
It demonstrates the equivalent in .NET of various type conversion in VB6.
For example, array <-> string, array <-> array, string <->string.

Is my suggestion above helpful to you? If you have any other questions or
concerns, please DON'T hesitate to tell me.

Regards,
Jialiang Ge ([email protected], remove 'online.')
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:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

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://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter

Thank you very much
This link did the trick
http://www.codeproject.com/KB/vb/CopyMemory_in_Net.aspx


Here's the VB6 code:

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As
Any, Source As Any, ByVal bytes As Long)

Public Function SngToStr(sngArray() As Single) As String

Dim lngLen As Long

lngLen = (UBound(sngArray) + 1) * 4

SngToStr = Space$(lngLen)
CopyMemory ByVal SngToStr, sngArray(0), lngLen

End Function

"Jialiang Ge [MSFT]" said:
Good morning Peter,

First of all, please help to check whether I understand the question
rightly or not:

***** PROBLEM DESCRIPTION *****
We used VB6 to store a float array into an OLE Object field of a Access
DB.
VB6: float array -> string (or a byte array) -> OLE Object field of a
Access DB.
Our need is to retrieve the float array from the OLE Object field using
C#.
C#: OLE Object field of the Access DB -> string (or a byte array) -> float
array.

Am I right?

*** PHASE 1. OLE Object field of the Access DB -> string (or a byte array)
***

In order to get the byte array (or the string) from the DB using C#, we
need to use the System.Data.OleDb namespace. Here is an example:

// the connection string of the access database.
string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=D:\db3.mdb";
OleDbConnection conn = new OleDbConnection(connStr);
conn.Open(); // open the connection

// the sql command
string query = "SELECT * FROM tblTest";
OleDbCommand cmd = new OleDbCommand(query, conn);

// read the data
while (rdr.Read())
{
byte[] arr;
// the ole object field returns as a byte array.
arr = rdr["ObjValue"] as byte[];
// if you need the string value, we can use Encoding.GetString to
parse the byte array to a string.
string val = System.Text.Encoding.Unicode.GetString(arr);
}
// close the reader and the connection.
rdr.Close();
conn.Close();

The first line of the example sets the connection string of the access
database. If you are using Access 2007, please refer to
http://www.connectionstrings.com/?carrier=access2007, otherwise, see
http://www.connectionstrings.com/?carrier=access.

The code line arr = rdr["ObjValue"] as byte[]; and string val =
System.Text.Encoding.Unicode.GetString(arr); retrieves the value of the
ole
object and put it in a byte array or a string object.

*** PHASE 2. string (or a byte array) -> float array ***

This phase of conversion depends on your algorithm of "float array ->
string (or a byte array)" in VB6. Peter, if you do not mind telling me
your
code of converting a float array to a string in VB6, I'd like to write
some
examples of the reverse conversion for your reference. You may also want
to
read this article:
http://www.codeproject.com/KB/vb/CopyMemory_in_Net.aspx
It demonstrates the equivalent in .NET of various type conversion in VB6.
For example, array <-> string, array <-> array, string <->string.

Is my suggestion above helpful to you? If you have any other questions or
concerns, please DON'T hesitate to tell me.

Regards,
Jialiang Ge ([email protected], remove 'online.')
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:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

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://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
J

Jialiang Ge [MSFT]

You are welcome, Peter.
Glad to help.

Regards,
Jialiang Ge
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:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 

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