I want to know how to execute more than one SQL command at once.
At this moment I’m doing it like this:
using (SqlConnection sqlConnection1 = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT nome FROM teste";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
// execute the command
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
listBox1.Items.Add(rdr["name"].ToString());
}
}
}
But how can I do to execute
use [databaseX]
SELECT nome FROM teste
in my c# program?
Separate multiple statements with a semicolon (
;).(BTW, the
usestatement is generally not needed because it’s set in your connection string.)