473,405 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

executenonquery returning -1 problem

Hi,
I'm using executenonquery to check the existence of table in the
database.
I tried two methods of queries, everything return -1 even though table
exists in database

Code as below
SqlConnection connection=new SqlConnection("Data Source=\LOCAL;
Initial Catalog=ff; Integrated Security=SSPI; Persist Security
Info=False; ");
connection.Open();
string tableExistsCheckQuery="SELECT * FROM
INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='resultsc'";
//string tableExistsCheckQuery="SELECT * FROM dbo.sysobjects WHERE
id =OBJECT_ID(N'[dbo].[resultsc]')AND OBJECTPROPERTY(id,
N'IsUserTable') = 1";

SqlCommand command=new
SqlCommand(tableExistsCheckQuery,connection);
int a=command.ExecuteNonQuery();
'a' always return -1 if table exists in data base or not
I tried changing the query as below
string tableExistsCheckQuery="SELECT * FROM dbo.sysobjects WHERE id
=OBJECT_ID(N'[dbo].[resultsc]')AND OBJECTPROPERTY(id, N'IsUserTable')
= 1";

its also returning -1.

Is there any mistake in my code?

Thanks,
Mani

Jun 20 '07 #1
3 7747
On Jun 20, 1:03 pm, Manikandan <plmanikan...@gmail.comwrote:
I'm using executenonquery to check the existence of table in the
database.
I tried two methods of queries, everything return -1 even though table
exists in database
<snip>
Is there any mistake in my code?
Yes - you're using a select statement and expecting a result other
than -1. From the docs:

<quote>
For UPDATE, INSERT, and DELETE statements, the return value is the
number of rows affected by the command. For all other types of
statements, the return value is -1.
</quote>

Now, are you trying to get the count from your query, if so,
ExecuteScalar is probably what you're after.

Jon

Jun 20 '07 #2
Hi,

A better approach would be:

SELECT count(*) FROM ......

and in your code:

bool exist = Convert.ToBoolean( com.ExecuteScalar() );

"Manikandan" <pl**********@gmail.comwrote in message
news:11********************@o61g2000hsh.googlegrou ps.com...
Hi,
I'm using executenonquery to check the existence of table in the
database.
I tried two methods of queries, everything return -1 even though table
exists in database

Code as below
SqlConnection connection=new SqlConnection("Data Source=\LOCAL;
Initial Catalog=ff; Integrated Security=SSPI; Persist Security
Info=False; ");
connection.Open();
string tableExistsCheckQuery="SELECT * FROM
INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='resultsc'";
//string tableExistsCheckQuery="SELECT * FROM dbo.sysobjects WHERE
id =OBJECT_ID(N'[dbo].[resultsc]')AND OBJECTPROPERTY(id,
N'IsUserTable') = 1";

SqlCommand command=new
SqlCommand(tableExistsCheckQuery,connection);
int a=command.ExecuteNonQuery();
'a' always return -1 if table exists in data base or not
I tried changing the query as below
string tableExistsCheckQuery="SELECT * FROM dbo.sysobjects WHERE id
=OBJECT_ID(N'[dbo].[resultsc]')AND OBJECTPROPERTY(id, N'IsUserTable')
= 1";

its also returning -1.

Is there any mistake in my code?

Thanks,
Mani

Jun 20 '07 #3
On Jun 20, 8:22 am, "Ignacio Machin \( .NET/ C# MVP \)" <machin TA
laceupsolutions.comwrote:
Hi,

A better approach would be:

SELECT count(*) FROM ......

and in your code:

bool exist = Convert.ToBoolean( com.ExecuteScalar() );

"Manikandan" <plmanikan...@gmail.comwrote in message
or even just Select 1 from...

Jun 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Mike | last post by:
I know this sounds strange but I am at a loss. I am calling a simple funtion that opens a connection to a SQL Server 2000 database and executes an Insert Statement. private void...
5
by: Paul Aspinall | last post by:
Hi I have a Stored Proc in SQL server, which creates a record key when a record is created. I want to return the value back to my code, once the record has been created. I am using SQLHelper...
4
by: MDW | last post by:
Hey all. I'm confused. I'm trying to add a single record into an Access 2000 database using ASP.Net. Here is the code: objConn = New OleDbConnection(strConnect) objConn.Open objCommand =...
2
by: jdb | last post by:
Hi, I am adding a record to the database using an ExecuteNonQuery, which adds without problem. Now after the record is added I run a method passing in some info as well as the curretnly opened...
2
by: jzogg7272 | last post by:
In my code I am executing a stored procedure to do a single row insert. I check the return value of the execution and I am getting -1, whereas a few weeks ago it was returning 0. Actually, I found...
22
by: b_r | last post by:
Hi, I'm trying to make a simple operation (insert into DB) in VB 2005 and SQL Server. The code is as follows: Dim sConnectionString As String = _ "Data...
5
by: Matt | last post by:
Hi all, I'm trying to use the following code in an application: // retrieve the connection string string strDbcx = Globals.strDbcx; // create the connection OleDbConnection dbcx = new...
8
by: roundcrisis | last post by:
Hi all: I m trying to get the return value of a stored procedure with .... DbCommand command = connection.CreateCommand(); command.CommandText = "Exec some_SP"; command.CommandType =...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.