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

SQLCommand Object to Specify Query Hint

I just wonder if there is a way to specify query hint while calling a stored procedure using SQLCommand object? For example, I need to call something like this EXECUTE SPName'0068', 40 WITH RECOMPILE I just wonder, how I can simulate this using…
Sameers Javed
  • 342
  • 2
  • 5
  • 16
2
votes
2 answers

SQL error not rolling back entire SqlCommand

I have been placing set xact_abort on into SQL command statements and noticed that it is not rolling back updates, inserts, etc in my C# SqlCommand on error. Taken from this post. The MSDN states that: When SET XACT_ABORT is ON, if a Transact-SQL…
Matt
  • 358
  • 3
  • 9
  • 23
2
votes
1 answer

SqlCommand returns 1 but database doesn't get updated.. what am i doing wrong?

using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand("INSERT INTO Alarm (artistname, tijd) VALUES (@artistname, @tijd)", connection); cmd.CommandType = CommandType.Text; cmd.Connection…
2
votes
1 answer

how to run CUSTOM DDL DML ... On Spring Boot at run time, Without Forcing Hibernate

How to access database and run custom executable or query on it? after some search i found that i can use sessionFactory, but i have to force hibernate as data handler, well that make me worry if it have some bad effect or behavior in my complex…
Hassan Faghihi
  • 1,888
  • 1
  • 37
  • 55
2
votes
2 answers

How to pass variable into SqlCommand statement and insert into database table

I'm writing a small program in C# that uses SQL to store values into a database at runtime based on input by the user. The only problem is I can't figure out the correct Sql syntax to pass variables into my database. private void…
Rupert
  • 75
  • 1
  • 3
  • 7
2
votes
1 answer

Azure - SqlBulkCopy throwing a timeout expired exception

I'm using an azure sql database (v12) on a vm. I have two different databases instances - one for staging and one for production. I'm trying to grab the data off of staging and insert it into production with the click of a button. This code works…
c22joe
  • 43
  • 8
2
votes
1 answer

Update SQL database using VB.NET

The code throw no errors it just didn't Execute the satement Private Sub Update_Program(item As Programme) 'Set Command SchoolTypes.Connexion.Open() item.Command = New SqlClient.SqlCommand("UPDATE T_Programme Set pro_nom='@nom' ,…
Danys Chalifour
  • 322
  • 1
  • 5
  • 12
2
votes
1 answer

SqlCommand only returns one row

Trying to only return the first few number of rows because my database was too big, however when I was testing my SQL, I did a select * and was only returned the first row. SqlCommand sqlCmd = new SqlCommand(); SqlDataReader…
mimickd
  • 77
  • 9
2
votes
2 answers

How do I add multiple rows in a table?

string date = p_text_data.Text; string sql = @"INSERT INTO Warehouse (title,count,price,date) "; try { using (SqlConnection connection = ConnectToDataBase.GetConnection()) { SqlCommand command = new SqlCommand(sql, connection); …
Mediator
  • 14,951
  • 35
  • 113
  • 191
2
votes
1 answer

Incorrect syntax near the keyword 'USE' error while creating database from Script

I am developing a multi tenant application so I need to create database from code for every new client. I have generated script of my schema from SSMS "Generate Script" utility, then I tried the following: public static void CreateIfNotExist(string…
Awais Mahmood
  • 1,308
  • 4
  • 21
  • 51
2
votes
1 answer

What would cause/how can I prevent a ContextSwitchDeadlock?

I have a rather long-running process in a Windows Service that periodically throws a "ContextSwitchDeadlock" exception: I have also rigged my service to send myself emails with details about encountered exceptions. I get: Date: 05/25/2016…
2
votes
2 answers

C# - Output SqlParameter uses different values then the ones given?

I have a SqlCommand which runs a stored procedure that contains two integer output parameters. Right before the SqlCommand runs I can see that the output parameters are set to the correct values, however when the command actually executes, it uses a…
Rachel
  • 130,264
  • 66
  • 304
  • 490
2
votes
2 answers

C# SQLCommand - Parametrized query is cut short

Apologies for the formatting, it's a little rough I'm having an issue with an INSERT statement through a SQLCommand in C#. Once the insert is completed the ID is returned to the program. I have a number of variables populated and a SQLCommand coded…
IrishCrf
  • 105
  • 3
  • 13
2
votes
1 answer

How does dotNet handle parameterised dates where programmatic date has no time but sql date has time

Dreadful title right? Thought I'd see if Stack overflow is quicker than me testing something while I get a thousand interruptions from other work :) I'm updating an old VB net application and trying to refactor some of the logic as I go. The app…
Paul D'Ambra
  • 7,629
  • 3
  • 51
  • 96
2
votes
3 answers

How to add parameter in SELECT query for fieldName IN @fieldName construction

string idVariable = "qwerty"; string sqlQuery = "select id from user where id = @id"; sqlCommand.Parameters.Add("@id", SqlDbType.VarChar).Value = idVariable; Adding a value for a specific field is ok. What if I need to have a few ids and IN in…
Andrii Muzychuk
  • 1,151
  • 3
  • 20
  • 28