![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection(@"Data source=s013;Database=Northwind;integrated security=true");
//define the query string
string sqlqry = @"select count(*) from employees";
//define the insert into string
string sqlins = @"insert into employees(firstname,lastname)values('zhuo','bin')";
//define the delete string
string sqldel = @"delete from employees where firstname='zhuo' an lastname'bin'";
//
SqlCommand cmdQry = new SqlCommand(sqlqry,conn);
SqlCommand cmdIns = new SqlCommand(sqlins,conn);
try
{
conn.Open();
Console.WriteLine("Before insert ,the number of the employees {0}\n",cmdQry.ExecuteScalar().ToString());
Console.WriteLine("Execute Statement: {0}",cmdIns.CommandText);
cmdIns.ExecuteNonQuery();
Console.WriteLine("After insert ,the number of the employees is {0}",cmdQry.ExecuteScalar().ToString());
cmdIns.CommandText = sqldel;
cmdIns.ExecuteNonQuery();
Console.WriteLine("Execute Statement: {0}", cmdIns.CommandText);
Console.WriteLine("After delete :{0}",cmdQry.ExecuteScalar().ToString());
}
catch (SqlException ex)
{
Console.WriteLine("The error is :" + ex.Message);
}
finally
{
conn.Close();
}
Console.ReadLine();
}
}
}