sqlcommand

How to set isolation level on SqlCommand/SqlConnection initialized with no transaction

让人想犯罪 __ 提交于 2019-11-26 16:40:25
问题 The following method is supposed to peroform a dirty read on an open connection. There are no transactions. Where do I set IsolationLevel? public string DoDirtyRead(string storedProcName, SqlConnection connection) { using (SqlCommand command = new SqlCommand(storedProcName, connection)) { command.CommandType = CommandType.StoredProcedure; // HOW TO SET IsolationLevel to READ_UNCOMMITTED here? command.ExecuteNonQuery(); } } 回答1: On the BeginTransaction method: (MSDN link) And if you just want

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

有些话、适合烂在心里 提交于 2019-11-26 16:14:42
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. You can also use AddWithValue() , but be aware of the possibility of the wrong implicit type conversion. cmd.Parameters.AddWithValue("@Name", "Bob"); What's going on in there? You quote the parameter lists

C# SQL insert command

a 夏天 提交于 2019-11-26 14:09:39
问题 Can anyone tell me the following 2 ways of inserting record creates better performance? Case 1 SqlCommand cmd = new SqlCommand(); for (int i = 0; i < 10000; i++) { cmd = new SqlCommand("insert into test(id, name) value('" + i + "', '" + i + "')"); cmd.ExecuteNonQuery(); } Case 2 string sql = null; for (int i = 0; i < 10000; i++) { sql += "insert into test(id, name) value('" + i + "', '" + i + "')"; } SqlCommand cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); 回答1: First of all: STOP

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

前提是你 提交于 2019-11-26 12:40:54
问题 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(); } 回答1: Since you have a using block, the Dispose method of the SQLCommand

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

僤鯓⒐⒋嵵緔 提交于 2019-11-26 12:33:54
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 TransactionScope()) using (SqlConnection conn = ConnectToDB()) { // Q1: Is connection automatically enlisted

Check if a record exists in the database

时光怂恿深爱的人放手 提交于 2019-11-26 11:18:07
问题 I am using these lines of code to check if the record exists or not. SqlCommand check_User_Name = new SqlCommand(\"SELECT * FROM Table WHERE ([user] = \'\" + txtBox_UserName.Text + \"\') \", conn); int UserExist = (int)check_User_Name.ExecuteScalar(); But I am getting an error: Object reference not set to an instance of an object. I want to do: if (UserExist > 0) // Update record else // Insert record 回答1: ExecuteScalar returns the first column of the first row. Other columns or rows are

Why is some sql query much slower when used with SqlCommand?

谁说胖子不能爱 提交于 2019-11-26 11:04:29
问题 I have a stored procedure that executes much faster from Sql Server Management Studio (2 seconds) than when run with System.Data.SqlClient.SqlCommand (times out after 2 minutes). What could be the reason for this? Details: In Sql Server Management Studio this runs in 2 seconds (on production database): EXEC sp_Stat @DepartmentID = NULL In .NET/C# the following times out after 2 minutes (on production database): string selectCommand = @\" EXEC sp_Stat @DepartmentID = NULL\"; string

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

纵然是瞬间 提交于 2019-11-26 09:28:02
问题 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 \" +

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

ぃ、小莉子 提交于 2019-11-26 08:48:19
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.VarChar, 4).Value = type; cmd.Connection.Open(); using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) {

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

巧了我就是萌 提交于 2019-11-26 08:09:11
问题 Is there any difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout in .NET? 回答1: 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