I need to do some sql query against sql-server 2008 R2. The command is:
C:> sqlcmd –S
.\APPSDB
1> use
Example_database
2> go
1> Select SiteName, SiteID, Platform, Description, AdminUser from dbo.ExampleInfo
2> go
I want to develop a console application in C# to run this query. My question is: What is the appropriate way to do it? Should I put the above command into a SQL script and use a process to execute this script, or should I use stand C# SQL API such as SqlConnection
to do it? Something like:
string cmdStr = @"....." //the above command
using (var connection = new SqlConnection(ConnectionString))
{
using (var command = connection.CreateCommand())
{
connection.Open();
command.CommandText = cmdStr;
using (var reader = command.ExecuteReader())
{
do
{
while (reader.Read())
{
Console.WriteLine(reader["SiteName"].ToString());
Console.WriteLine(reader["SiteName"].ToString());
// the rest
}
Console.WriteLine("--next command--");
} while (reader.NextResult());
}
}