Skip to content

Instantly share code, notes, and snippets.

@shadowmint
Last active December 4, 2017 13:01
Show Gist options
  • Save shadowmint/1f73f409f15ab4dccdac74297b1876bd to your computer and use it in GitHub Desktop.
Save shadowmint/1f73f409f15ab4dccdac74297b1876bd to your computer and use it in GitHub Desktop.
Execute sql with .NET core
static void Main(string[] args)
{
var connectionString = @"Server=.\RAMBO;Database=EFTripleJ;Trusted_Connection=True;";
var connection = new SqlConnection(connectionString);
using (IDbConnection dbConnection = connection)
{
string sQuery = "PRINT('hello world');";
var x = dbConnection as SqlConnection;
x.InfoMessage += conn_InfoMessage;
dbConnection.Open();
dbConnection.Execute(sQuery);
}
}
static void conn_InfoMessage(object sender, SqlInfoMessageEventArgs e)
{
foreach (var error in e.Errors)
{
Console.WriteLine("---------------------------------------------------");
Console.WriteLine("Source {0} $ Message{1} $ error{2}", e.Source, e.Message,
error.ToString()
);
}
}
<PackageReference Include="System.Data.Common" Version="4.3.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment