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
0
votes
1 answer

Call linked server procedure

If I call procedure on linked server, I get the following error: Msg 0, Level 20, State 0, Line 0 A severe error occurred on the current command. The results, if any, should be discarded. It works in SSMS, also it works with classic asp, but not…
Simon
  • 1,955
  • 5
  • 35
  • 49
0
votes
2 answers

DataTable is not getting the data requested

I ran the SQL Query in SQL Server Management Studio and it worked. I get the following error in my WinForm C# application The parameterized query '(@word1 text)SELECT distinct [database].[dbo].[tableName].[n' expects the parameter '@word1', which…
Cocoa Dev
  • 9,361
  • 31
  • 109
  • 177
0
votes
1 answer

Insert values in separate table rows

I'm trying to insert checkboxlist multiple values in database, Here is my insert code: string str = string.Empty; try { var sb = new StringBuilder(); var collection = ListLawCat.CheckedItems; foreach (var item in…
Mandragorasprout
  • 123
  • 1
  • 6
  • 12
0
votes
3 answers

Altering data types within a table using SQL command ALTER

I have researched how to alter table data types and I understand how to do it but I cannot get it to work. I am trying to update my table Person within APP using the following command: ALTER TABLE APP.PERSON ALTER uName varchar; What I have tried…
John Vasiliou
  • 977
  • 7
  • 27
  • 48
0
votes
2 answers

How do I use SUM function in MySQL to return '0' if no values are found

I want my sum function to return '0', if a value does not exist, Can anyone identify if there is a problem with the following SQL command. I have changed the order of the coalesce but this has not made a difference SqlCommand scGetPostings = new…
0
votes
1 answer

Trying to query SQL Server 2008 from C#, program seems to hit an endless loop that I can't explain

I have a SQL Server database and I'm trying to query it. First I retrieve all names and id's from a table called Firms, use that to populate a ListBox. When I click a an item in that list box I want to populate another ListBox with employees that…
Kalec
  • 2,681
  • 9
  • 30
  • 49
0
votes
1 answer

Sql Timeout error, but why?

The following code results in a System.Data.SqlClient.SqlException: Timeout expired. const string sqlStmt = @"SELECT * FROM CUSTOMER_INFO WHERE CUSTOMER_NO = @CUSTOMER_NO;"; SqlCommand command =…
Stuart
  • 1,544
  • 5
  • 29
  • 45
0
votes
1 answer

Updating database table with dataset issues

im trying to update my database table with a dataset. I have a local excel file that can be edited and imported into a dataset. Then i want to use this dataset with the updated data to update my database table. This is my code: public void…
Lahib
  • 1,305
  • 5
  • 34
  • 62
0
votes
1 answer

How to use using in fillCommand?

I want to use using for sql command: using(SqlCommand command = new SqlCommand("usp_UpdateUser", _sqlConnection, _sqlTransaction)) { .... } But in FillCommand(ref command) I am getting exception: Cannot pass command as ref or out variable…
lakki
  • 127
  • 1
  • 5
  • 11
0
votes
3 answers

C# Connection And NonQuery Execution To Database

Just wondering if there are any typical issues when using this form of database command/connection. Is there something "better"? Anything else that could possibly help me improve my TSQL/C# skills would be much appreciated! Thank you! private void…
Joshua Volearix
  • 313
  • 1
  • 2
  • 11
0
votes
1 answer

Shortening data access code... would this work?

I am querying for data, if the data does not exist, I insert it. if it does, I do something else: SqlCommand checkHead = new SqlCommand("SELECT * FROM TABLE WHERE ORDER_NO = '" + orderNo + "';", connection); SqlDataReader checkHeadReader =…
Stuart
  • 1,544
  • 5
  • 29
  • 45
0
votes
5 answers

Store Value from sql Command and compare it with value

This is with reference to my previous question: Search data in database I have used the following code to execute my query. But I'm having issues for storing the value from the command and then use that result to compare the value. This is my…
Esha
  • 391
  • 1
  • 9
  • 37
0
votes
5 answers

C# abstraction and database layer

I am wondering what's a better way to abstract some of this code, into a simple DAL. At this time, I'm just patching the code and don't have time or need yet to use EF, Linq2Sql or any ORM right now. public string GetMySpecId(string dataId) …
cdub
  • 24,555
  • 57
  • 174
  • 303
0
votes
3 answers

Query not functioning with LIKE

I'm running a query via C# using an SqlCommand. One for of the query performs as expected, but when I complicate it, I'm getting strange results that don't seem to make sense. I'm hoping someone is this community can make more sense of it than I am…
steventnorris
  • 5,656
  • 23
  • 93
  • 174
0
votes
2 answers

True or False with Once SQL Command

I want to run sql command only once for update column.. UPDATE article SET published = '1 OR 0' WHERE id = '1' In above command "1 OR 0" is mean; If article's published column is 1/true, set 0/false.. If published is 0/false, set 1/true.. What can…
Bora
  • 10,529
  • 5
  • 43
  • 73