ExecuteNonQuery Result in C# - c#

How can i know if i create a database successfully? I am using "CREATE DATABASE DemoDB" as a SQL command. ExecuteNonQuery() method returns 0. What should i use to understand if i created a database successfully?

As MSDN says:
For UPDATE, INSERT, and DELETE statements, the return value is the
number of rows affected by the command. When a trigger exists on a
table being inserted or updated, the return value includes the number
of rows affected by both the insert or update operation and the number
of rows affected by the trigger or triggers. For all other types of
statements, the return value is -1. If a rollback occurs, the return
value is also -1.

ExecuteNonQuery will return 0 for a CREATE DATABASE command because it returns the rows changed by your query.
This will return some rows if the DB exists:
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'DemoDB'

ExecuteNonQuery throws a MySqlException exception if there is an error. It returns 0 if successful but you don't need to check the return value; if it returns normally it has succeeded. For example here is the exception I get when I try to create a database that already exists:
Can't create database 'name'; database exists

Related

Is it possible to know when an upsert query has done nothing?

I have a chain of queries that start with an upsert. I would like the skip all of the following queries if the first query is found to be in conflict. Is there a way for the upsert to return a value that indicates a conflict exists? I am using the Npgsql nugget on c#
Npgsql's ExecuteNonQuery returns the number of rows affected, which will be 0 if all inserted rows had a conflict.

SQL Server/C#: Drop Multiple Tables/ExecuteNonQuery Results

Good morning everyone,
The short and sweet question is: what will SqlCommand.ExecuteNonQuery() return in the case of the query being multiple drop table statements?
For example, if I pass five tables to my drop method, and it builds a query with five drop table statements, what will ExecuteNonQuery return? I think it will return a value of negative one (based on MSDN), hoping it will return the exact count of tables that were dropped successfully, and hoping that it will not return the number of rows cumulatively removed from the database since this would be extremely excessive and most likely not the answer.
CODE
string query = string.Empty;
foreach (string name in tableNames)
query += $"DROP TABLE [{name}]; ";
using (SqlCommand cmd = new SqlCommand(query, conn)
droppedTableCount = cmd.ExecuteNonQuery(); // Returns number of rows affected.
return droppedTableCount;
MSDN
You can use the ExecuteNonQuery to perform catalog operations (for example, querying the structure of a database or creating database objects such as tables), or to change the data in a database without using a DataSet by executing UPDATE, INSERT, or DELETE statements. Although the ExecuteNonQuery returns no rows, any output parameters or return values mapped to parameters are populated with data. For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

Search for string in SQL table unsuccessful

I'm trying to search a sql table for a specific string, and return the number of observations found. It keeps returning -1 though, whether the string is in the table or not. Here's my code:
#{
Layout = "~/_Layout.cshtml";
Page.title = "TestArea";
var db = Database.Open("Cafeen");
string SearchWord = "Jolly";
var msg = db.Execute("SELECT COUNT(*) FROM Products WHERE ProductName = #SearchWord");
}
<p>#msg</p>
Should I perhaps use something other than COUNT(*)? What is the significance of -1? I would have assumed the expression to return 0 if the string can't be found.
You are using the WebMatrix.Data namespace. In this context you should call the QuerySingle method not the Execute one because, as many have already stated, that method is for not returning rows data.
The Execute method is used to perform non-query commands on a
database, such as the SQL Drop, Create, Delete, Update, and Insert
commands.
Moreover I suggest to change your query statement to a more performant one
var db = Database.Open("Cafeen");
string SearchWord = "Jolly";
string cmdText = #"IF EXISTS(SELECT 1 FROM Products
WHERE ProductName = #searchWord)
SELECT 1 ELSE SELECT 0";
int exists = Convert.ToInt32(db.QuerySingle(cmdText, SearchWord));
.....
Pertinent to the SQL Database, there is:
SqlCommand.ExecuteScalar Method ()
(re: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar(v=vs.110).aspx)
Otherwise, refer to Database.QueryValue Method (re: https://msdn.microsoft.com/en-us/library/webmatrix.data.database.queryvalue(v=vs.111).aspx)
Both methods return a scalar value from the first column/ first row.
Also, instead of COUNT(*) in SQL statement you can use COUNT(1) for better performance.
Hope this may help.
In db.Execute and SqlCommand.ExecuteNonQuery:
For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements like SELECT, the return value is -1. If a rollback occurs, the return value is also -1.
Have a look at the following links may be helpful:
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx
How to Identify whether SQL job is successfully executed or not in C#

SELECT ## IDENTITY using SQL Server CE from C# code

Im using Data Adapter/Set in SQL CE, i do create the following query to insert into table and then SELECT ##IDENTITY,
I want this SELECT statement return me the Student ID each time after Inserting into table, here is my Query:
INSERT INTO [Student] ([Name], [Family], [Address], [Phonenumber])
VALUES(#Name,#Family,#Address,#Phonenumber);
SELECT ##IDENTITY;
here is how i call query:
int x = da.Insert("Albert", "Alexandra", "No4.Oxford", Telnum);
Int x suppose to return me ID...
Here is the Error im getting :
There was an error parsing the query. [ Token line number = 4,Token line offset = 1,Token in error = SELECT ]
Insert Query it self it works but once adding SELECT ## IDENTITY at the end im getting error.
I really don't know what i'm doing wrong.
The return value of ExecuteNonQuery will be number of rows effected by these query. so you need to use store procedure instead of Single Query.
According to MSDN, CE doesn't support multiple commands per execution and you need to do this as two commands synchronously.
If you'd like to do this in a single call, you need to use a stored procedure rather than Insert, because it uses ExecuteNonQuery, which does not return any records. Otherwise you'll need to perform a select in another call to determine the identity.
The return value of ExecuteNonQuery is an integer that denotes the number of rows affected by your call.

ExecuteNonQuery doesn't return results

This is my (rough) code (DAL):
int i;
// Some other declarations
SqlCommand myCmdObject = new SqlCommand("some query");
conn.open();
i = myCmdObject.ExecuteNonQuery();
conn.close();
The problem is: Even though there is a record present on my SELECT query, the value in i remains -1.
What could be the problem?
What kind of query do you perform? Using ExecuteNonQuery is intended for UPDATE, INSERT and DELETE queries. As per the documentation:
For UPDATE, INSERT, and DELETE
statements, the return value is the
number of rows affected by the
command. When a trigger exists on a
table being inserted or updated, the
return value includes the number of
rows affected by both the insert or
update operation and the number of
rows affected by the trigger or
triggers. For all other types of
statements, the return value is -1.
Whenever you want to execute an SQL statement that shouldn't return a value or a record set, the ExecuteNonQuery should be used.
So if you want to run an update, delete, or insert statement, you should use the ExecuteNonQuery. ExecuteNonQuery returns the number of rows affected by the statement. This sounds very nice, but whenever you use the SQL Server 2005 IDE or Visual Studio to create a stored procedure it adds a small line that ruins everything.
That line is: SET NOCOUNT ON; This line turns on the NOCOUNT feature of SQL Server, which "Stops the message indicating the number of rows affected by a Transact-SQL statement from being returned as part of the results" and therefore it makes the stored procedure always to return -1 when called from the application (in my case a web application).
In conclusion, remove that line from your stored procedure, and you will now get a value indicating the number of rows affected by the statement.
Happy programming!
http://aspsoft.blogs.com/jonas/2006/10/executenonquery.html
You use EXECUTENONQUERY() for INSERT,UPDATE and DELETE.
But for SELECT you must use EXECUTEREADER().........
Because the SET NOCOUNT option is set to on. Remove the line "SET NOCOUNT ON;" in your query or stored procedure.
See more at SqlCommand.ExecuteNonQuery() returns -1 when doing Insert / Update / Delete.
Could you post the exact query? The ExecuteNonQuery method returns the ##ROWCOUNT Sql Server variable what ever it is after the last query has executed is what the ExecuteNonQuery method returns.
The ExecuteNonQuery method is used for SQL statements that are not queries, such as INSERT, UPDATE, ... You want to use ExecuteScalar or ExecuteReader if you expect your statement to return results (i.e. a query).
From MSDN: SqlCommand.ExecuteNonQuery Method
You can use the ExecuteNonQuery to
perform catalog operations (for
example, querying the structure of a
database or creating database objects
such as tables), or to change the data
in a database without using a DataSet
by executing UPDATE, INSERT, or DELETE
statements.
Although the ExecuteNonQuery returns
no rows, any output parameters or
return values mapped to parameters are
populated with data.
For UPDATE, INSERT, and DELETE
statements, the return value is the
number of rows affected by the
command. When a trigger exists on a
table being inserted or updated, the
return value includes the number of
rows affected by both the insert or
update operation and the number of
rows affected by the trigger or
triggers. For all other types of
statements, the return value is -1. If
a rollback occurs, the return value is
also -1.
You are using SELECT query, thus you get -1
If what you want is to get just a single integer from the query, use:
myCmdObject.ExecuteScalar()
if you want to run an update, delete,
or insert statement, you should use
the ExecuteNonQuery. ExecuteNonQuery
returns the number of rows affected by
the statement.
How to Set Count On

Categories