Executing a Query That Does Not Return a Result Set : SqlCommand Select « ADO.Net « C# / CSharp Tutorial






/*
CREATE TABLE MyTable(
    Id int NOT NULL PRIMARY KEY,
    Field1 nvarchar(50) NOT NULL,
    Field2 nvarchar(50) NOT NULL,
)

INSERT INTO MyTable VALUES (1, 'Field 1.1', 'Field 2.1');
INSERT INTO MyTable VALUES (2, 'Field 1.2', 'Field 2.2');
INSERT INTO MyTable VALUES (3, 'Field 1.3', 'Field 2.3');
*/

using System;
using System.Data;
using System.Data.SqlClient;

    class Program
    {
        static void Main(string[] args)
        {
            string sqlConnectString = "Data Source=(local);Integrated security=SSPI;Initial Catalog=MyDatabase;";

            string sqlDelete = "DELETE FROM MyTable WHERE Id = 2";

            SqlConnection connection = new SqlConnection(sqlConnectString);
            SqlCommand command = new SqlCommand(sqlDelete, connection);
            connection.Open( );
            int rowsAffected = command.ExecuteNonQuery( );
            Console.WriteLine("{0} row(s) affected.", rowsAffected);
            Console.WriteLine("Record with Id = 2 deleted.");
            connection.Close( );
        }
    }








32.21.SqlCommand Select
32.21.1.Simple Query
32.21.2.How to perform a SELECT statement using ADO.NET
32.21.3.Execute Scalar Example
32.21.4.Executing a Query That Does Not Return a Result Set
32.21.5.Executing a Query That Returns Multiple Result Sets with DataSet
32.21.6.Executing a Query That Returns Multiple Result Sets with SqlDataReader
32.21.7.Execute the command to get Scalar value
32.21.8.CommandReader
32.21.9.Get table list