Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Test");
string fname = "zhuobin";
string lname = "bin";
SqlParameter firstName;
SqlParameter lastName;
SqlConnection conn = new SqlConnection("Data source=zhuobin;uid=sa;pwd=zhuobin;database=northwind");
string sqlqry = @"select count(*) from employees";
string sqlInsert = @"insert into employees(firstname,lastname)values(@fname,@lname)";
string sqldel = @"delete from employees where firstname=@fname and lastname=@lname";
SqlCommand cmdQry = new SqlCommand(sqlqry,conn);
SqlCommand cmdNon = new SqlCommand(sqlInsert,conn);
try
{
conn.Open();
Console.WriteLine("Before insert the number of tthe employees is {0}",cmdQry.ExecuteScalar());
firstName = new SqlParameter();
firstName.ParameterName = "@fname";
firstName.SqlDbType = SqlDbType.VarChar;
firstName.Size = 10;
firstName.Value = fname;
lastName = new SqlParameter();
lastName.ParameterName = "@lname";
lastName.SqlDbType = SqlDbType.VarChar;
lastName.Size = 10;
lastName.Value = lname;
cmdNon.Parameters.Add(firstName);
cmdNon.Parameters.Add(lastName);
// cmdNon.Parameters["fname"].Value = fname;
// cmdNon.Parameters["lname"].Value = lname;
Console.WriteLine("Execute statement:{0}",cmdNon.CommandText);
cmdNon.ExecuteNonQuery();
Console.WriteLine("After the insert the number of the employees is {0}",cmdQry.ExecuteScalar().ToString());
cmdNon.CommandText=sqldel;
Console.WriteLine("execute statement :{0}",cmdNon.CommandText);
cmdNon.ExecuteNonQuery();
Console.WriteLine("After the delete ,the number of the employees is {0}",cmdQry.ExecuteScalar().ToString());
}
catch (SqlException ex)
{
Console.WriteLine("Error:" + ex.Message);
}
finally
{
conn.Close();
}
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Test");
string fname = "zhuobin";
string lname = "bin";
SqlParameter firstName;
SqlParameter lastName;
SqlConnection conn = new SqlConnection("Data source=zhuobin;uid=sa;pwd=zhuobin;database=northwind");
string sqlqry = @"select count(*) from employees";
string sqlInsert = @"insert into employees(firstname,lastname)values(@fname,@lname)";
string sqldel = @"delete from employees where firstname=@fname and lastname=@lname";
SqlCommand cmdQry = new SqlCommand(sqlqry,conn);
SqlCommand cmdNon = new SqlCommand(sqlInsert,conn);
try
{
conn.Open();
Console.WriteLine("Before insert the number of tthe employees is {0}",cmdQry.ExecuteScalar());
firstName = new SqlParameter();
firstName.ParameterName = "@fname";
firstName.SqlDbType = SqlDbType.VarChar;
firstName.Size = 10;
firstName.Value = fname;
lastName = new SqlParameter();
lastName.ParameterName = "@lname";
lastName.SqlDbType = SqlDbType.VarChar;
lastName.Size = 10;
lastName.Value = lname;
cmdNon.Parameters.Add(firstName);
cmdNon.Parameters.Add(lastName);
// cmdNon.Parameters["fname"].Value = fname;
// cmdNon.Parameters["lname"].Value = lname;
Console.WriteLine("Execute statement:{0}",cmdNon.CommandText);
cmdNon.ExecuteNonQuery();
Console.WriteLine("After the insert the number of the employees is {0}",cmdQry.ExecuteScalar().ToString());
cmdNon.CommandText=sqldel;
Console.WriteLine("execute statement :{0}",cmdNon.CommandText);
cmdNon.ExecuteNonQuery();
Console.WriteLine("After the delete ,the number of the employees is {0}",cmdQry.ExecuteScalar().ToString());
}
catch (SqlException ex)
{
Console.WriteLine("Error:" + ex.Message);
}
finally
{
conn.Close();
}
Console.ReadLine();
}
}
}