Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

Strongly typed DataTables

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Code Monkey - 21 Aug 2007 19:45 GMT
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?
Nicholas Paldino [.NET/C# MVP] - 21 Aug 2007 20:32 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Silly question maybe, but I've been doing the following for far too
> long now:
[quoted text clipped - 23 lines]
> making the sql and getting the xsd, how would I go about translating
> the above code into a strongly typed object?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.