Reputation: 65
I basically copied the example from microsoft.com, but it seems that CommandType
does not exist in the current context. The example does not show declaring it.
Thanks
string SQLstring = @"Server=DELLXPS\SQLEXPRESS;Database=SEIN;Integrated Security=true";
SqlConnection sqlConnection1 = new SqlConnection(SQLstring);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.CommandText = "SELECT * FROM ResidentUsers";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.
sqlConnection1.Close();
Upvotes: 0
Views: 143
Reputation: 903
The using the keyword should be used around the connection, command, and datareader. This will ensure that even in the event of an exception occurring, the resources related to those items are released and connections are closed.
using System.Data.SqlClient;
using System.Data;
string SQLstring = @"Server=DELLXPS\SQLEXPRESS;Database=SEIN;IntegratedSecurity=true";
string commandText = "SELECT * FROM ResidentUsers";
using (var sqlConnection1 = new SqlConnection(SQLstring))
using (var cmd = new SqlCommand(commandText, sqlConnection1) { CommandType = CommandType.Text })
{
sqlConnection1.Open();
using (var reader = cmd.ExecuteReader())
{
// Data is accessible through the DataReader object here.
}
sqlConnection1.Close();
}
Upvotes: 1