/* ------------------------ My Meta Content Here SEO ------------------------ */

Pages

Main Menu

Saturday, May 4, 2019

ADO.NET

How to Create and Execute SqlCommand in ADO.NET


ADO.NET, Data Access, .NET Development

Stored Procedures, sql commond

How to Create and Execute SqlCommand in ADO.NET

Introduction

We can create and execute different types of SqlCommand. In this applilcation, we will demonstrate how to create and execute SqlCommand:

1. Create different types of SqlCommand;
2. Execute SqlCommand in different ways;
3. Display the result.

Using the Code

1. ExecuteNonQuery method
If you need modify the data (Add, Delete, Update), you can use the method to complete it.

C#

public static Int32 ExecuteNonQuery(String connectionString, String commandText,  
    CommandType commandType, params SqlParameter[] parameters) 
{ 
    using (SqlConnection conn = new SqlConnection(connectionString)) 
    { 
        using (SqlCommand cmd = new SqlCommand(commandText, conn)) 
        { 
            cmd.CommandType = commandType; 
            cmd.Parameters.AddRange(parameters); 
            conn.Open(); 
            return cmd.ExecuteNonQuery(); 
        } 
    } 
} 

2. ExecuteScalar method
If you only need one value (first column and first row), you can use the method to get the value, such as the statistical value.

C#

public static Object ExecuteScalar(String connectionString, String commandText, 
    CommandType commandType, params SqlParameter[] parameters) 
{ 
    using (SqlConnection conn = new SqlConnection(connectionString)) 
    { 
        using (SqlCommand cmd = new SqlCommand(commandText, conn)) 
        { 
            cmd.CommandType = commandType; 
            cmd.Parameters.AddRange(parameters); 
            conn.Open(); 
            return cmd.ExecuteScalar(); 
        } 
    } 
} 

3. ExecuteReader method
When you need the details of the data, you can use this method to return the information.

C#

public static SqlDataReader ExecuteReader(String connectionString, String commandText,  
    CommandType commandType, params SqlParameter[] parameters) 
{ 
    SqlConnection conn = new SqlConnection(connectionString); 
    using (SqlCommand cmd = new SqlCommand(commandText, conn)) 
    { 
        cmd.CommandType = commandType; 
        cmd.Parameters.AddRange(parameters); 
        conn.Open(); 
        // When using CommandBehavior.CloseConnection, the connection will be closed when the // IDataReader is closed. 
        SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); 
        return reader; 
    } 
} 

4. Parameter
You can add several parameters to the command and set the properties of parameter, such as the value, the type and the direction. If the direction is set as Output, you can get the value of parameter after executing the command.

C#

// Specify the year of StartDate 
SqlParameter parameterYear = new SqlParameter("@Year", SqlDbType.Int); 
parameterYear.Value = year; 
SqlParameter parameterBudget = new SqlParameter("@BudgetSum", SqlDbType.Money); 
parameterBudget.Direction = ParameterDirection.Output; 

5. Command Type.
There're three command types: StoredProcedure, Text (Default), TableDirect. The TableDirect type is only for OLE DB.

C#

using (SqlDataReader reader = SqlHelper.ExecuteReader(connectionString, commandText,  
               CommandType.StoredProcedure, parameterYear, parameterBudget)) 

2 comments:

My Blog List

  • काश - काश मुझे भी पीने की आदत होती,मैं कब का मुर्दा हो गया होता। छुटकारा मिलता आज के आतंकवाद से, किसी संतान भूमि में सो गया होता। मेरा एतबार कौन करेगा, मैंने मुर...
    3 months ago
  • काश - काश मुझे भी पीने की आदत होती,मैं कब का मुर्दा हो गया होता। छुटकारा मिलता आज के आतंकवाद से, किसी शमशान भूमि में सो गया होता। मेरा एतबार कौन करेगा, मैंने मुर...
    3 months ago
  • Kumaon University Nainital B.Ed entrance exam test result 2012 - कुमाऊँ विश्वविधालय, नैनीताल (उत्तराखण्ड)
    10 years ago