Click here to Skip to main content
15,901,122 members
Home / Discussions / C#
   

C#

 
GeneralSQL query in c# Pin
Yevgeny Efter29-Mar-08 6:30
Yevgeny Efter29-Mar-08 6:30 
GeneralRe: SQL query in c# Pin
Giorgi Dalakishvili29-Mar-08 7:12
mentorGiorgi Dalakishvili29-Mar-08 7:12 
GeneralRe: SQL query in c# Pin
Yevgeny Efter29-Mar-08 7:17
Yevgeny Efter29-Mar-08 7:17 
GeneralRe: SQL query in c# Pin
Giorgi Dalakishvili29-Mar-08 7:22
mentorGiorgi Dalakishvili29-Mar-08 7:22 
GeneralRe: SQL query in c# Pin
Yevgeny Efter29-Mar-08 7:31
Yevgeny Efter29-Mar-08 7:31 
GeneralRe: SQL query in c# Pin
User 665829-Mar-08 7:55
User 665829-Mar-08 7:55 
GeneralRe: SQL query in c# Pin
Yevgeny Efter29-Mar-08 8:13
Yevgeny Efter29-Mar-08 8:13 
GeneralRe: SQL query in c# Pin
Luis Alonso Ramos29-Mar-08 12:34
Luis Alonso Ramos29-Mar-08 12:34 
It's not so hard, but it depends on your query. Is it a regular query or a stored procedure? if it is a sotred procedure, do you use output parameters or a regular return value. Here is some sample code:
using(SqlConnection conn = new SqlConnection(connString))
{
    conn.Open();
 
    //
    // Regular query - returns a bit value from a table
    //
 
    string sql = "SELECT BitColumn FROM Table WHERE Name = 'Luis'";
    SqlCommand cmd = new SqlCommand(sql, conn);
    bool result = Convert.ToBoolean(cmd.ExecuteScalar());
 
    //
    // Stored procedure - returns bit value in output parameter and in return value
    //
 
    cmd = new SqlCommand("sp_Test", conn);
    cmd.Parameters.Add("@BoolVal", SqlDbType.Boolean).Direction = ParameterDirection.Output;
    cmd.Parameters.Add("RetVal", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
    cmd.ExecuteNonQuery();

    int result1 = Convert.ToInt32(cmd.Parameters["RetVal"].Value);
    bool result2 = Convert.ToBoolean(cmd.Parameters["@BoolVal"].Value);
}
For the second example, you will need a stored procedure like this:
CREATE PROCEDURE sp_Test (@BoolVal bit OUT) AS
BEGIN
    -- Return in output parameter
    SELECT @BoolVal = BitColumn FROM Table WHERE NAME = 'Luis';
 
    -- Return value of the stored procedure
    IF @BoolVal IS NULL
        RETURN 0
    ELSE
        RETURN 1
END
When you execute a query, it returns a resultset (a table with data). A stored procedure that SELECTs something and doesn't get it into a variable is returning a resultset. ExecuteReader and data adapters get the whole resultset, but ExecuteScalar gets the data in the first column of the first row of the first table (useful for returning one value).

The stored procedure example calls ExecuteNonQuery because no resultset is returned. Output values are either an output parameter or the return value. You access both through the Commands collection of the SqlCommand object.
I hope this helps!


Luis Alonso Ramos
Intelectix
Chihuahua, Mexico




My Blog!



GeneralRe: SQL query in c# Pin
Yevgeny Efter30-Mar-08 0:13
Yevgeny Efter30-Mar-08 0:13 
GeneralRe: SQL query in c# Pin
Luis Alonso Ramos30-Mar-08 1:04
Luis Alonso Ramos30-Mar-08 1:04 
GeneralRe: SQL query in c# Pin
Yevgeny Efter30-Mar-08 6:00
Yevgeny Efter30-Mar-08 6:00 
GeneralMicrosoft Windows Software Development Kit Pin
BlitzPackage29-Mar-08 4:47
BlitzPackage29-Mar-08 4:47 
GeneralRe: Microsoft Windows Software Development Kit Pin
Colin Angus Mackay29-Mar-08 5:33
Colin Angus Mackay29-Mar-08 5:33 
Generalmodeling tool OnPaint Pin
Gareth H29-Mar-08 4:15
Gareth H29-Mar-08 4:15 
GeneralRe: modeling tool OnPaint Pin
Colin Angus Mackay29-Mar-08 5:37
Colin Angus Mackay29-Mar-08 5:37 
GeneralRe: modeling tool OnPaint Pin
Gareth H29-Mar-08 5:45
Gareth H29-Mar-08 5:45 
GeneralRe: modeling tool OnPaint Pin
Colin Angus Mackay29-Mar-08 5:56
Colin Angus Mackay29-Mar-08 5:56 
General.NET Application to sent a FAX Pin
Member 388976029-Mar-08 4:13
Member 388976029-Mar-08 4:13 
GeneralRe: .NET Application to sent a FAX Pin
Gareth H29-Mar-08 4:19
Gareth H29-Mar-08 4:19 
GeneralDatabase connection problem Pin
wsamuel29-Mar-08 4:05
wsamuel29-Mar-08 4:05 
GeneralRe: Database connection problem Pin
Gareth H29-Mar-08 4:18
Gareth H29-Mar-08 4:18 
GeneralRe: Database connection problem Pin
wsamuel29-Mar-08 5:23
wsamuel29-Mar-08 5:23 
GeneralRe: Database connection problem Pin
Gareth H29-Mar-08 5:50
Gareth H29-Mar-08 5:50 
GeneralRe: Database connection problem Pin
wsamuel29-Mar-08 16:36
wsamuel29-Mar-08 16:36 
GeneralRe: Database connection problem Pin
nelo_29-Mar-08 13:10
nelo_29-Mar-08 13:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.