Check return of sql command in c#

I want to check if my query returns any value or not and write the remaining logic accordingly.

SqlCommand myCommand = new SqlCommand("Select * from phc.userbase where [user]='@Username' and [password]='@password'", myConnection);

I want to know this command returns null or not. I tried

myReader = myCommand.ExecuteReader(); bool rd = myReader.Read(); if rd==false

but I can't get it working. Any ideas?

Here are my parameters:

SqlParameter myParam = new SqlParameter("@Username", SqlDbType.VarChar, 25); myParam.Value = usr; SqlParameter myParam2 = new SqlParameter("@password", SqlDbType.VarChar, 25); myParam2.Value = pass;

-------------Problems Reply------------

It depends what you mean by null here; do you mean "no rows" ? If so:

using(var myReader = myCommand.ExecuteReader()) {
if(myReader.Read()) {
// at least 1 row; can now check columns
} else {
// no rows

To be honest, though, you could probably change that to SELECT 1 ... and use ExecuteScalar - much less bother.

In this case, though, it is probably easier to just SELECT 1 of query... and use:

object value = myCommand.ExecuteScalar();
if(value != null) { ... got a row ...}

use SqlDataReader.HasRows()

if (myReader.HasRows())
// Do something

Category:c# Views:0 Time:2012-03-15

Related post

  • Nagios oracle check ORA-00933: SQL command not properly ended 2011-11-16

    I am attempting to using one of many checks that are designed to check oracle db. I chose this one because it seemed pretty flexible as you are able to feed some SQL statements into it with a flag. Here's the page for the plugin I am trying to use: h

  • What is the SQL command to return the field names of a table? 2008-09-19

    Say I have a table called myTable. What is the SQL command to return all of the field names of this table? If the answer is database specific then I need SQL Server right now but would be interested in seeing the solution for other database systems a

  • Using the SQL Command object, how can you check to see if the result set is empty? 2009-04-06

    Using the ExecuteScalar method in the SQL Command object, how can you check to see if the result set is empty? I am using, C#, and MS SQL 2008. Right now when I run the following code the Response.Write returns a 0 when the resultset is empty

  • How to execute sql command and read its return value when the column has no name 2011-12-27

    I've been using SqlDataReader to read results from my querys to an SQL server db. SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { Console.WriteLine(rdr["ColumnName"].ToString()); } But now I want to create a Job in my C# program, and wh

  • The new command text returns data with schema different from the schema of the main query. Check your query's command text if this is not desired 2012-01-19

    The new command text returns data with schema different from the schema of the main query. Check your query's command text if this is not desired. I am getings this warning, when ever i add a query in a Strongly Typed DataSet which returns a single c

  • SQL: how to know if a field has "Allow nulls" checked or not checked by SQL command 2009-10-14

    I want to know which table's field are required or not required so I have to get "Allow nulls" state. How to do that? --------------Solutions------------- I will assume you are talking about SQL Server. There is a table, INFORMATION_SCHEMA.COLUMNS, t

  • Checking for an SQL result in VB.NET 2009-12-02

    I need to check if my SQL statement returned any results from the Access db I'm using to store my data. I have this code atm: cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\Computing Project\database.mdb;") cn.Open() cmd =

  • SSIS SQL Command datetime parameter with timezone offset 2011-02-16

    I am designing a SSIS package to transform data from SQL DB to another SQL DB. I'm using OLE DB source and have Query written as SQL Command. I'm passing a datetime parameter which will be read via SSIS variable from XML configuration file. In Config

  • How do I check for the SQL Server Version using Powershell? 2011-09-28

    What's the easiest way to check for the SQL Server Edition and Version using powershell? --------------Solutions------------- Invoke-Sqlcmd -Query "SELECT @@VERSION;" -QueryTimeout 3 [reflection.a

  • Search "[" or "]" inside a table using SQL command 2011-11-28

    In an SQL table I have following columns id, title, content And I would like to search text inside title or content having "[" or "]". What will be the SQL command to fetch such kind of data? (edit: removed "efficient") (added) As per "rsbarro" I hav

  • How to get the Entity-SQL command text by LINQ query? 2012-02-10

    It's an exercise of EF code-first. There's a simple method. I want to get the Entity SQL command text generated by object services. (MyDbContext is derived form DbContext. Person is a POCO class.) using (MyDbContext context = new MyDbContext()) { var

  • Improving code readability for SQL commands 2008-10-03

    In one of my classes for a web application I am developing, I have some reasonably long SQL queries. When developing a three-tier application, what are the best practices to make this kind of code neater? Dim dc As New SqlCommand("INSERT INTO Choices

  • How do I get the SQL command text when I insert a new record with Linq? 2009-02-11

    var newUser = new tblUser() { Email = strEmail, Password = strPassword, DateBirth = DateTime.Parse(strDateBirth), }; db.tblUsers.InsertOnSubmit(newUser); db.SubmitChanges(); I want to get the actual sql command text that linq generated. -------------

  • SQL commands to get performance statistics 2009-02-17

    Are there SQL commands that I could use to extract performance monitoring data from MS SQL 2005, such as: transactions per second page reads/writes connections (@@CONNECTIONS gives the total, but what about current) physical reads locks and blocks ot

  • retrieve most recently executed SQL command (T-SQL) 2009-07-28

    One of my developers working on a trigger-based logging facility in SQL Server 2008 asked me if there was a command to retrieve the most recently executed SQL command within T-SQL. I thought there was a system stored procedure for just such a functio

  • SQL command not changing data 2009-08-06

    I'm using the SQL command below to update a column called Address, the value contains line feeds and I want to replace them with a semi-colon. In Query Analayzer the preview looks perfect but when I check the data in Enterprise Manager (MS SQL 2000)

  • SQL command for PROGRESS database 2009-10-06

    Please bear with me new to SQL- I am trying to write an SQL command with a join in a PROGRESS db. I would like to then select only the first matching record from the join. I thought to use LIMIT but PROGRESS does not support that. MIN or TOP would al

  • Why does the balance become 1.00 whenever the sql command below is executed no matter what value of $cost is? 2009-11-20

    Why does the balance become 1.00 whenever the sql command below is executed no matter what value of $cost is? update account set balance=balance-'$cost' and username='steven'" The value of balance is bigger than $cost initially, after the execution,

  • How can I monitor the SQL commands send over my ADO connection? 2010-01-14

    i need intercept all the SQL commands that pass between an ADO connection component and a database server. something like the TSQLmonitor of dbExpress, but for ADO . Anybody know any third-party component that implements this functionality? UPDATE I

Copyright (C), All Rights Reserved.

processed in 0.109 (s). 11 q(s)