Questions tagged [sqlcommand]

Represents a Transact-SQL statement or stored procedure to execute against a SQL Server database which is used in Microsoft .NET.

SqlCommand is a class in the .NET Framework whose instances represent a SQL statement to later be executed against a SQL Server database.

General syntax:

SqlCommand is instantiated with a query string and a connection. In C#:

SqlCommand cmd = new SqlCommand("select CategoryName from Categories", con);

Reference

SqlCommand on MSDN

919 questions
34
votes
1 answer

SqlCommand Parameters Add vs. AddWithValue

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 =…
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
32
votes
6 answers

How do I translate a List into a SqlParameter for a Sql In statement?

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…
KallDrexx
  • 27,229
  • 33
  • 143
  • 254
32
votes
6 answers

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

I usually use code like this: using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString)) { var command = connection.CreateCommand(); command.CommandText = "..."; connection.Open(); …
abatishchev
  • 98,240
  • 88
  • 296
  • 433
32
votes
3 answers

Save byte array in sql server

Am looking to use an approach in saving passwords that requires using byte array as in this post So which data type should i use in sql server to save byte array? and how can i pass and retrieve the byte array using SqlCommand?
Akkad
  • 609
  • 1
  • 7
  • 14
29
votes
5 answers

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

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 =…
kateroh
  • 4,382
  • 6
  • 43
  • 62
26
votes
7 answers

Insert into C# with SQLCommand

What's the best way to INSERT data into a database? This is what I have but it's wrong.. cmd.CommandText = "INSERT INTO klant(klant_id,naam,voornaam) VALUES(@param1,@param2,@param3)"; cmd.Parameters.Add(new SqlParameter("@param1",…
KevinDW
  • 305
  • 1
  • 4
  • 9
25
votes
8 answers

How can I get an error message that happens when using ExecuteNonQuery()?

I am executing a command in this way : var Command = new SqlCommand(cmdText, Connection, tr); Command.ExecuteNonQuery(); In the command there is an error, however .NET does not throw any error message. How could I know that the command did not…
Haider Ali Wajihi
  • 2,756
  • 7
  • 51
  • 82
25
votes
5 answers

SqlCommand object, what length of time for CommandTimeout?

How do I decide what length of time to use as a timeout when using an SqlCommand object? On parts of the code I'm working on (written by somebody else) I have: cmd.CommandTimeout = 60; Which I think is quite short. However, I have seen some people…
bobble14988
  • 1,749
  • 5
  • 26
  • 38
23
votes
6 answers

MySqlCommand Command.Parameters.Add is obsolete

I'm making an C# windows Form Application in visual studio 2010. That application is connecting to an mysql database, and I want to insert data in it. Now do I have this part of code: MySqlConnection connection; string cs = @"server=server…
Mathlight
  • 6,436
  • 17
  • 62
  • 107
21
votes
6 answers

expects parameter '@ID', which was not supplied?

I am sending ID as outparameter but its giving error System.Data.SqlClient.SqlException: Procedure or function 'usp_ClientHistoryItem' expects parameter '@ID', which was not supplied. Code using (SqlCommand cmd = new…
Azhar
  • 20,500
  • 38
  • 146
  • 211
21
votes
3 answers

Why the "Non" in "ExecuteNonQuery"?

I know this is not a hell of an useful question but I can't help being bugged by it. So, Why said method (in *Command classes) is called ExecuteNonQuery instead of ExecuteQuery? Aren't those SQL statements we throw at DBs, queries?
Camilo Martin
  • 37,236
  • 20
  • 111
  • 154
20
votes
5 answers

Check if record in a table exist in a database through ExecuteNonQuery

in my program i need to check if a record in the database already exists in the table using the if statement. using c# i am trying to do this through an sql connection. as i supposed that the ExecuteNonQuery(); command returns an integer value, if…
Albert A-w
  • 317
  • 1
  • 3
  • 7
19
votes
4 answers

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

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…
Ole Lynge
  • 4,457
  • 8
  • 43
  • 57
18
votes
3 answers

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

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…
M4N
  • 94,805
  • 45
  • 217
  • 260
17
votes
13 answers

Check if a record exists in the database

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 =…
Kamran
  • 4,010
  • 14
  • 60
  • 112
1
2
3
61 62