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

How can I declare and use T-SQL variables across multiple SqlCommands using the same SqlConnection object to perform multiple inserts?

I want to load a list of records given a possibly lengthy list of usernames (anywhere from one to thousands of usernames). Disregard how the name(s) are chosen, and assume they cannot be determined from any existing data in the database. This…
Triynko
  • 18,766
  • 21
  • 107
  • 173
3
votes
3 answers

SqlCommand, passing something that could be null

I have a SQL command that I've been asked to modify, and I'm having some troubles with the fact that what I'm passing to the SQL can now be null. If I'm passing a value, I can rely on the columnName = @parameterName in the SQL, but with NULL, I…
Drew McGhie
  • 1,086
  • 1
  • 12
  • 26
3
votes
6 answers

SQLCommand.ExecuteScalar() - why it throws a System.NullReferenceException?

Could anyone notice what could be wrong with the following function: public string Login(string username, string password) { string result = ""; string select = "SELECT user_id FROM [user] WHERE username = @username AND password…
Niko Gamulin
  • 66,025
  • 95
  • 221
  • 286
3
votes
7 answers

using the ?? operator and dealing with a null value

I am returning a scalar value from a SQL Server 2008 database: string reason = cmd.ExecuteScalar().ToString() ?? : ""; I want to make sure that if null is returned, that reason = "" and not null. i am getting an error on this line: Error 3 …
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
3
votes
1 answer

Is this Sql-injection-proof Asp.net code?

Problem: I have a form with text values, and a function that must return a string query based on the values of the text values too. Solution: I created a SQLCommand query with parameters, then I put the SQLCommand.CommandText to a string and I…
Attila
  • 702
  • 1
  • 12
  • 34
3
votes
2 answers

SqlConnection/SqlCommand keeps database in use after Close and Dispose

I am trying to create a temporary database for the purpose of integration testing in Xunit, but when I try to drop the temporary database, I get an error: Cannot drop database "TempDatabase_[numbers]" because it is currently in use. Simply closing…
VeeTheSecond
  • 3,086
  • 3
  • 20
  • 16
3
votes
2 answers

Why is my SqlParameter not being recognised?

I am getting this error: System.Data.SqlClient.SqlException: 'The parameterized query '(@inputKarakterSoort varchar(8000))SELECT TOP 2 * FROM Karakter ' expects the parameter '@inputKarakterSoort', which was not supplied. But I think I have given…
user14605908
3
votes
1 answer

Output parameter value wrong

I am passing a value to a parameter in a Stored Procedure and also declaring it's direction as ParameterDirection.InputOutput. In the Stored Procedure, the parameter is also declared as an OUTPUT parameter and the value being returned from the…
Leah
  • 2,547
  • 5
  • 23
  • 28
3
votes
3 answers

Convert SqlCommand Output to List?

I am using an ADO.NET SqlCommand with a single SqlDbType.Structured parameter to send a table-valued parameter to a sproc. The sproc returns many rows, which I need to get into a strongly-Typed List of . What is the best way to convert the result…
Snowy
  • 5,942
  • 19
  • 65
  • 119
3
votes
1 answer

Adding parameters to SQLCommand and adding value later

I've seen the different discussions of using Parameters.Add vs Parameters.AddWithValue (one is explicit, the other is not), but that isn't my issue. I'm defining a command, which will be used to do multiple inserts into the database. I figured I'd…
SchmitzIT
  • 9,227
  • 9
  • 65
  • 92
3
votes
5 answers

Must declare the scalar variable @param problem

Newbie alert! Error: Must declare the scalar variable "@param2". Must declare the scalar variable "@param2" (twice for two param2's) protected void Button1_Click(object sender, EventArgs e) { SqlDataSource ds1 = new…
Ranjanmano
  • 135
  • 1
  • 4
  • 14
3
votes
0 answers

SqlCommand Read() hanging randomly when returning results on Windows Server 2016 / SQL Server 2016

I have a custom application that builds a Lucene Index on our various SQL Server VMs hosted in Azure (it runs locally on the machine. I know this isn't ideal, but reasons). This program has run flawlessly on multiple machines for years. Recently we…
Rich
  • 39
  • 1
3
votes
2 answers

Rolling sum till a certain value is reached, plus calculated duration

I have a requirement where I need to know when the sum(value) reaches certain point and calculate duration. Below is the sample table. create table sample (dt timestamp, value real); insert into sample values ('2019-01-20 00:29:43 ',0.29) …
3
votes
1 answer

How to pass a variable to ODBC SQL Command in SSDT 2017?

I couldn't find the option to pass a parameter to the ODBC Source? I could see lot of article related to this but I couldn't find the same option in the Data flow Task expression. The [ODBC Source].[SqlCommand] isn't in the of the Data Flow…
Tanmoy Santra
  • 55
  • 1
  • 4
3
votes
2 answers

Why does IS NULL work but SQL Parameter with DBNull.Value for DateTime does not?

I have a C# console application that is performing a database lookup on SQL Server 2014. It queries against a table with a DateTime column called EffectiveDate which allows nulls. If I write a standard SQL query that uses 'WHERE EffectiveDate IS…
sldorman
  • 145
  • 2
  • 11