Loading SQL Server data into DataTable

DataTable represents one table of in-memory data.

SqlConnection Connection = null;
String sql_server_ip_or_hostname "..";
String database_name = "..";
String username = "..";
String password = "..";
 
try
{
    Connection = new SqlConnection(
      "Data Source=" + sql_server_ip_or_hostname + ";" +
      "Initial Catalog=" + database_name + ";" +
      "User Id=" + username + ";" +
      "Password=" + password + ";" +
      "Connect Timeout=30;"));
 
    Connection.Open();
}
catch
{
    Connection = null;
}
 
if(Connection != null) {
    SqlCommand Command = Connection.CreateCommand();
    Command.CommandText = ".... .....";
    DataTable dt = new DataTable("MyDataInfo");
    SqlDataReader Reader = Command.ExecuteReader();
    Command = null;
    dt.Load(Reader);
    Reader.Close();
    Connection.Close();
    Connection = null;
}
Categories: C# Tags: , , , , , ,
%d bloggers like this: