By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,663 Members | 2,157 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,663 IT Pros & Developers. It's quick & easy.

Populating disconnected ADODB.Recordset with System.Data.DataTable data.

P: n/a
I need to support a VB6 application that will be receiving
disconnected ADODB.Recordset from out DotNet solution. Our dotnet
solution deals with System.Data.DataTable. I need to populate a
disconnected ADODB.Recordset from System.Data.DataTable data. Below
is the source code I am implementing to test the process. I do not
get any error, that I can see. The problem I have is that at the end,
the recordset seems to be empty. Any suggestions are very much
appreciated. At this moment I do not know what to do.
Thank you so much for the time and effort you may spend on this
matter.
Elcc.
private bool testConvertDT_RS()
{
System.Data.DataTable dt;
ADODB.Recordset rs;

// create dummy data table.
dt = PopulateDataTable( );

// attempt to populate disconnected ADODB.Recordset
with System.Data.DataTable data
rs = ConvertToRS ( dt );

if( null == rs )
{
throw new Exception(" Recordset is null.");
}

if( 0 == rs.Fields.Count )
{
throw new Exception(" Recordset Field count is zero.");
}

return true;
}
private System.Data.DataTable PopulateDataTable()
{

System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
string[,] ColValues = {{"R1-C1", "R2-C1", "R3-C1", "R4-C1", "R5-C1"}
, {"R1-C2", "R2-C2", "R3-C2", "R4-C2", "R5-C2"} ,
{"R1-C3", "R2-C3", "R3-C3", "R4-C3", "R5-C3"}};

dt.Columns.Add(new System.Data.DataColumn("Column01",
System.Type.GetType("System.String")));
dt.Columns.Add(new System.Data.DataColumn("Column02",
System.Type.GetType("System.String")));
dt.Columns.Add(new System.Data.DataColumn("Column03",
System.Type.GetType("System.String")));
for( int i = 0 ; i < 5 ; i++ )
{
dr = dt.NewRow();
for( int n = 0 ; n < 3 ; n++ )
{
dr[n] = ColValues[ n, i];
}
dt.Rows.Add(dr);
}
return dt;
}
private ADODB.Recordset ConvertToRS(System.Data.DataTable dt)
{
ADODB.Recordset rs = new ADODB.Recordset();

try
{

rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient;
rs.ActiveConnection = null;

for( int i = 0; i < dt.Columns.Count; i++)
{
rs.Fields.Append( dt.Columns[i].ColumnName,
ADODB.DataTypeEnum.adBSTR,
dt.Columns[i].MaxLength,
ADODB.FieldAttributeEnum.adFldUnspecified,
null );
}
rs.Open( Type.Missing,
Type.Missing,
ADODB.CursorTypeEnum.adOpenStatic,
ADODB.LockTypeEnum.adLockOptimistic,
-1);
foreach(System.Data.DataRow dr in dt.Rows)
{
rs.AddNew(Type.Missing, Type.Missing);

for( int n = 0; n < dt.Columns.Count; n++)
{
rs.Fields[n].Value = dr[n];
}

rs.Update(Type.Missing, Type.Missing);

}

rs.Close();
return rs;

}
catch(Exception e)
{
throw e;
}

}
Jul 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.