C#: SQL ExecuteNonQuery
Author: jason
Date: 2019-01-20 14:01:25
Category: Technical

Building upon the previous SQL Connect with Using, here is executing a non-query, i.e. a tsql statement where we are not expecting to get multiple values back (INSERT, UPDATE, DELETE).

Code


public static void ConnectDatabaseNonQuery()
{
try
{
//using System.Data.SqlClient
string connectionString =
@"Data Source=.SQL2017;Initial Catalog=master;Integrated Security=True;Connection TimeOut=30";

using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand("DROP TABLE IF EXISTS #Temp", con);
command.CommandTimeout = 30;
command.Connection.Open();
int i = command.ExecuteNonQuery();
Console.Write("{0}", i);
}
}
catch (Exception e)
{
Console.Write(e);
}
}







jason @ jasonthomasfrance.com - www.masterstationlog.com - copyright 2009