Click here to Skip to main content
12,298,463 members (44,136 online)
Click here to Skip to main content

Stats

62.3K views
447 downloads
43 bookmarked
Posted

Tips for LINQ: Fill any generic List from a DataTable

, 3 Mar 2009 CPOL
This article shows how to populate any generic list from a DataTable.
FromDataTableToGenericListExample
bin
Release
FromDataTableToGenericListExample.exe
FromDataTableToGenericListExample.vshost.exe
FromDataTableToGenericListExample.vshost.exe.manifest
Data
Database
FromDataTableToGenericListExample.csproj.user
Process
Properties
Scripts
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace FromDataTableToGenericListExample
{    
    public static class SqlDataBase
    {
        private static SqlConnection Connection;
        private static SqlCommand Command;
        private static SqlDataAdapter DataAdapter;

        public static bool Connect(string servername, string databasename, string user, string password)
        {

            try
            {
                Connection = new SqlConnection();
                string connectionstring = "Server=" + servername + ";Database=" + databasename + ";User ID=" + user + ";Password=" + password + ";Trusted_Connection=False";
                Connection.ConnectionString = connectionstring;
                Connection.Open();

                return true;
            }
            catch (Exception Ex)
            {
                Console.WriteLine(Ex.ToString());
                return false;
            }
        }

        public static void Disconnect()
        {
            if (Connection != null)
            {
                if (Connection.State == ConnectionState.Open)
                {
                    Connection.Close();
                    Connection.Dispose();
                }
            }
        }

        public static DataTable ReturnDataTable(string strSql)
        {
            DataTable dt = new DataTable();

            try
            {
                Command = new SqlCommand(strSql, Connection);
                DataAdapter = new SqlDataAdapter(Command);
                DataAdapter.Fill(dt);
                DataAdapter.Dispose();
                Command.Dispose();

                return dt;
            }

            catch (Exception Ex)
            {
                Console.WriteLine(Ex.ToString());
                return null;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Santiago Sanchez
Software Developer
Spain Spain
I´ve been working with Oracle, Sql Server and Visual Basic 6 since 2003 and with C# since 2006. Now I´m fighting with Biztalk 2006 too...

MCTS - .NET Framework 4, Windows Applications
MCTS - Accessing Data with .NET Framework 4

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 3 Mar 2009
Article Copyright 2009 by Santiago Sanchez
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid