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

SqlParameter - Setting to DEFAULT Value

How do I set a SqlParameter's value to the equivalent of a SQL INSERT statement's DEFAULT keyword? MSDN documentation says to "use null or do not set Value to use the default value for the parameter." However, doing either results in a SqlException…
Ben Gribaudo
  • 5,057
  • 1
  • 40
  • 75
2
votes
2 answers

Disable DML queries in SQLCommand C#

Problem I'm writing a GUI application that allows users to generate an Excel file based on SELECT SQL query user enters in TextBox. It will connect to SQL server, run select over database, fill DataTable object and push that data to an Exel…
2
votes
3 answers

How to create a database trigger with ADO.NET

I am trying to create a trigger with SqlCommand and I am getting the error: Incorrect syntax near the keyword 'trigger' When I copy the same query in SQL Server it is executing successfully. Here is how the SQL command looks…
2
votes
2 answers

Commit multiple SqlCommands with SqlTransaction

I am trying to pass a list of SqlCommand into a member function that holds the connection to the database. public void CommitAsTransaction(List commands) { SqlTransaction transaction = null; SqlConnection connection = null; …
Hexadron
  • 58
  • 1
  • 4
2
votes
2 answers

Regex to remove escape characters (specific ones) in C#

The regex below is not what I exactly need: Regex.Replace(value.ToString(), "[^0-9a-zA-Z]+", "") I need to remove escape characters from my string because I am creating one SQL with string and when I have this character ' or this \r\n etc. my Sql…
Roger Oliveira
  • 1,589
  • 1
  • 27
  • 55
2
votes
1 answer

How to make an existing column primary key and auto increment in sql?

I have a column named FinderLogId in a table named FinderLog. I wanna make it primary key and auto increment. What I tried is the following: ALTER TABLE [gheymatyab.com_db].[dbo].[FinderLog] ALTER COLUMN [FinderLogId] int NOT NULL IDENTITY…
Hamid Reza
  • 2,913
  • 9
  • 49
  • 76
2
votes
4 answers

Write utf-8 to a sql server Text field using ADO.Net and maintain the UTF-8 bytes

I have some xml encoded as UTF-8 and I want to write this to a Text field in SQL Server. UTF-8 is byte compatible with Text so it should be able to do this and then read out the xml later still encoded as utf-8. However special characters such as…
Carrick
  • 4,423
  • 3
  • 18
  • 12
2
votes
3 answers

When does SqlCommand close when used in another function?

I have a program in C# where I created various class, and all of them need to make calls to the database. So I decided to create a static class that handles all the calls to allow me to apply far-reaching changes very easily. Within that class I…
2
votes
1 answer

Database.ExecuteNonQuery does not return

I have a very odd issue. When I execute a specific database stored procedure from C# using SqlCommand.ExecuteNonQuery, my stored procedure is never executed. Furthermore, SQL Profiler does not register the command at all. I do not receive a command…
Dan Waterbly
  • 850
  • 7
  • 15
2
votes
4 answers

What SQL is being sent from a SqlCommand object

I have a SqlCommand object on my c# based asp.net page. The SQL and the passed parameters are working the majority of the time. I have one case that is not working, I get the following error: String or binary data would be truncated. The statement…
Justin808
  • 20,859
  • 46
  • 160
  • 265
2
votes
2 answers

SQL ExecuteNonQuery for Multiple rows

I have a table("Product_Location") with the following columns: ProductID (PK), LocationID (PK), Quantity i would like to update the table in the database from rows in a datatable. if row already exists then Update quantity otherwise Insert new…
user2219524
  • 21
  • 1
  • 3
2
votes
1 answer

Mysql change column name from "group" to "group_code"

I have set a column name to "group", which turned out to be a reserved word. Now I try to change the name to "group_code", but I get an error. I try: ALTER TABLE task_values CHANGE group group_code VARCHAR(40) NOT NULL; and ALTER TABLE task_values…
alwbtc
  • 28,057
  • 62
  • 134
  • 188
2
votes
2 answers

SqlCommand.Parameters.AddWithValue Not Returning Correct Results

I'll admit that I'm a bit of a newbie (though learning fast!) when it comes to using parameterized queries in C#, so I'm probably just overlooking something here, but I can't seem to figure out how to get a parameterized query to work for me. Here…
Nate Irwin
  • 600
  • 1
  • 11
  • 20
2
votes
1 answer

Why does ExecuteReader() pad strings with traling spaces?

I have two queries: Q1: select name from presidents where country = 'USA' Q2: select 'Obama' as name from presidents where country = 'USA' When using System.Data.Odbc.OdbcCommandExecuteReader then the returned DbDataReader contains 'Obama' in case…
Patrick Fromberg
  • 1,313
  • 11
  • 37
2
votes
4 answers

Parse sql parameters from commandtext

Is it possible to parse sql parameters from plain commandtext? e.g. //cmdtext = SELECT * FROM AdWorks.Countries WHERE id = @id SqlCommand sqlc = new SqlCommand(cmdtext); SqlParameterCollection parCol = sqlc.Parameters //should contain now 1…
MJay
  • 487
  • 1
  • 6
  • 13