Strongly typed DataTables

C

Code Monkey

Silly question maybe, but I've been doing the following for far too
long now:

public static DataTable myDataTable()
{
string sql = @"SELECT column1, column2, column3, column4
FROM myTable";
using (SqlConnection conn = new SqlConnection(websqlconn))
{
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}

The above code would normally be using stored procedures, but for a
little bit of added clarity, I've included a simple query.

Now, the silly bit.

Without going through all the code bloat of Visual Studio IDE and
making the sql and getting the xsd, how would I go about translating
the above code into a strongly typed object?
 
N

Nicholas Paldino [.NET/C# MVP]

Do you want a strongly typed object, or do you want a strongly typed
data set? If you want a strongly typed object, then you will have to fill a
dataset, or get a reader (by executing the command) yourself and then map
the values from the result set to the custom object.

If you want a strongly typed data set, then you have to create a new
instance of your strongly typed data set and then pass that to the Fill
method on the SqlDataAdapter.

You should be able to create a strongly typed data set with the
designer, and then delete the adapters, leaving only the strongly typed data
set structure, and then use that.
 

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