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

Just do this: using(var connection = new SqlConnection(ConfigurationManager.ConnectionStrings[“MyConn”].ConnectionString)) using(var command = connection.CreateCommand()) { command.CommandText = “…”; connection.Open(); command.ExecuteNonQuery(); } Not calling dispose on the command won’t do anything too bad. However, calling Dispose on it will suppress the call to the finalizer, making calling dispose a performance enhancement.

When does “SqlConnection does not support parallel transactions” happen?

You’ll get this if the connection already has an uncommitted transaction and you call BeginTransaction again. In this example: class Program { static void Main(string[] args) { using (SqlConnection conn = new SqlConnection(“Server=.;Database=TestDb;Trusted_Connection=True;”)) { conn.Open(); using (var tran = conn.BeginTransaction()) { using (var cmd = new SqlCommand(“INSERT INTO TESTTABLE (test) values (‘” + DateTime.Now.ToString() + … Read more

When should I open and close a connection to SQL Server

No, don’t keep a static SqlConnection unless you have to. Threading would be one concern, but more importantly – usually you simply don’t need to. With your code as presented, the internal connection pooling means that most of the time you will get the same underlying connection on successive calls anyway (as long as you … Read more

Is it better to execute many sql commands with one connection, or reconnect every time?

By default, SqlConnection will use connection pooling. Therefore your code does most likely not actually open many connections in either case. You can control if SqlConnection will use pooling by enabling or disabling the pool in the connectionstring, depending on what DB your connection string is for, the syntax will vary. See here for some … Read more

How to use the ConfigurationManager.AppSettings

Your web.config file should have this structure: <configuration> <connectionStrings> <add name=”MyConnectionString” connectionString=”…” /> </connectionStrings> </configuration> Then, to create a SQL connection using the connection string named MyConnectionString: SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“MyConnectionString”].ConnectionString); If you’d prefer to keep your connection strings in the AppSettings section of your configuration file, it would look like this: <configuration> <appSettings> … Read more

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

Just change the SqlCommand.CommandText instead of creating a new SqlCommand every time. There is no need to close and reopen the connection. // Create the first command and execute var command = new SqlCommand(“<SQL Command>”, myConnection); var reader = command.ExecuteReader(); // Change the SQL Command and execute command.CommandText = “<New SQL Command>”; command.ExecuteNonQuery();

Does SqlCommand.Dispose close the connection?

No, Disposing of the SqlCommand will not effect the Connection. A better approach would be to also wrap the SqlConnection in a using block as well: using (SqlConnection conn = new SqlConnection(connstring)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(cmdstring, conn)) { cmd.ExecuteNonQuery(); } } Otherwise, the Connection is unchanged by the fact that a … Read more

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

If your issue is with linked servers, you need to look at a few things. First, your users need to have delegation enabled and if the only thing that’s changed, it’l likely they do. Otherwise you can uncheck the “Account is sensitive and cannot be delegated” checkbox is the user properties in AD. Second, your … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)