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
6
votes
5 answers

C# SQL insert command

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 +…
RoyT
  • 139
  • 1
  • 1
  • 14
6
votes
3 answers

Data Adapter Vs Sql Command

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…
Jayesh
  • 1,511
  • 1
  • 16
  • 37
6
votes
2 answers

SqlCommand.Parameters.AddWithValue issue: Procedure or function X expects parameter @Y, which was not supplied

I have a problem with the folowwing piece of code. I am passing a parameter (List) to a method executing the following code. When it executes SQL Server throws an error saying that the proc expects a parameter that was not provided. I…
Petrus
  • 213
  • 1
  • 3
  • 6
6
votes
4 answers

C# multiple parallel inserts in database

I have a datatable with around 3000 rows. Each of those rows need to be inserted in a database table. Currently, i am running a foreach loop as under: obj_AseCommand.CommandText = sql_proc; obj_AseCommand.CommandType =…
Harsh
  • 107
  • 1
  • 2
  • 5
6
votes
2 answers

C# SqlDataReader Execution Statistics and Information

I am creating an automated DB Query Execution Queue, which essentially means I am creating a Queue of SQL Queries, that are executed one by one. Queries are executed using code similar to the following: using (SqlConnection cn = new…
6
votes
3 answers

What happens to Unicode in a System.Data.SQLCommand

I have a SQLCommand : "Update Customers Set Name = @name where code = @code" and this code: cmd.Parameters[0].Value = "بهروز";//(some Unicode characters) cmd.Parameters[1].Value = 1; cmd.ExecuteNonQuery(); or this code: …
Behrooz
  • 1,696
  • 2
  • 32
  • 54
6
votes
2 answers

Stored Proc and SqlCommand Timeout

If I run a stored proc using the SqlCommand and the SqlCommand times out does the StoredProc continue to execute or does it get force to quit when the SqlCommand disconnects?
RoboDev
  • 4,003
  • 11
  • 42
  • 51
5
votes
5 answers

Parsing a SQL string in c#

I have the need to Parse a Command.CommandText. I don't want to run the query. I only want to see if the query will succeed if the command was executed. Say i have; "SELECT * FROM SomeTable WHERE (1=1)" This string will succeed. but, "SELECT * FROM…
Willem
  • 9,166
  • 17
  • 68
  • 92
5
votes
3 answers

Is SqlConnection / SqlCommand thread safe?

I am currently creating a WCF web service. As part of its job, it will unfortunately need to do some fairly intensive computations, however these computations can fortunately be shared between calls to the webservice. In effect - we only need to do…
Fafnr
  • 247
  • 1
  • 3
  • 18
5
votes
3 answers

What are the limitations of T-SQL that can be executed by a System.Data.SqlClient.SqlCommand object?

I have some Transact-SQL that lloks like this, can it be executed through a SqlCommand object, or do I need to start learning Sql Management Objects? BEGIN TRANSACTION BEGIN TRY IF NOT EXISTS ( SELECT * FROM…
Michiel van Oosterhout
  • 22,839
  • 15
  • 90
  • 132
5
votes
0 answers

C# pass parameters to Postgres Npgsqlcommand DO block

I get the error "42883: operator does not exist: integer =@ integer" Npgsql.PostgresException when trying to pass parameters to a DO block: var cmd = new NpgsqlCommand(); cmd.CommandText = @"DO $$ BEGIN IF EXISTS (SELECT id FROM pub.table1 WHERE…
TheMixy
  • 1,049
  • 15
  • 36
5
votes
1 answer

SqlClient.SqlCommand.ExecuteScalarAsync behaving like synchronous call

I've stripped my application down to a minimal POC and I'm still getting the same effect. It appears that ExecuteScalarAsync is behaving like a synchronous call. I thought that when the await is encountered the remainder of the code in the async…
dtaylor
  • 997
  • 3
  • 10
  • 26
5
votes
1 answer

SqlParameter DbType.Date -> SqlDbType.DateTime Conversion

I'm using the generic IDbCommand functions (in non SqlCommand helper methods) to set SqlCommand.DbType to DbType.Date similar to this: var param = command.CreateParameter(); param.DbType = DbType.Date; param.ParameterName =…
Gene
  • 1,587
  • 4
  • 18
  • 38
5
votes
6 answers

SqlCommand.Dispose() not disposing the SqlParameters in it - Memory Leak - C#.NET

I've a windows forms application with MS SQL Server 2005 as the back end. I have written code in the form to call few stored procedures using SqlConnection, SqlCommand objects and i properly dispose everything. I've disposed sqlcommand object by…
NLV
  • 21,141
  • 40
  • 118
  • 183
5
votes
3 answers

C# .Net SqlConnection to LocalDB

I feel silly for even having to ask this, but I must be missing something. I am simply trying to open a new SqlConnection to my LocalDB instance on my development machine. Visual Studio 2013. I can and am connected to LocalDB from SSMS and am not…
mituw16
  • 5,126
  • 3
  • 23
  • 48