sqlcommand

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

妖精的绣舞 提交于 2019-11-26 07:43:20
问题 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 ? 回答1: Just do this: using(var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConn

Does SqlCommand.Dispose close the connection?

天大地大妈咪最大 提交于 2019-11-26 07:28:57
问题 Can I use this approach efficiently? using(SqlCommand cmd = new SqlCommand(\"GetSomething\", new SqlConnection(Config.ConnectionString)) { cmd.Connection.Open(); // set up parameters and CommandType to StoredProcedure etc. etc. cmd.ExecuteNonQuery(); } My concern is : Will the Dispose method of the SqlCommand (which is called when exiting the using block) close the underlying SqlConnection object or not? 回答1: No, Disposing of the SqlCommand will not effect the Connection. A better approach

SqlCommand Parameters Add vs. AddWithValue [duplicate]

元气小坏坏 提交于 2019-11-26 04:50:09
问题 This question already has answers here : Difference with Parameters.Add and Parameters.AddWithValue (4 answers) Closed 5 years ago . When should I use Parameters. Add/AddWithValue ? In the following MSDN example they use Parameters.Add for int and Parameters.AddWithValue for string command.Parameters.Add(\"@ID\", SqlDbType.Int); command.Parameters[\"@ID\"].Value = customerID; command.Parameters.AddWithValue(\"@demographics\", demoXml); What is the best to use for datetime 回答1: Use Add if you

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

孤街醉人 提交于 2019-11-26 04:45:00
问题 What\'s the best method to pass parameters to SQLCommand? You can do: cmd.Parameters.Add(\"@Name\", SqlDbType.VarChar, 20).Value = \"Bob\"; or cmd.Parameters.Add(\"@Name\", SqlDbType.VarChar).Value = \"Bob\"; or cmd.Parameters.Add(\"@Name\").Value = \"Bob\"; It seems like the first one might be somehow \"better\" either performance-wise or error checking-wise. But I would like to know more definitively. 回答1: You can also use AddWithValue() , but be aware of the possibility of the wrong

When should “SqlDbType” and “size” be used when adding SqlCommand Parameters?

不想你离开。 提交于 2019-11-26 03:26:58
问题 There is a related question to this: What's the best method to pass parameters to SQLCommand? But I am wanting to know what the differences are and if there are any problems with the different ways. I usually use a structure something like this: using (SqlConnection conn = new SqlConnection(connectionString)) using (SqlCommand cmd = new SqlCommand(SQL, conn)) { cmd.CommandType = CommandType.Text; cmd.CommandTimeout = Settings.Default.reportTimeout; cmd.Parameters.Add(\"type\", SqlDbType

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

删除回忆录丶 提交于 2019-11-26 02:59:32
问题 What does it mean for an SqlConnection to be \"enlisted\" in a transaction? Does it simply mean that commands I execute on the connection will participate in the transaction? If so, under what circumstances is an SqlConnection automatically enlisted in an ambient TransactionScope Transaction? See questions in code comments. My guess to each question\'s answer follows each question in parenthesis. Scenario 1: Opening connections INSIDE a transaction scope using (TransactionScope scope = new

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

 ̄綄美尐妖づ 提交于 2019-11-26 02:59:28
问题 This question already has an answer here: Downloading MySQL dump from command line 10 answers How do I import an SQL file using the command line in MySQL? 47 answers Not Duplicate! looking for some feature have phpmyadmin during export in command line I want to export and import a .sql file to and from a MySQL database from command line . Is there any command to export .sql file in MySQL? Then how do I import it? When doing the export/import, there may be constraints like enable/disable

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

纵饮孤独 提交于 2019-11-25 23:29:16
问题 I have many users on my web site (20000-60000 per day), which is a download site for mobile files. I have remote access to my server (windows server 2008-R2). I\'ve received \"Server is unavailable\" errors before, but am now seeing a connection timeout error. I\'m not familiar with this - why does it occur and how can I fix it? The full error is below: Server Error in \'/\' Application. Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not

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

≡放荡痞女 提交于 2019-11-25 21:59:03
问题 I have an MS SQL Server 2005 database. In a few procedures I have table parameters that I pass to a stored proc as an nvarchar (separated by commas) and internally divide into single values. I add it to the SQL command parameters list like this: cmd.Parameters.Add(\"@Logins\", SqlDbType.NVarchar).Value = \"jim18,jenny1975,cosmo\"; I have to migrate the database to SQL Server 2008. I know that there are table value parameters, and I know how to use them in stored procedures. But I don\'t know