HS Banner
Back
SqlConnection Class in C#

Author: b.henry 03/20/2021
Language: C#
Tags: cs


Description:

Represents a connection to a SQL Server database.

Article:

The following example creates a SqlCommand and a SqlConnection. The SqlConnection is opened and set as the Connection for the SqlCommand. The example then calls ExecuteNonQuery. To accomplish this, the ExecuteNonQuery is passed a connection string and a query string that is a Transact-SQL INSERT statement. The connection is closed automatically when the code exits the using block.

SqlConnection Class (System.Data.SqlClient) | Microsoft Docs

Full Source Code:
private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
}

Back
Comments
Add Comment
There are no comments yet.