sqlcommand

SqlCommand.Parameters.AddWithValue Not Returning Correct Results

I'll admit that I'm a bit of a newbie (though learning fast!) when it comes to using parameterized queries in C#, so I'm probably just overlooking something here, but I can't seem to figure out how to get a parameterized query to work for me. Here is a much simplified example. If more information is needed, I am certainly willing to sup...

SQLCommand/SQLConnection vs OleDbCommand/OleDbConnection

Does it make a difference whether I use SQLCommand/SQLConnection instead of OleDbCommand/OleDbConnection. Do I get any advantages out of that, from a API-comfortability-, feature-, performance- or securitly perspective? Or any other perspective? ...

SqlCommand() ExecuteNonQuery() truncates command text.

I'm building a custom db deployment utility, I need to read text files containing sql scripts and execute them against the database. Pretty easy stuff, so far so good. However I've encountered a snag, the contents of the file are read successfully and entirely, but once passed into the SqlCommand and then executed with SqlCommand.Exec...

ADO.NET Command Classes - ExecuteNonQuery Method

I've heard that Microsoft accepts that the name of ExecuteNonQuery method of command classes was given inappropriate. Is there any official information about it, or it is just a rumour? ...

How do I delete all tables in a database using SqlCommand?

Unlike the other posts about the task "delete all tables", this question is specifically about using SqlCommand to access the database. A coworker is facing the problem that no matter how it attempts it, he can't delete all tables from a database via SqlCommand. He states that he can't perform such action as long as there is an active c...

What SQL is being sent from a SqlCommand object

I have a SqlCommand object on my c# based asp.net page. The SQL and the passed parameters are working the majority of the time. I have one case that is not working, I get the following error: String or binary data would be truncated. The statement has been terminated. I understand the error and but all the columns in the database shoul...

Database.ExecuteNonQuery does not return

I have a very odd issue. When I execute a specific database stored procedure from C# using SqlCommand.ExecuteNonQuery, my stored procedure is never executed. Furthermore, SQL Profiler does not register the command at all. I do not receive a command timeout, and no exeception is thrown. The weirdest thing is that this code has worked fi...

Bizarre Escape Character Question

I have the following c# code embedded in a literal <% %> of a c# asp.net page string commandString = "SELECT tblData.Content " + "FROM tblData " + "WHERE (tblData.ref = N\'%"+myCurrentREF+"%\')"; This is breaking my code since it apparently cannot use the \' escape character. Why is it s...

SqlCommand Timeout Expired on Simple Insert Statement

I am trying to execute a simple insert statment into a table with a large amount of rows...one of the columns in the table is of Xml data type sqlCmd.CommandText = String.Format( @"INSERT INTO Responses (CorrelationId, LoanNumber,ClientId, Received, Source, ResponseXml, Success, ErrorMessage) VALUES ('{0}', '{1}', '{2}...

SqlCommand.Parameters.AddWithValue issue

Hi I have a problem with the folowwing piece of code. I am passing a parameter (List) to a methods executing the following code. When it executes sql throws an error saying that the proc expects a parameter that was not provided. I know this error and understand it, and when stepping through the code I can see that the cmdExecuteReader ...

Is it possible to get the parsed text of a SqlCommand with SqlParameters?

What I am trying to do is create some arbitrary sql command with parameters, set the values and types of the parameters, and then return the parsed sql command - with parameters included. I will not be directly running this command against a sql database, so no connection should be necessary. So if I ran the example program below, I wo...

Why the "Non" in "ExecuteNonQuery"?

I know this is not a hell of an useful question but I can't help being bugged by it. So, Why said method (in *Command classes) is called ExecuteNonQuery instead of ExecuteQuery? Aren't those SQL statements we throw at DBs, queries? ...

How to capture SQL with parameters substituted in? (.NET, SqlCommand)

Hello, If there an easy way to get a completed SQL statement back after parameter substitution? I.e., I want to keep a logfile of all the SQL this program runs. Or if I want to do this, will I just want to get rid of Parameters, and do the whole query the old school way, in one big string? Simple Example: I want to capture the outp...

In Java, send commands to another command-line program

I am using Java on Windows XP and want to be able to send commands to another program such as telnet. I do not want to simply execute another program. I want to execute it, and then send it a sequence of commands once it's running. Here's my code of what I want to do, but it does not work: (If you uncomment and change the command to "cmd...

Write utf-8 to a sql server Text field using ADO.Net and maintain the UTF-8 bytes

I have some xml encoded as UTF-8 and I want to write this to a Text field in SQL Server. UTF-8 is byte compatible with Text so it should be able to do this and then read out the xml later still encoded as utf-8. However special characters such as ÄÅÖ, which are multi-byte in UTF-8 get changed on the way. I have code like this: byte[] ...

Under what circumstances is an SqlConnection automatically enlisted in an ambient TransactionScope Transaction?

What does it mean for an SqlConnection to be "enlisted" in a transaction? Does it simply mean that commands I execute on the connection will participate in the transaction? If so, under what circumstances is an SqlConnection automatically enlisted in an ambient TransactionScope Transaction? See questions in code comments. My guess to...

Do i need to dispose of MySqlCommand?

I find it incredibly annoying to write a using statement on every one of my queries (which require its own command or write parameters.clear()) which sometimes require declaring variables outside of the using block. Its so incredibly annoying and looks much dirtier compared to the version without disposing the object. Do i need to dispo...

What value should .net SqlCommand.ExecuteNonQuery() return if no rows are affected?

I have the following code: int result = -1; StringBuilder sb = new StringBuilder(); SqlCommand cmd = MyConnection. sb.AppendLine("delete from Table1 where ID in"); sb.AppendLine("(select id from Table1 t1 where not exists(select * from Table2 t2 where t2.Table1ID = t1.ID))"); cmd.CommandText = sb.ToString(); result = cmd.ExecuteNonQuer...

SqlCommand.Dispose() not disposing the SqlParameters in it - Memory Leak - C#.NET

Hello I've a windows forms application with MS SQL Server 2005 as the back end. I have written code in the form to call few stored procedures using SqlConnection, SqlCommand objects and i properly dispose everything. I've disposed sqlcommand object by calling oSqlCommand.Dispose() But i witnessed my application consuming huge amount...

Select most frequent from column in

Hi i have a columm in the table and i want to select the most common item from the selected column. The table is set up publication: id Title published I want to beable to select the most recurring places where publications have been published. Is this possible to do? Thanks in Advance Dean ...