0

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();
1
  • 5
    You need a using directive for its namespace. Commented Oct 26, 2016 at 17:26

2 Answers 2

1

you are missing some namespace try to add

using System.Data;
Sign up to request clarification or add additional context in comments.

Comments

1

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.