//数据库连接字段
SqlConnection thisConnection = new SqlConnection(@"Server=WIN-8SK1EA43KTP/MSSQL08R2;Integrated Security=True;" + "Database=db_office");
//打开数据库连接
thisConnection.Open();
//创建放sql语句的对象
SqlCommand thisCommand = thisConnection.CreateCommand();
//书写sql语句
thisCommand.CommandText = "select count(*) from email";
//创建一个对象,用来放查询返回的结果
object countResult = thisCommand.ExecuteScalar();
//打印出结果
Console.WriteLine("Count of Email={0}.",countResult); thisConnection.Close();
//关闭连接
Console.ReadKey();
累时insert update delete的数据库操作不返会任何数据;我们可以用ExecuteNonQuery()知道影像到的行数.
//数据库连接字段
SqlConnection thisConnection = new SqlConnection(@"Server=WIN-8SK1EA43KTP/MSSQL08R2;Integrated Security=True;" + "Database=db_office");
//打开数据库连接
thisConnection.Open();
//创建放sql语句的对象
SqlCommand thisCommand = thisConnection.CreateCommand();
//书写sql语句
thisCommand.CommandText = "update lianxi set lanmu=6 where id=1";
//创建一个对象,用来放查询返回的结果
int rowaffected = thisCommand.ExecuteNonQuery();
//打印出结果
Console.WriteLine("Count of Email={0}.",rowaffected);
//关闭连接
thisConnection.Close();