Mega Code Archive

 
Categories / C# / ADO Database
 

Create SqlCommand object

using System; using System.Data; using System.Data.SqlClient;    class CommandExampleSql    {       static void Main()        {          SqlConnection thisConnection = new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI");          SqlCommand thisCommand = new SqlCommand();          Console.WriteLine("Command created.");          try {             thisConnection.Open();             thisCommand.Connection = thisConnection;               thisCommand.CommandText = "SELECT COUNT(*) FROM Employee";             Console.WriteLine("Ready to execute SQL command: {0}", thisCommand.CommandText);          } catch (SqlException ex) {             Console.WriteLine(ex.ToString());          } finally {             thisConnection.Close();             Console.WriteLine("Connection Closed.");          }       }    }