filling a data table form a sqldata reader

G

Guest

Hi,

I am trying to populate a datatable from a sql data reader but I am having
some issues that hopefully somebody can help me out with.

I have added the using System.Data.Common to the top of my form

This is my code
connect = new
System.Data.SqlClient.SqlConnection(connectString);
command = new System.Data.SqlClient.SqlCommand("SELECT *
FROM "+owner+tableName+ " WHERE "+ tableField+ " IN("+inClause+")",connect);
connect.Open();
reader = command.ExecuteReader();

System.Data.Common.DataAdapter da;
da.Fill(dataTbl, reader);

And the error i get is

System.Data.Common.DataAdapter.Fill(System.data.DataTable,System.Data.IDataReader) is inaccessible due to its protection level.

I believe that I need to probably add a reference but I am not sure what to
add.

Any help would be appreciated.

Thanks
 
G

Guest

Your syntax is incorrect - you need to read up on ADO.NET.

If you use a DataReader which is forward only, you generally populate your
own business object (not datasets) as you have to read through the database
sequentially.

You are getting the DataSet and DataReader mixed up. When you use a
DataAdapter, you generally use DataSets. When you use the Fill method, you
fill a DataSet object, not a DataReader.

When you create the DataAdapter you need to assign it a SqlConnection
object, your code isn't doing this.

Another tip, using a DataAdapter you don't need to open and close the
connection, the DataAdapter does this for you automatically. If using a
DataReader - always remember to close it afterwards.

So try changing your code to something like:

connect = new
System.Data.SqlClient.SqlConnection(connectString);


System.Data.Common.DataAdapter da = new
System.Data.Common.DataAdapter("SELECT *
FROM "+owner+tableName+ " WHERE "+ tableField+ " IN("+inClause+")",connect);
DataSet mySet = new DataSet("Myset");

da.Fill(mySet);

I'd recommend using the Data Access Block from the Client Software Factory,
it makes database coding so much easier. I had to scratch my head to answer
this post as I can bearly remember ADO now that I use the data application
block.
 

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