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
5
votes
1 answer

Single SqlConnection with Transaction, multiple commands situation

I'm trying to use only a single connection and run two commands together, one using transaction and one without. The one without is a trace/logging function as this solution is deployed in another location. So that when the process fails partly I…
Uknight
  • 711
  • 6
  • 9
5
votes
2 answers

How can I Pass a Table Name to SqlCommand?

I am trying to pass a table name as a parameter to my query through SqlCommand but it doesn't seems to be working. Here is my code; SqlConnection con = new SqlConnection( "server=.;user=sa;password=12345;database=employee" ); con.Open(…
Uthaiah
  • 1,283
  • 13
  • 14
5
votes
5 answers

C# Update Table using SqlCommand.Parameters

I'm trying to update an MSSQL table using SqlCommand, I think it's a syntax error with my T-SQL, but here is what I have so far: SqlCommand sqlCmd = new SqlCommand("UPDATE yak_tickets SET email = @emailParam, subject = @subjectParam, text =…
skylerl
  • 4,030
  • 12
  • 41
  • 60
5
votes
3 answers

"On Delete Cascade" if deleting a row from the child table

I'm having a difficult time understanding "on delete cascade" If I had the following example: create table X (id int primary key, name char(10)); create table Y (bid int primary key, aid references A(id) on delete cascade); X contains one row (111,…
5
votes
4 answers

Can't gzip my mysqldump on the command line windows

I'm trying to make a back up of my MySQL db and zip the file. Every time I try to run this command... C:\wamp\bin\mysql\mysql5.5.24\bin\mysqldump.exe -u usernam -ppassword db_name | gzip > sites\www.site.com-local\backups\backup-date.sql.gz All I…
Richard Skinner
  • 738
  • 3
  • 10
  • 26
5
votes
1 answer

How can I delete a row from an Oracle database in C#?

I have a problem when I want to delete an entire row based on id_curse. This is my code and this is my error I get: An unhandled exception of type 'Oracle.DataAccess.Client.OracleException' occurred in Oracle.DataAccess.dll. Additional…
Irina
  • 65
  • 1
  • 1
  • 5
5
votes
2 answers

Executing an SQLCommand without specifying a Transaction

We have some lists of data being fetched in our application via a SqlCommand performing a SELECT query on a SQL Server database. We do not explicitly setup a transaction on the SqlCommand, instead just passing it a SqlConnection and running it. Is…
Graham
  • 305
  • 1
  • 5
  • 11
5
votes
2 answers

Create SQL Server table programmatically

I need to programmatically create a SQL Server 2008 table in C# such that the columns of the table should be generated from a list of columns (each column name is the name of a row in the table) My question is what is the command string to loop…
wingman55
  • 119
  • 2
  • 2
  • 8
4
votes
5 answers

Does Mono treat System.Data.SqlCommands differently to .NET?

I'm having some trouble with an application that we're porting to Mono. (For reference, the .NET runtime is 4.0, and the mono version is 2.6.7.) EDIT: This problem persists on Mono 2.10.2 As part of the startup of the application, it reads data into…
simple
  • 100
  • 9
4
votes
2 answers

Do I need to put top 1 when using SqlCommand.ExecuteScalar()

I've analyzed the code of SqlCommand and see that it works fine (see CompleteExecuteScalar method). But I'm not sure should I use top 1 in the query. For example I have select name from Person and table Person contains a lot of records. Should I…
Serg046
  • 1,043
  • 1
  • 13
  • 42
4
votes
3 answers

Problems Passing Parameters into SqlCommand

I'm having problems passing parameters to a SQL string for a SqlCommand. When I use option 1 (see below), the code works. When I use option 2, it doesn't work. I'm not sure how to get the .AddWithValue method to work with the SqlCommand. Any help…
UltraJ
  • 467
  • 1
  • 10
  • 20
4
votes
2 answers

What SqlCommand.Parameters.AddWithValue really does?

What changes SqlCommand.Parameters.AddWithValue() does with the query? I expect that: It replaces every ' character by '', If a parameter value is a string or something which must be converted to a string, it surrounds the value by ', so for…
Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
4
votes
3 answers

Calling sqlCommand in a loop increasing exec time every step

I've got a loop that executes the stored procedure in a loop with over 40,000 iterations, like so: SqlCommand command = new SqlCommand("WriteDataToDB"); command.Connection = _connection; command.CommandType =…
Michael
  • 43
  • 3
4
votes
1 answer

How to use stored procedure in C# to return a list of results?

Here is my stored procedure: CREATE Proc UpdateChecklist ( @TemplateId As INT ) as begin select MF.CheckListDataId from TemplateModuleMap TM inner join ModuleField MF on TM.ModuleId = MF.ModuleId where TM.TemplateId =…
MinhNguyen
  • 816
  • 1
  • 11
  • 26
4
votes
7 answers

How to capture SQL with parameters substituted in? (.NET, SqlCommand)

If there an easy way to get a completed SQL statement back after parameter substitution? I.e., I want to keep a logfile of all the SQL this program runs. Or if I want to do this, will I just want to get rid of Parameters, and do the whole query…
Bryan
  • 417
  • 6
  • 17