Reputation: 925
I just got a new PC and am trying to remember how to set everything back up as needed, but am epic failing. I want to run this simple piece of code below, but have been hammered with compile errors. can someone be so kind to point out what I need to do to remove all of these compile errors?
using (SQLConnnection conn = new SQLConnection(connectionStringSQL)
{
conn.Open();
Using (SqlCommand command = new SqlCommand("SELECT * FROM Table1");
conn.Close();
}
LIST of compile errors ----
1) 'SQL.SQLConnection': type used in a using statement must be implicitly convertible to 'System.IDisposable'
2) 'SQL.SQLConnection' does not contain a constructor that takes 1 arguments
3) 'SQL.SQLConnection' does not contain a definition for 'Open' and no extension method 'Open' accepting a first argument of type 'SQL.SQLConnection' could be found (are you missing a using directive or an assembly reference?)
4) The best overloaded method match for 'System.Data.SqlClient.SqlCommand.SqlCommand(string, System.Data.SqlClient.SqlConnection)' has some invalid arguments
5) cannot convert from 'SQL.SQLConnection' to 'System.Data.SqlClient.SqlConnection'
6) The name 'command' does not exist in the current context
7) 'SQL.SQLConnection' does not contain a definition for 'Close' and no extension method 'Close' accepting a first argument of type 'SQL.SQLConnection' could be found (are you missing a using directive or an assembly reference?)
Upvotes: 0
Views: 5827
Reputation: 1099
If you read the 5th error cleary , you would see that. You are using
using SQL.SQLConnection;
But you should use
using System.Data.Sqlclient.Sqlconnection;
Check your namespace header or specified it. I can read this from ther error. Besides, you are missing a bracket. See selman22 answer.
Upvotes: 3
Reputation: 14389
since you are using using
you dont need to close the connection , it will close, moreover, since you dont execute your query:
using (SqlConnection conn = new SqlConnection(connectionStringSQL))
{
conn.Open();
using (SqlCommand command = new SqlCommand("SELECT * FROM Table1"))
{
cmd1 = conn.CreateCommand();
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = command;
cmd1.ExecuteNonQuery();
}
}
though probably you want to execute a DataReader
but it is up to you
Upvotes: 1
Reputation: 101701
using (SqlConnection conn = new SqlConnection(connectionStringSQL))
{
conn.Open();
using (SqlCommand command = new SqlCommand("SELECT * FROM Table1"))
{
}
conn.Close();
}
SQLConnnection
-> SqlConnection
Using
-> using
using
.using
using(...); <----
Upvotes: 1