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
4
votes
4 answers

one sql command with two connection string

I want to run this query in C# SELECT * FROM [FirstDataBase].[dbo].[table1] INNER JOIN [SecondDataBase].[dbo].[table2] and my code is : SqlConnection cn = new SqlConnection(myConnectionString); SqlCommand cmd = new SqlCommand(@"SELECT * FROM…
Atzi
  • 457
  • 1
  • 6
  • 16
4
votes
1 answer

Iterate through parameters in DbCommand

I'm trying to intercept and log all SQL commands to the database. And I need the parameters and their values as well. But command.Parameters doesn't have an IEnumerable. I could go for a for-statement but kinda feels like a set back. public override…
Quoter
  • 4,236
  • 13
  • 47
  • 69
4
votes
3 answers

Does .net SqlCommand.ExecuteReader close connection?

In this sentence: myCommand.ExecuteReader(CommandBehavior.CloseConnection) does it close connection in case of exception?
Antonio
  • 321
  • 2
  • 4
  • 9
4
votes
1 answer

Executing merge statement via C# SqlCommand not working

I am making my first attempt at using a temp table and a MERGE statement to update a SQL table via a SqlCommand object in C#. The program I'm working on is designed to first export a very large set of records (upwards of 20k+) into an excel…
NealR
  • 10,189
  • 61
  • 159
  • 299
4
votes
2 answers

How to insert the elements of an array into a SQL Server database using C#?

I need to insert the weather forecast (temperature) into a SQL Server database in order to control remotely the heating of a building. The steps are: Getting data from an RSS feed Putting them in an array Connecting to an SQL database Inserting the…
Yoan
  • 95
  • 1
  • 1
  • 7
4
votes
3 answers

How to make Sqlcommand accept null values

I'm trying to get data in a gridview from a database to show up in text boxes upon clicking and it works fine for the rows with no null data, although since my int columns have some null values my GetInt32 methods keep returning "Data is Null. This…
Nick V
  • 55
  • 6
4
votes
2 answers

Parameter.addwithvalue - ExecuteReader: CommandText property has not been initialized

I get the error ExecuteReader: CommandText property has not been initialized at the adapter.fill(ds) line. The weird thing is that if I replace @user with an actual string (e.g. 'name') it works perfectly fine, so something seems to be broken in the…
user1339253
  • 149
  • 1
  • 2
  • 15
4
votes
1 answer

Optional sql command parameters with Oracle Client

I'm trying to feed sqlcommand with more parameters than it uses. Because of that I'm getting exception ORA 01036. The description leads me to a fact that I can only pass parameters that will actually be used in the query. I cannot find it anywhere -…
IamDeveloper
  • 5,156
  • 6
  • 34
  • 50
4
votes
1 answer

How do multi rows insert with MySqlCommand and prepare statement?(#C)

Mysql give example how insert rows with prepare statement and .NET: http://dev.mysql.com/doc/refman/5.5/en/connector-net-programming-prepared.html Its looks that its works like that,because in the end of each iteration call…
Ben
  • 25,389
  • 34
  • 109
  • 165
4
votes
1 answer

vb.net sqlclient insert output

I have a ms sql table PRODUCTS. And it has three columns ID (int),NAME (nvarchar),TSTAMP (timestamp) I want to get new inserted row's both id and timestamp (like multiple select scope_identity). I can achieve that in sql as following: INSERT INTO…
user1645334
  • 89
  • 1
  • 7
4
votes
4 answers

Failed to convert parameter value from a String to a Decimal

Im importing a csv to my sql server table using the following code SqlCommand nonqueryCommand = myConnection.CreateCommand(); nonqueryCommand.CommandText = "INSERT INTO MYTABLE VALUES(@num1,…
John x
  • 4,031
  • 8
  • 42
  • 67
4
votes
3 answers

How to log executed sql statement

Scenario I am executing a stored procedure using ADO.NET SqlCommand: cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "sp_Trk_GetSuspiciusVisitor"; cmd.Parameters.Add(new SqlParameter("channel","gb")); cmd.Parameters.Add(new…
Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
4
votes
2 answers

Does SqlDbType.DateTime not store seconds?

using System.Data; using System.Data.SqlClient; I am using a SqlCommand cmd.Parameters of SqlDbType.DateTime to create a record in the database. Seconds are not being stored in the database. The field in the db is of type datetime. Do I need to do…
Lill Lansey
  • 4,775
  • 13
  • 55
  • 77
3
votes
4 answers

Is my SqlCommand variable overworked? Did it get tired?

In my testing project, I have a static class called FixtureSetup which I use to setup my integration testing data for validation. I use the same SqlCommand and SqlParameter variable (not the object itself) within that class, repeatedly, using the…
The Evil Greebo
  • 7,013
  • 3
  • 28
  • 55
3
votes
3 answers

Update DB using stored procedure and ADO.NET

I think I'm missing an 'USING" statement in my class as I'm getting an error when I try to set the commandType to stored procedure. When I type 'cmd.CommandType =', Intellisense fails to find the 'CommandType.StoredProcedure (Note: the function is…
Susan
  • 1,822
  • 8
  • 47
  • 69