471,355 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,355 software developers and data experts.

Strongly typed DataTables

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?

Aug 21 '07 #1
1 3239
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Code Monkey" <dl********@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
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?

Aug 21 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mark | last post: by
3 posts views Thread by Duncan Welch | last post: by
2 posts views Thread by Jim Heavey | last post: by
20 posts views Thread by Dennis | last post: by
1 post views Thread by Larry Lard | last post: by
12 posts views Thread by BillE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.