sqlconnection

When should I open and close a connection to SQL Server

女生的网名这么多〃 提交于 2019-11-28 16:21:54
I have a simple static class with a few methods in it. Each of those methods open a SqlConnection, query the database and close the connection. This way, I am sure that I always close the connection to the database, but on the other hand, I don't like to always open and close connection. Below is an example of what my methods look like. public static void AddSomething(string something) { using (SqlConnection connection = new SqlConnection("...")) { connection.Open(); // ... connection.Close(); } } Considering that the methods are inside a static class, should I have a static member containing

How to get last executed SQL query by SqlConnection?

主宰稳场 提交于 2019-11-28 11:44:06
Actually, my scenario is bit different than mentioned here. I have asked other question. But as I am not getting solution there, I decided to change the approach. I have a SqlConnection object accessible to my code. All other ADO.NET objects like SqlCommand , SqlParameter etc are not accessible to me. These other objects are consumed by Dapper Extensions ORM. My application executes SQL queries using SqlConnection object and Dapper Extensions method. SQL query is auto generated by Dapper Extensions; generated query is not accessible to me. I want to log this SQL query. I already have my

C# SQLConnection pooling

烂漫一生 提交于 2019-11-28 10:53:20
Can anyone brief me how to do Connection Pooling in ADO.Net, I do need to connect to 3 separate databases. 2 of them are in same server and the other in a separate one. Better with code snipts.. Marc Gravell as long as you are strict about disposing your connections, the default (for sql-server at least) is that it will just work automatically. In your example you could well only have 3 underlying connections (one per connection string). But always ensure your connections are disposed, ideally with using : using(var conn = new SqlConnection(connectionString)) { // use conn } then it is

Windows authentication trusted connection not working

邮差的信 提交于 2019-11-28 09:54:45
问题 MSSQL Server is in the "abc" domain and have mixed mode authentication. I am connecting from the machine which is not in domain or in a domain "xyz" but with in the same network using MSSQL Jdbc driver 2.0. I have logged in as admin or account in xyz domain. It works fine using following url for connection for "sa" or SQL Mode Authentication. jdbc:sqlserver://%DB_IP%:%DB_PORT%;SelectMethod=cursor;DatabaseName=dbname It doesn't work For window authentication using credential "MSSQLDomain

SQL Server returns error “Login failed for user 'NT AUTHORITY\\ANONYMOUS LOGON'.” in Windows application

会有一股神秘感。 提交于 2019-11-28 05:37:21
An application that has been working without problem (and has not had any active development done on it in about 6 months or so) recently began failing to connect to database. Operations admins cant say what might have changed that would cause the problem. The client application uses a hardcoded connection string with Integrated Security=True, but when the applications attempts to create a connection to the database, it throws an SQLException saying "Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON". I can log on to the database through Management Studio on this account without problem. All

Use SqlConnection.GetSchema to get Tables Only (No Views)

a 夏天 提交于 2019-11-28 04:50:43
问题 When I use SqlConnection.GetSchema("Tables"); it returns all the tables AND views for the target database. Is there any way to just return tables? All the research I have done indicates I am doing this correctly yet it always returns the views like they are tables. I have dug down into the DataTable in debug and I can't even find a difference. The data types are reported the same... As far as I can tell, it can't differentiate between a view and a table. (It does make sense in a way since a

Check if SQL Connection is Open or Closed

喜欢而已 提交于 2019-11-28 04:48:06
How do you check if it is open or closed I was using if (SQLOperator.SQLCONNECTION.State.Equals("Open")) however, even the State is 'Open' it fails on this check. You should be using SqlConnection.State e.g, using System.Data; if (myConnection != null && myConnection.State == ConnectionState.Closed) { // do something // ... } Here is what I'm using: if (mySQLConnection.State != ConnectionState.Open) { mySQLConnection.Close(); mySQLConnection.Open(); } The reason I'm not simply using: if (mySQLConnection.State == ConnectionState.Closed) { mySQLConnection.Open(); } Is because the ConnectionState

How to use ADO.net Entity Framework with an existing SqlConnection?

半城伤御伤魂 提交于 2019-11-28 04:45:18
I have an existing asp.net website that uses an SqlConnection. I have added the ADO.net Entity Framework. I have successfully connected to the database and created the .edmx file. I am able to connect through the Entity Framework with the connectionstring that is automatically generated. I want to use the existing SqlConnection object that I use throughout the site for the Entity Framework connection. I do not want to have to use a second database connection for the one page that is going to use the ADO.net Entity Framework and I don’t want to change the entire site to use the new Entity

Difference between Sql Connection and OLEDB Connection

假装没事ソ 提交于 2019-11-28 03:22:24
问题 What is the difference between the SQL Connection and OLEDB Connection? Is that OLEDB is common to all (also SQL Server)? To which are all Servers, OLEDB is using? 回答1: The advantage of using OleDbConnection is flexibility. You can change your database (for instance, move to Oracle)and not have to change your code. If you using SQLServer as backend then use SQLConnection for better performance. check with this link http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/fadb2742-d75a

Arithmetic overflow exception when opening SQL connection

Deadly 提交于 2019-11-28 02:48:10
问题 I got very weird ArithmeticOverflowException when opening an SQL connection to the underlying SQL database (stack trace included below). It doesn't make a difference which version of the server is used (I've verified MS SQL 2005/2008/2012/2014), error is still the same. All the newest updates and patches from Windows Update installed. OS is Windows 8.1 / 10 (same occurs on both systems). Server is installed locally and the connection is made via user and password. Connection timeout verified