-2

Does anybody know if .Net Framework include interface for configuring connection to SQL Server? For example in Java/Oracle we have Universal Connection Pool (UCP) and oracle.ucp.ConnectionLabelingCallback it can helps to configure your database connection. And now I try to find the same feature in .Net/SqlServer. Firstable I would like to handle each connection to database and make some things before my application will execute some statemens (sql queries). For example: I'd like to audit user activities. Any user of my application try to add data in the database and I need to log this activity. Before executing insert query I can save transaction_id for this user in the database and after insert I can get this user by transaction_id (User it is not database user, but it is a user of my application because my application uses only one account to connect to Sql Server). Thank you for help and I apologize for my English.

Alexandr
  • 127
  • 1
  • 1
  • 13
  • I'm not a Java/Oracle guy so I'm not really sure what UCP all involves, however, .Net will pool connections by default based on the connection string that you provide. See [this](https://msdn.microsoft.com/en-us/library/8xx3tyca(v=vs.110).aspx) for an overview. – Chris Haas Sep 23 '15 at 19:32

1 Answers1

0

Your asking two questions. I'll answer the one in the title. .Net has the SqlConnection class. See https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection(v=vs.110).aspx

Here is how it is typically used. The using ensures its disposed of properly.

using (SqlConnection connection = new SqlConnection(connectionString))
{

    connection.Open();
    // Do work here; connection closed on following line.
}

You will also want to know how the connection string is formatted. See:

How to set SQL Server connection string?

https://www.connectionstrings.com/sql-server-2012/

In addition to the SqlConnection, there are more ways to control how a query is run using the SqlCommand. See https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand(v=vs.110).aspx

Here is a simple example:

     SqlConnection conn = new SqlConnection(connectionString);

     using (SqlCommand cmd = new SqlCommand(commandText, conn)) {
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddRange(parameters);

        conn.Open();
        SqlDataReader reader = cmd.ExecuteReader();


     }

You can google ADO.NET examples for more. Like http://www.csharp-station.com/Tutorial/AdoDotNet/Lesson02

Community
  • 1
  • 1
sean
  • 1,187
  • 17
  • 25
  • Thank you for this, but how to use `SqlConnection` is not a problem. My problem is catching event when `SqlConnection` opened. For example: I'd like to audit user activities. Any user of my application try to add data in the database and I need to log this activity. I have already started to learn `Audit Login Event Class` (https://msdn.microsoft.com/en-us/library/ms190260.aspx) may be it helps – Alexandr Sep 24 '15 at 04:59