sqlconnection

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

主宰稳场 提交于 2019-11-27 04:22:01
问题 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

Do I have to Close() a SQLConnection before it gets disposed?

ぃ、小莉子 提交于 2019-11-27 03:54:29
Per my other question here about Disposable objects , should we call Close() before the end of a using block? using (SqlConnection connection = new SqlConnection()) using (SqlCommand command = new SqlCommand()) { command.CommandText = "INSERT INTO YourMom (Amount) VALUES (1)"; command.CommandType = System.Data.CommandType.Text; connection.Open(); command.ExecuteNonQuery(); // Is this call necessary? connection.Close(); } CMS Since you have a using block, the Dispose method of the SQLCommand will be called and it will close the connection: // System.Data.SqlClient.SqlConnection.Dispose

C# SQLConnection pooling

社会主义新天地 提交于 2019-11-27 03:51:43
问题 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.. 回答1: 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

SqlCommand with using statement

ε祈祈猫儿з 提交于 2019-11-27 02:08:06
I saw that in most samples SqlCommand was used like this using (SqlConnection con = new SqlConnection(CNN_STRING)) { using (SqlCommand cmd = new SqlCommand("Select ID,Name From Person", con)) { SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); return ds; } } I know why we are using "using" statement. But SqlCommand doesn't inlcude Close() method, so should we really use it within using statement Because it also implements IDisposable . The purpose of Using statement is that when control will reach end of using it will dispose that object of using block and

Does DataAdapter.Fill() close its connection when an Exception is thrown?

坚强是说给别人听的谎言 提交于 2019-11-27 01:56:28
问题 I am using ADO.NET (.NET 1.1) in a legacy app. I know that DataAdapter.Fill() opens and closes connections if the connection hasn't been opened manually before it's given to the DataAdapter. My question: Does it also close the connection if the .Fill() causes an Exception? (due to SQL Server cannot be reached, or whatever). Does it leak a connection or does it have a built-in Finally-clause to make sure the connection is being closed. Code Example: Dim cmd As New SqlCommand Dim da As New

How to run multiple SQL commands in a single SQL connection?

只愿长相守 提交于 2019-11-27 00:37:54
I am creating a project in which I need to run 2-3 sql commands in a single sql connection. Here is the code I have written: SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\project.mdf;Integrated Security=True"); con.Open(); SqlCommand cmd = new SqlCommand("select * from " + mytags.Text + " ", con); SqlDataReader rd = cmd.ExecuteReader(); if (rd.Read()) { con.Close(); con.Open(); SqlCommand cmd1 = new SqlCommand("insert into " + mytags.Text + " values ('fname.lname@gmail.com','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text

What is the difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout?

一世执手 提交于 2019-11-26 22:05:23
Is there any difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout in .NET? Yes. CommandTimeout is how long a single command can take to complete. ConnectionTimeout is how long it can take to establish a connection to the server to start with. For instance, you may be executing relatively long-running queries - it's perfectly okay for them to take 10 minutes to complete, but if it took 10 minutes to make the connection to start with, you'd know that something was badly wrong. SqlCommand.CommandTimeout = timeout limit for your SQL query. Means, how much time a (eg:

ASP.NET use SqlConnection connect MySQL

假装没事ソ 提交于 2019-11-26 20:48:11
This is the connection string saved in web.config : <appSettings> <add key="conn" value="Driver={MySQL ODBC 5.1 Driver};server=127.0.0.1;uid=root;pwd=1234;database=gis_server;option=3"/> </appSettings> This is the code to connect to the database: protected bool CheckPasswordBySqlServer(string strEmail, string strPsw) { if (strEmail.ToLower() == "admin") { return false; } string str = "select id,Rank,RankEnc,ParentUser,Company from tbl_User where userName=@UserName and password1=@password"; private string strConn = ConfigurationManager.AppSettings["conn"].ToString(); SqlConnection sqlConnection

Is SqlCommand.Dispose() required if associated SqlConnection will be disposed?

泪湿孤枕 提交于 2019-11-26 20:46:32
I usually use code like this: using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString)) { var command = connection.CreateCommand(); command.CommandText = "..."; connection.Open(); command.ExecuteNonQuery(); } Will my command automatically disposed? Or not and I have to wrap it into using block? Is it required to dispose SqlCommand ? Just do this: using(var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString)) using(var command = connection.CreateCommand()) { command.CommandText = "..."; connection

Is it best to pass an open SqlConnection as a parameter, or call a new one in each method?

痞子三分冷 提交于 2019-11-26 16:26:05
问题 If methods/functions I'm going to call involve the need of an open SqlConnection, I will open this up in the method which is calling the function. For example: protected static void btnSubmit(){ conn.Open(); myMethod(someParam, conn); conn.Close(); } protected static void myMethod(object someParam, SqlConnection conn){ //Some SQL commands etc here.. } I do this so that I: Only ever open and close 1 SqlConnection per process However, would it be better to structure my code like so: protected