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
407
votes
26 answers

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated

I have many users on my web site (20000-60000 per day), which is a download site for mobile files. I have remote access to my server (windows server 2008-R2). I've received "Server is unavailable" errors before, but I am now seeing a connection…
SilverLight
  • 19,668
  • 65
  • 192
  • 300
354
votes
8 answers

How to export and import a .sql file from command line with options?

Not Duplicate! looking for some feature have phpmyadmin during export in command line I want to export and import a .sql file to and from a MySQL database from command line. Is there any command to export .sql file in MySQL? Then how do I import…
AZinkey
  • 5,209
  • 5
  • 28
  • 46
208
votes
3 answers

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

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…
Triynko
  • 18,766
  • 21
  • 107
  • 173
192
votes
6 answers

How to pass table value parameters to stored procedure from .net code

I have a SQL Server 2005 database. In a few procedures I have table parameters that I pass to a stored proc as an nvarchar (separated by commas) and internally divide into single values. I add it to the SQL command parameters list like…
124
votes
8 answers

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

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…
John B
  • 20,062
  • 35
  • 120
  • 170
114
votes
11 answers

Is it safe to not parameterize an SQL query when the parameter is not a string?

In terms of SQL injection, I completely understand the necessity to parameterize a string parameter; that's one of the oldest tricks in the book. But when can it be justified to not parameterize an SqlCommand? Are any data types considered "safe" to…
johnnyRose
  • 7,310
  • 17
  • 40
  • 61
98
votes
6 answers

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

Is there any difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout in .NET?
Dhanapal
  • 14,239
  • 35
  • 115
  • 142
64
votes
5 answers

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

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 =…
TAG
  • 1,232
  • 1
  • 10
  • 12
63
votes
27 answers

How to clear MySQL screen console in Windows?

The title is my question. I googled and try something like mysql> !\ clear mysql> !\ cls mysql> system cls mysql> system clear blah blah ... but none of them works. Anyone show me how to clear screen, just like cls command in Windows?
Leo Lerdorf
  • 1,304
  • 4
  • 15
  • 19
62
votes
9 answers

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

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…
user1831272
  • 683
  • 2
  • 7
  • 6
61
votes
4 answers

Does SqlCommand.Dispose close the connection?

Can I use this approach efficiently? using(SqlCommand cmd = new SqlCommand("GetSomething", new SqlConnection(Config.ConnectionString)) { cmd.Connection.Open(); // set up parameters and CommandType to StoredProcedure etc. etc. …
Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166
53
votes
4 answers

ReadOnlyException DataTable DataRow "Column X is read only."

I've got a short piece of code that originally created an SqlDataAdapter object over and over. Trying to streamline my calls a little bit, I replaced the SqlDataAdapter with an SqlCommand and moved the SqlConnection outside of the loop. Now,…
user153923
48
votes
2 answers

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

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…
PeteT
  • 18,754
  • 26
  • 95
  • 132
46
votes
2 answers

Replacing a DataReader with a DataTable

I'm adapting some code that someone else wrote and need to return a DataTable for time's sake. I have code like this: using (SqlCommand command = new SqlCommand(query, conn)) { //add parameters and their values using (SqlDataReader dr =…
cdub
  • 24,555
  • 57
  • 174
  • 303
36
votes
4 answers

revoke vs deny : what is the difference

What is the difference between the DENY and REVOKE commands in SQL Server?
ceth
  • 44,198
  • 62
  • 180
  • 289
1
2 3
61 62