Last active
December 4, 2017 13:01
-
-
Save shadowmint/1f73f409f15ab4dccdac74297b1876bd to your computer and use it in GitHub Desktop.
Execute sql with .NET core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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