using System.Data;
using System.Data.SqlClient;
namespace ExecuteNonQueryExample
{
class Program
{
static void Main(string[] args)
{
SqlConnection thisConnection = new SqlConnection(
@"Data Source=scott;Initial Catalog=northwind;Persist Security Info=True;User ID=sa;Password=sa123");
thisConnection.Open();
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "update products set unitPrice=unitprice*1.05 where SupplierId=12";
//Inset,Update,Delelte的数据修改操作也不返回任何数据,我们对这些命令感兴趣的是修改操作影响的行数,可以用ExecuteNonQuery()方法
int rowsAffected = thisCommand.ExecuteNonQuery();
Console.WriteLine("Rows Updated={0}", rowsAffected);
thisConnection.Close();
Console.ReadLine();
}
}
}
using System.Data.SqlClient;
namespace ExecuteNonQueryExample
{
class Program
{
static void Main(string[] args)
{
SqlConnection thisConnection = new SqlConnection(
@"Data Source=scott;Initial Catalog=northwind;Persist Security Info=True;User ID=sa;Password=sa123");
thisConnection.Open();
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "update products set unitPrice=unitprice*1.05 where SupplierId=12";
//Inset,Update,Delelte的数据修改操作也不返回任何数据,我们对这些命令感兴趣的是修改操作影响的行数,可以用ExecuteNonQuery()方法
int rowsAffected = thisCommand.ExecuteNonQuery();
Console.WriteLine("Rows Updated={0}", rowsAffected);
thisConnection.Close();
Console.ReadLine();
}
}
}
结果:
Rows Updated=5