sqlcommand

How do I translate a List<string> into a SqlParameter for a Sql In statement? [duplicate]

廉价感情. 提交于 2019-11-27 14:21:27
This question already has an answer here: Pass Array Parameter in SqlCommand 11 answers I seem to be confused on how to perform an In statement with a SqlParameter . So far I have the following code: cmd.CommandText = "Select dscr from system_settings where setting in @settings"; cmd.Connection = conn; cmd.Parameters.Add(new SqlParameter("@settings", settingList)); reader = cmd.ExecuteReader(); settingsList is a List<string> . When cmd.ExecuteReader() is called, I get an ArgumentException due to not being able to map a List<string> to "a known provider type". How do I (safely) perform an In

C# SQL insert command

为君一笑 提交于 2019-11-27 08:32:23
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(); First of all: STOP concatenating together your SQL code!! This is an invitation to hackers everywhere to attack you with SQL

From .NET can I get the full SQL string generated by a SqlCommand object (with SQL Parameters)?

巧了我就是萌 提交于 2019-11-27 07:56:19
问题 From the .NET environment can I get access to the full SQL string that is generated by a SqlCommand object? Note: The full SQL string shows up in Intellisense hover, in VisualStudio, while in debug mode. I'm willing to use reflection techniques if I must. I'm sure somebody here knows a way to get at it. Update 1 : I'm calling a stored procedure having parameters with cmd.CommandType = CommandType.StoredProcedure and am trying to acquire the full SQL generated and run. I wonder if the cmd

SqlCommand() ExecuteNonQuery() truncates command text

北慕城南 提交于 2019-11-27 07:35:48
问题 I'm building a custom db deployment utility, I need to read text files containing sql scripts and execute them against the database. Pretty easy stuff, so far so good. However I've encountered a snag, the contents of the file are read successfully and entirely, but once passed into the SqlCommand and then executed with SqlCommand.ExecuteNonQuery only part of the script is executed. I fired up Profiler and confirmed that my code is not passing all of the script. private void ExecuteScript

Is it possible to get the parsed text of a SqlCommand with SqlParameters?

狂风中的少年 提交于 2019-11-27 06:38:50
问题 What I am trying to do is create some arbitrary sql command with parameters, set the values and types of the parameters, and then return the parsed sql command - with parameters included. I will not be directly running this command against a sql database, so no connection should be necessary. So if I ran the example program below, I would hope to see the following text (or something similar): WITH SomeTable (SomeColumn) AS ( SELECT N':)' UNION ALL SELECT N'>:o' UNION ALL SELECT N'^_^' )

What does SqlCommand.Prepare() do and when should it be used? [duplicate]

ぐ巨炮叔叔 提交于 2019-11-27 04:50:15
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Pros and Cons of using SqlCommand Prepare in C#? This is what MSDN says about SqlCommand.Prepare() : Creates a prepared version of the command on an instance of SQL Server. Can anybody provide more insight as to what that means and when it should be used? 回答1: The Prepare method is actually on DbCommand, which all classes which derive from it will pick up. What it does is specific to the database provider that

Check if a record exists in the database

怎甘沉沦 提交于 2019-11-27 04:40:11
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 Soner Gönül ExecuteScalar returns the first column of the first row. Other columns or rows are ignored. It looks like your first column of the first row is null , and that's why you get

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

♀尐吖头ヾ 提交于 2019-11-27 04:05:57
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 connectionString = "server=***;database=***;user id=***;pwd=***"; using (SqlConnection connection = new

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

Data Adapter Vs Sql Command

馋奶兔 提交于 2019-11-27 03:34:26
问题 Which one would be better in executing an insert statement for ms-sql database: Sql DataAdapter or SQL Command Object? Which of them would be better, while inserting only one row and while inserting multiple rows ? A simple example of code usage: SQL Command string query = "insert into Table1(col1,col2,col3) values (@value1,@value2,@value3)"; int i; SqlCommand cmd = new SqlCommand(query, connection); // add parameters... cmd.Parameters.Add("@value1",SqlDbType.VarChar).Value=txtBox1.Text; cmd