Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

ado net execute sql query

class Program
{
    static void Main()
    {
        string connectionString =
            "Data Source=(local);Initial Catalog=Northwind;"
            + "Integrated Security=true";

        // Provide the query string with a parameter placeholder.
        string queryString =
            "SELECT ProductID, UnitPrice, ProductName from dbo.products "
                + "WHERE UnitPrice > @pricePoint "
                + "ORDER BY UnitPrice DESC;";

        // Specify the parameter value.
        int paramValue = 5;

        // Create and open the connection in a using block. This
        // ensures that all resources will be closed and disposed
        // when the code exits.
        using (SqlConnection connection =
            new SqlConnection(connectionString))
        {
            // Create the Command and Parameter objects.
            SqlCommand command = new SqlCommand(queryString, connection);
            command.Parameters.AddWithValue("@pricePoint", paramValue);

            // Open the connection in a try/catch block.
            // Create and execute the DataReader, writing the result
            // set to the console window.
            try
            {
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine("	{0}	{1}	{2}",
                        reader[0], reader[1], reader[2]);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# .net core entity framework one to many 
Csharp :: Block iFrames | How to Stop Your Website From Being iFramed 
Csharp :: sto playing audiosource 
Csharp :: c# enum get string value 
Csharp :: minimum of three numbers 
Csharp :: unity array c# 
Csharp :: c# clear linkList 
Csharp :: c# datagridview multiple row selection without control 
Csharp :: serial begin 
Csharp :: how to edit .csproj file 
Csharp :: c# unit test exception using try catch 
Csharp :: how to make rabbitmq start and stop base on c# services 
Csharp :: How to create a new object instance from a Type 
Csharp :: how prevent user remove file linux 
Csharp :: How to get selected item from Dropdown in GridView 
Csharp :: unity3d gameobject follow path 
Csharp :: c# move directory 
Csharp :: summernote dropdown plugin 
Csharp :: create class for database connection in c# 
Csharp :: c# WriteLine() 
Csharp :: reflection get enum value C# 
Csharp :: get xml from url 
Csharp :: array to object c# 
Csharp :: string length f# 
Csharp :: c# delete object 
Csharp :: shuffle array c# 
Csharp :: rename join ta le in many to many 
Csharp :: empty int array c# 
Csharp :: blazor editform empty 
Csharp :: actual vector3 object unity 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =