简体   繁体   中英

How to receive a SQL-Statement as a DataTable

I have the following code:

public static DataTable GetDataTable(string sConnStr, string sTable)
{
    DataTable dt = new DataTable();

    SqlConnection sqlConn10 = new SqlConnection(sConnStr);
    sqlConn10.Open();
    SqlCommand sqlComm10 = new SqlCommand("SELECT * FROM " + sTable, sqlConn10);
    SqlDataReader myReader10 = sqlComm10.ExecuteReader();

    try
    {
        while (myReader10.Read())
        {
            // Code needed
        }
    }
    catch
    {
        myReader10.Close();
        sqlConn10.Close();
    }

    return dt;
}

The problem is, I don't know how to go on. All I want is to get a DataTable with the data from the SQL-Statement. Thanks for your help!

You could use a data adapter:

public static DataTable GetDataTable(string sConnStr, string sTable)
{
    using (SqlConnection sqlConn10 = new SqlConnection(sConnStr))
    using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM EmployeeIDs", sqlConn10))
    {
        sqlConn10.Open();
        DataTable dt = new DataTable();
        adapter.Fill(dt);
        return dt;
    }
}

Don't use a SqlDataReader , use a SqlDataAdapter . A DataReader is for fast, forward-only operations, while the DataAdapter is geared towards set-based operations.

See MSDN: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.aspx

See http://www.dotnetperls.com/sqldataadapter for a simple example.

DataAdapter.Fill(DataTable, mySqlQuery);

http://msdn.microsoft.com/en-us/library/497b1k4t.aspx

Simply Do this:

SqlDataReader myReader10 = sqlComm10.ExecuteReader();
dt.Load(myReader10);

and return dt;

So your would be like this:


try
    {
        while (myReader10.Read())
         {
            dt.Load(myReader10);
         }
    }
    catch
    {
        myReader10.Close();
        sqlConn10.Close();
    }
return dt;


public static DataTable GetDataTable(string sConnStr, string sTable)
{
    using (SqlConnection sqlConn10 = new SqlConnection(sConnStr))
    using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM " + sTable, sqlConn10))
    {
        sqlConn10.Open();
        DataTable dt = new DataTable();
        adapter.Fill(dt);
        return dt;
    }
}

// you should use best practices, use the using statement to dispose connection and other objects after completion of work.



public static DataTable GetDataTable(string sConnStr, string sTable)
{    
   DataTable dt = new DataTable();
   try
   {
       using (SqlConnection sqlConn10 = new SqlConnection(sConnStr))
       using (SqlCommand sqlComm10 = new SqlCommand("SELECT * FROM " + sTable, sqlConn10))
       {
           sqlConn10.Open();
           using (SqlDataReader myReader10 = sqlComm10.ExecuteReader())
           {                     
               dt.Load(myReader10);
           }
       }
   }
   catch(Exception ex)
   {
   }
   return dt;
}

Ref: Put SqlDataReader data into a DataTable while i process it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM