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
9
votes
2 answers

SQLCommand ExecuteNonQuery Maximum CommandText Length?

I've searched around the internet and everything seems to be about individual fields or doing one insert. I have a migration tool that is migrating from an old legacy database (superbase) to our SQL server DB (2008). Currently I'm reading 20,000…
DontFretBrett
  • 1,135
  • 3
  • 17
  • 32
9
votes
3 answers

How to set CommandTimeout

I am using Microsoft.SqlServer.Management.Smo. My Code: Server server = new Server(new ServerConnection( new SqlConnection(ConnectionString)); server.ConnectionContext.ExecuteNonQuery(script); I want to set CommandTimeout for it like we do with…
Dr. Rajesh Rolen
  • 14,029
  • 41
  • 106
  • 178
8
votes
5 answers

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

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…
John K
  • 28,441
  • 31
  • 139
  • 229
8
votes
4 answers

Parameterized SqlCommand - advantage of specifying SqlDbType?

Folks, Unless I'm mistaken, a parameterized sql query can be generated by specifying SqlDbType for each parameter or not. It seems that I can construct an SqlParameter by providing the parameter name and the value of the parameter only. What is…
Suraj
  • 35,905
  • 47
  • 139
  • 250
8
votes
3 answers

Combining output of two or more select statement

How to combine output of two or more SELECT statements, I have multiple tables which are having some data that I need to fetch them so I write multiple SELECT query. Now I want to combine result of the queries so what do I need to do ? I want the…
user2485642
  • 167
  • 1
  • 1
  • 8
8
votes
3 answers

How to chain multiple T-SQL statements (separated by GO's) into a single call to SQL using SqlCommand

I have a C# desktop app that calls various SQL Server stored procedures to perform various work of exporting and importing data to a SQL Server 2008 R2 database. These all work fine, no problem. And my app calls them just fine with all parameters…
Mike
  • 321
  • 2
  • 4
  • 9
8
votes
1 answer

Should you reuse SqlConnection, SqlDataAdapter, and SqlCommand objects?

I'm working with a DAL object that is written in a layout similar to the following code. I simplified a lot of the code code just to show the setup. public class UserDatabase : IDisposable { private SqlDataAdapter UserDbAdapter; private…
Equixor
  • 259
  • 4
  • 13
7
votes
2 answers

Does SqlCommand.Clone() create a deep copy or shallow copy?

Does SqlCommand.Clone() create a deep copy or shallow copy? Also, is it safe to call Clone() from multiple threads concurrently (create one command that multiple threads can copy, set parameter values, and execute)?
yzorg
  • 4,224
  • 3
  • 39
  • 57
7
votes
3 answers

How to Close a DataReader on Exception

I have the following code in some methods of my Data Layer: StringBuilder sb = new StringBuilder(); SqlCommand s = new SqlCommand(sb.ToString(), conn); try { SqlDataReader dr = s.ExecuteReader(); while(dr.Read()) …
apacay
  • 1,702
  • 5
  • 19
  • 41
7
votes
5 answers

Passing a parameter to an sql stored procedure in c#

string commandGetIslemIdleri = ("EXEC GetIslemIdleri"); cmd = new SqlCommand(commandGetIslemIdleri, sqlConn); cmd.Parameters.Add(new SqlParameter("@CARIID", 110)); using (var reader = cmd.ExecuteReader()) //error…
Bastardo
  • 4,144
  • 9
  • 41
  • 60
7
votes
5 answers

sql select average from distinct column of table

In my table I have some colms like this, (beside another cols) col1 | col2 s1 | 5 s1 | 5 s2 | 3 s2 | 3 s2 | 3 s3 | 5 s3 | 5 s4 | 7 I want to have average of ALL col2 over Distinct col1. (5+3+5+7)/4=5
BlueGirl
  • 491
  • 2
  • 9
  • 29
7
votes
1 answer

Add SqlParameter to bind LIKE '%@x%'

I am having an issue getting the following code to correctly add the SqlCommand parameter @vendor. For some reason, the query being passed seems to always be: select TOP 500 * from [mike_db].[dbo].[na_pe_sql_import] where vendname like…
66Mhz
  • 225
  • 2
  • 4
  • 12
7
votes
4 answers

Should I migrate to Entity Framework?

I've been using the SqlCommand class and writing my own T-SQL queries for a very long time. I've stuck with it because I'm comfortable with it, and it probably stemmed from me starting as a windows developer in the days before we had many other…
prospector
  • 3,389
  • 1
  • 23
  • 40
7
votes
2 answers

Strategy to optimize this large SQL insert via C#?

I have about 1.5 million files I need to insert records for in the database. Each record is inserted with a key that includes the name of the file. The catch: The files are not uniquely identified currently. So, what we'd like to do is, for each…
SeanKilleen
  • 8,809
  • 17
  • 80
  • 133
7
votes
2 answers

SqlCommand.Prepare method requires all parameters to have an explicitly set type

I have the following snippet of code in my WCF web service that builds a set of where conditions according to the formatting of the values of a provided dictionary. public static Dictionary[] VehicleSearch(Dictionary
taiidani
  • 147
  • 1
  • 3
  • 10