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
3
votes
2 answers

Text is being truncated on Insert into SQL Server database

We have a strange problem using SqlCommand to insert data into a SQL Server database. If we try to insert a large text into the column konnotiz1, the text is being truncated after 43245 characters. For some reason I don't understand the real…
Jürgen Hoffmann
  • 947
  • 4
  • 15
3
votes
2 answers

Why does a SqlException thrown by SqlCommand.ExecuteNonQuery contain all the PRINTs as errors?

When I run the following snippet try { using (SqlConnection conn = new SqlConnection("I'm shy")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "PRINT 'A';PRINT 'B';PRINT 'C';RAISERROR('SQL_Error', 18,…
Kevin Doyon
  • 3,464
  • 2
  • 33
  • 38
3
votes
4 answers

How to assign a value to sqlparameter NULL values without checking for null or 0?

The following check is needed to check for null values. is there a way i can do it directly? if(node.ID==0) { cmd.Parameters["@ID"].Value = DBNull.Value; } else { cmd.Parameters["@ID"].Value = node.ID; } is there way we can…
Dexters
  • 2,419
  • 6
  • 37
  • 57
3
votes
2 answers

SqlCommand Create Database race condition in C#

If I create a database and then try to connect to it with MS Sync Framework within 2 seconds it throw Exception "Cannot open database ..." Adding Thread.Sleep(4000); Will cause code to work properly, however I don't want a hard time set as this…
Brent
  • 1,378
  • 2
  • 16
  • 30
3
votes
3 answers

using SqlCommand and scope

Could anyone tell me perhaps whether there is a good reason to use one of the following two blocks of code rather than the other? using (SqlTransaction mySqlTransaction = mySqlConnection.BeginTransaction) { using (SqlCommand mySqlCmd = …
Dewald Swanepoel
  • 1,651
  • 4
  • 15
  • 38
3
votes
2 answers

How to pass "%" as part of a parameter to a SqlCommand?

When using SqlDataSource it is made this way: SqlDataSource1.SelectCommand = "SELECT field,field FROM TABLE WHERE name LIKE @name" SqlDataSource1.SelectParameters.Clear() SqlDataSource1.SelectParameters.Add( _ New Parameter("name",…
phalanx
  • 497
  • 5
  • 17
  • 33
3
votes
3 answers

c# execute SqlCommand with Parameters in "using" code block

I am attempting to execute an SQL Command through a 'using' code block, but can't seem to get it to work with Parameters. I get the error: 'Parameters does not exist in the current context', does anyone have a possible solution for this problem?…
Divide100
  • 224
  • 1
  • 2
  • 11
3
votes
4 answers

C# Local SQL rows returned if statement

I'm trying to get my login system to work. Currently I think I have everything in place for it to work except the if statement conditions (if row is returned, then if statement is true, else login unsuccessful). I'm not sure how to read in the…
3
votes
2 answers

Manipulate optional parameters in the WHERE clause

I'm working on a web-form that receives a string that contains an SQL query statement with SQL parameters. It generates an ASP.NET control for each parameter and then assigns the value of each control to the parameters of the SQL query string that…
Teo
  • 125
  • 1
  • 11
3
votes
2 answers

SqlCommand read one value

I have a problem with the value returned from SqlCommand, I have this code: string sqlSelect = "Select TOP 1 Quotation.SentToSupp as SentToSupp FROM Quotation JOIN Notifications ON Quotation.QuotationId = QuotationID "; SqlCommand Comm = new…
userS
  • 35
  • 1
  • 1
  • 3
3
votes
1 answer

Using Subquery in System.Data.SqlCommand (C#) - Syntax Error, working in SQL Server Mgmt Studio

I got a query used by a Webservice which contains a subquery. When executing the query in SQL Server Management Studio, it works fine. However, using this in the webservice as a SqlCommand, I get: System.Data.SqlClient.SqlException: Incorrect…
user2083834
  • 73
  • 1
  • 7
3
votes
6 answers

ExecuteNonQuery() not saving any record

I'm working a WinForms based C# tool which has an attached MDF file based database. I'm trying to use the SqlCommand.ExecuteNonQuery() method to save a record to this attached MDF database, but the record is not saved. No error or exception occurs;…
Ahmad
  • 12,886
  • 30
  • 93
  • 146
3
votes
3 answers

sql command for reading a particular sheet, column

This is probably a very stupid question for SQL stalwarts, but I just want one SQL command. Details, I am using a data analysis tool called R, this tool uses ODBC to read data from XLS. I am now trying to read data from an XLS file. The ODBC tool in…
Alphaneo
  • 12,079
  • 22
  • 71
  • 89
2
votes
6 answers

SqlCommand C# Issue

The below piece of code that throws the following exception.. Error Message: Object reference not set to an instance of an object. Stack Trace: at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior…
AskaQn
2
votes
3 answers

Why won't C# show the table

I'm using Visual Web Developer 2010 Express and SQL Server 2008 R2 Management Studio Express Hey guys, Pretty new at C# here. I'm trying to follow this C# ADO.NET tutorial (currently on step 2), and I'm stumped. I'm following all the steps, and…
Johnny Doe
  • 193
  • 2
  • 3
  • 16