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

Pages

Main Menu

Saturday, May 11, 2019

What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?

Static variable in C#? static variable value is shared among all instances of that class.

static variable shares the value of it among all instances of the class.
Example without declaring it static:
public class Variable
{
    public int i = 5;
    public void test()
    {
        i = i + 5;
        Console.WriteLine(i);
    }
}


public class Exercise
{
    static void Main()
    {
        Variable var = new Variable();
        var.test();
        Variable var1 = new Variable();
        var1.test();
        Console.ReadKey();
    }
}
Explanation: If you look at the above example, I just declare the int variable. When I run this code the output will be 10 and 10. Its simple.
Now let's look at the static variable here; I am declaring the variable as a static.
Example with static variable:
public class Variable
{
    public static int i = 5;
    public void test()
    {
        i = i + 5;
        Console.WriteLine(i);
    }
}


public class Exercise
{
    static void Main()
    {
        Variable var = new Variable();
        var.test();
        Variable var1 = new Variable();
        var1.test();
        Console.ReadKey();
    }
}
Now when I run above code, the output will be 10 and 15. So the static variable value is shared among all instances of that class.
Read More »

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)) 
Read More »

My Blog List