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

What’s the best method to pass parameters to SQLCommand?

What’s going on in there? You quote the parameter lists for several overloads of Add. These are convenience methods that correspond directly to constructor overloads for the SqlParameter class. They essentially construct the parameter object using whatever constructor has the same signature as the convenience method you called, and then call SqlParameterCollection.Add(SqlParameter) like this: SqlParameter … Read more

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

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 … Read more

Is it safe to not parameterize an SQL query when the parameter is not a string?

I think it’s safe… technically, but it’s a terrible habit to get into. Do you really want to be writing queries like this? var sqlCommand = new SqlCommand(“SELECT * FROM People WHERE IsAlive = ” + isAlive + ” AND FirstName = @firstName”); sqlCommand.Parameters.AddWithValue(“firstName”, “Rob”); It also leaves you vulnerable in the situation where a … Read more

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

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 disassemble protected override void Dispose(bool disposing) { if (disposing) { this._userConnectionOptions = null; this._poolGroup = null; this.Close(); } this.DisposeMe(disposing); base.Dispose(disposing); }

How to pass table value parameters to stored procedure from .net code

DataTable, DbDataReader, or IEnumerable<SqlDataRecord> objects can be used to populate a table-valued parameter per the MSDN article Table-Valued Parameters in SQL Server 2008 (ADO.NET). The following example illustrates using either a DataTable or an IEnumerable<SqlDataRecord>: SQL Code: CREATE TABLE dbo.PageView ( PageViewID BIGINT NOT NULL CONSTRAINT pkPageView PRIMARY KEY CLUSTERED, PageViewCount BIGINT NOT NULL ); … Read more

Under what circumstances is an SqlConnection automatically enlisted in an ambient TransactionScope Transaction?

I’ve done some tests since asking this question and found most if not all answers on my own, since no one else replied. Please let me know if I’ve missed anything. Q1: Is connection automatically enlisted in transaction? Yes, unless enlist=false is specified in the connection string. The connection pool finds a usable connection. A … Read more

How to export and import a .sql file from command line with options? [duplicate]

Type the following command to import sql data file: $ mysql -u username -p -h localhost DATA-BASE-NAME < data.sql In this example, import ‘data.sql’ file into ‘blog’ database using vivek as username: $ mysql -u vivek -p -h localhost blog < data.sql If you have a dedicated database server, replace localhost hostname with with actual … Read more

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated

Looks like you have a query that is taking longer than it should. From your stack trace and your code you should be able to determine exactly what query that is. This type of timeout can have three causes; There’s a deadlock somewhere The database’s statistics and/or query plan cache are incorrect The query is … Read more

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