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

Refactor code into using statement

I have a dal layer with lots of methods, all of them call stored procedures, some return lists (so with a use of SqlDataReader), others only a specific value. I have a helper method that creates the SqlCommand: protected SqlCommand…
Gideon
  • 18,251
  • 5
  • 45
  • 64
2
votes
2 answers

SQL Procedure not working/very slow with SQL Command

I have the following scenario; Dim cmd as New SQLCommand cmd.Connection = myopenconnection cmd.CommandText = "usp_getdata" cmd.Parameters.AddWithValue("@Year", 2008) cmd.CommandType = StoredProcedure Dim reader = cmd.ExecuteReader The application…
A. Agius
  • 1,211
  • 1
  • 14
  • 28
2
votes
2 answers

How to select all rows WHERE a string filed start with a specific string

How can I do this: select all rows WHERE a string filed start with a specific string like this (it's a command for SqlDataAdapter select command string in C#): SELECT * FROM mytable WHERE user_id = asd* asd means all user_id filed that start with…
hamze
  • 7,061
  • 6
  • 34
  • 43
2
votes
1 answer

Will a SqlConnection() declared before using it in a using() statement still close the connection when done? (C#/SQL Server)

In a situation like this: SqlConnection cn = new SqlConnection( ConfigurationManager .ConnectionStrings["AppConnection"] .ConnectionString ); using (cn) {...} Will the using() statement still close the connection even though it wasn't…
KWallace
  • 1,570
  • 1
  • 15
  • 25
2
votes
0 answers

Fastest bulk deleting using LINQ-to-SQL

I have an old solution running on LINQ-to-SQL (DMBL), where I want to delete a large amount of rows based on id's (incl. a lot of referenced objects). But the traditional way of using DeleteOnSubmit or DeleteAllOnSubmit is too slow (my calculation…
baddaydaddy
  • 604
  • 8
  • 25
2
votes
2 answers

C# | Use parameter in SqlCommand query

I have this ASP.NET Core project and in my DAL I collect my data from my database, but I want the user to be able to type a type character (Karakter is character in Dutch) and the DAL should pick 2 random characters from the database with…
user14443684
2
votes
0 answers

Temp tables via SqlCommand from .NET application

I use following pattern to work with SQL Server temp tables from .NET applications: Open the connection Create a temp table (only with ad hoc command!!!) Write a lot of data into it via SqlBulkCopy Perform a select/update with join to this…
alex4711
  • 21
  • 1
2
votes
3 answers

How to insert a c# datetime var into SQL Server

Here is the code: string ConnectionString= @"Data Source=localhost\SQLEXPRESS; Initial Catalog=notepad; Integrated Security=SSPI "; SqlConnection con = new SqlConnection(ConnectionString); con.Open(); string strEvent = TextBoxEvent.Text; string…
richard
  • 21
  • 1
  • 2
2
votes
1 answer

How to convert SqlCommand query to use parameters built using concatenated strings including variables using ternary operators

this is my first question, hopefully I do things correctly. Also to preface, I'm a programming noob tasked with developing and fixing some code where I work since the programmer we've had until now left. And if done quite well for now with my…
2
votes
2 answers

Prepared Statements in SQL-Server through C#

i found using of prepared statements in PHP by mysqli_stmt_prepare() Function. what is like it in C# for SQL-Server? i found this code example(using parameterize command). is this what i am looking for? SqlConnection conn = new…
ahoo
  • 1,321
  • 2
  • 17
  • 37
2
votes
2 answers

Read in a txt file using sqlcmd?

Is it possible to read in a txt file using sqlcmd, to avoid needing to type every cell by hand? I've been given a txt file with every table row on one line, and every column seperated by a comma. (a .cvs file) I've been trying to look up some…
Hanna
  • 10,315
  • 11
  • 56
  • 89
2
votes
0 answers

How to read complete result of FOR XML SQL Server Query?

I want to save the complete result of a FOR XML SQL Query to a file. My SQL Query looks something like this: SELECT * FROM Customer FOR XML RAW in my code, I now want to execute this query against an SQL Server and read the complete XML result and…
dczychon
  • 51
  • 1
  • 5
2
votes
0 answers

Change parameter indicator in SqlCommand from '@' to ':'

I'm working on library which allows me to create connections, commands, readers etc for 3 db types: oracle, postgresql and sql server. I need to pass parameters into query but it seems like postgres and oracle supports : sign as parameter indicator,…
Adam Mrozek
  • 1,410
  • 4
  • 26
  • 49
2
votes
3 answers

Problem with query string and extract values

I can't extract the values through a query and insert them into textboxes Where am I going wrong? Request.QueryString.Get("ID_Persona"); string query = "SELECT ID,Nome,Cognome,Email,CodiceFiscale FROM Persona WHERE ID = @id"; using…
Elmachico
  • 128
  • 13
2
votes
1 answer

C# ExecuteScalar() null COUNT vs SELECT

I noticed some odd behavior and hoped one of the experts could explain the difference. My UI requires an image is unique before presenting it to the user for their task. I store checksums in the database and query those for unique values. I…
JoeBob_OH
  • 417
  • 3
  • 10