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

SqlCommand() ExecuteNonQuery() truncates command text

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…
H. Abraham Chavez
  • 828
  • 2
  • 8
  • 20
15
votes
5 answers

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

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…
Burg
  • 1,988
  • 1
  • 18
  • 22
15
votes
5 answers

I'm getting an error in SQL command not properly ended

when I enter this INSERT INTO works_on (essn, pno, hours) values ('123456789', 1, 32.5), ('123456789', 2, 7.5), ('666884444', 3, 40.0), ('453453453', 1, 20.0), ('453453453', 2, 20.0), ('333445555', 2, 10.0), ('333445555', 3, 10.0), ('333445555', 10,…
UnPatoCuacCuac
  • 315
  • 2
  • 7
  • 18
14
votes
1 answer

Set custom default CommandTimeout for all new Command Objects

The default CommandTimeout value is 30 seconds. You can manually change the value on an instance of the command object by doing the following Dim cmd As New System.Data.SqlClient.SqlCommand cmd.CommandTimeout = 60 Is there a way to specify a…
KyleMit
  • 30,350
  • 66
  • 462
  • 664
12
votes
4 answers

SqlCommand.Cancel() causes a performance boost?

I have seen this show up several places in code, never with an explanation, just a cryptic comment above it (Declaration and execution included for an idea of context. It's just a standard procedure of running a SqlCommand): //SqlCommand cmd = new…
fire.eagle
  • 2,723
  • 1
  • 21
  • 23
12
votes
5 answers

When would SqlCommand.ExecuteReader() return null?

When using calling the SqlCommand.ExecuteReader() method, ReSharper tells me I have a possible NullReference exception when I use the SqlDataReader object afterwards. So with the following code: using (SqlConnection connection = GetConnection()) { …
Ray
  • 45,695
  • 27
  • 126
  • 169
11
votes
2 answers

Confused between SqlCommand & SqlDataAdapter

everyone I am a student and new to .NET and specially MVC3 development but for one of my project I’ve to work over it and so going through the learning phase Issue and confusion I am facing is regarding DB-Connectivity, whast I leanree d regarding…
Maven
  • 14,587
  • 42
  • 113
  • 174
11
votes
1 answer

What does SqlDbType.Structured mean?

From msdn website I get the following: A special data type for specifying structured data contained in table-valued parameters. It seems my code works with it and without it (pushing table to DB using stored procedure). Can someone explain what…
Zbun
  • 4,875
  • 4
  • 19
  • 28
10
votes
7 answers

ExecuteNonQuery inside loop

I'm trying to insert a database record inside a loop in C#. It works when I hard code the values like this: string query3 = "INSERT INTO furniture (room_id,member_id) VALUES (222,333);"; SqlCommand cmd3 = new SqlCommand(query3,…
Dan
  • 5,836
  • 22
  • 86
  • 140
10
votes
2 answers

poor performance with sqlparameter

I have a web service, so the handler is called multiple times concurrently all the time. Inside I create SqlConnection and SqlCommand. I have to execute about 7 different commands. Different commands require various parameters, so I just add them…
10
votes
1 answer

How to stop PowerShell SqlCommand from echoing parameters

I have a few functions that use the SqlCommand object to to data inserts, and queries. But one function (the last one in the file) seems to echo (most of) the attributes into the output. The function in question: function…
Tim Meers
  • 928
  • 1
  • 14
  • 24
10
votes
4 answers

In Java, send commands to another command-line program

I am using Java on Windows XP and want to be able to send commands to another program such as telnet. I do not want to simply execute another program. I want to execute it, and then send it a sequence of commands once it's running. Here's my code of…
bradvido
  • 2,743
  • 7
  • 32
  • 49
10
votes
1 answer

SqlCommand (Using Statement / Disposing issue)

Take the following example... Using cn As New SqlConnection(ConnectionString) Try Dim cmd As SqlCommand = New SqlCommand With cmd .Connection = cn …
cw_dev
  • 465
  • 1
  • 12
  • 27
10
votes
2 answers

How To perform a SQL Query to DataTable Operation That Can Be Cancelled

I tried to make the title as specific as possible. Basically what I have running inside a backgroundworker thread now is some code that looks like: SqlConnection conn = new SqlConnection(connstring); SqlCommand cmd = new…
David W
  • 487
  • 2
  • 5
  • 12
9
votes
1 answer

Why both SqlConnection and SqlTransaction are present in SqlCommand constructor?

I wonder, what is the reason to have this SqlCommand constructor overload: public SqlCommand( string cmdText, SqlConnection connection, SqlTransaction transaction ) ? When I need to create an internal method that does its bit using a…
GSerg
  • 76,472
  • 17
  • 159
  • 346
1 2
3
61 62