命令对象SqlCommand(四)执行语句
2012-07-18 00:46
267 查看
View Code
示例说明:3条SQL语句存放在3个字符串变量中,首先是查询行,然后插入行,最后初始化其CommandText属性,设置删除语句,删除插入的行。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace CommandNonQuery { class Program { static void Main(string[] args) { SqlConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=Northwind;Integrated Security=True"); string sqlqry = @"select count(*) from employees"; string sqlins=@"insert into employees(firstname,lastname) values('tan','ding')"; string sqldel = @"delete from employees where firstname='tan' and lastname='ding'"; SqlCommand cmdqry = new SqlCommand(sqlqry, conn); SqlCommand cmdnon = new SqlCommand(sqlins, conn); try { conn.Open(); Console.WriteLine("Before Insert:Number of employees {0}\n", cmdqry.ExecuteScalar()); Console.WriteLine("Executing statement {0}", cmdnon.CommandText); cmdnon.ExecuteNonQuery(); Console.WriteLine("After Insert:Number of employees {0}\n", cmdqry.ExecuteScalar()); cmdnon.CommandText = sqldel; Console.WriteLine("Executing statement {0}", cmdnon.CommandText); cmdnon.ExecuteNonQuery(); Console.WriteLine("After Delete:Number of employees {0}\n", cmdqry.ExecuteScalar()); } catch (SqlException ex) { Console.WriteLine(ex.ToString()); } finally { conn.Close(); Console.WriteLine("Connection closed."); } Console.ReadKey(); } } }
示例说明:3条SQL语句存放在3个字符串变量中,首先是查询行,然后插入行,最后初始化其CommandText属性,设置删除语句,删除插入的行。
相关文章推荐
- 命令对象SqlCommand(三)执行命令,返回多个结果
- C#从一个SqlCommand对象生成可执行的SQL语句
- 黑马程序员之ADO.NET学习笔记:通过SqlCommand对象执行SQL语句
- 命令对象SqlCommand(二)执行命令,返回单个记录
- 一次ORA-4030问题诊断及解决【解决思路不错,说明了对象的统计信息与优化器的优化操作(即选择执行一个SQL语句在该优化参数环境下最佳的执行计划)间的关系】
- sqlcommand对象 datareader对象的执行原理
- 查询Oracle正在执行的sql语句、当前的被锁对象
- hibernate执行sql语句后转换为实体对象
- 在C#中通过命令对象执行SQL
- 使用命令对象代替switch语句的写法示例
- ASP.NET基础教程-SqlCommand对象-用参数点位符代替SQL语句中的硬编码列值
- 假定CSomething是一个类,执行下面这些语句之后,内存里创建了____个CSomething对象。
- Mysql使用kill命令解决死锁问题(杀死某条正在执行的sql语句)
- 基于C#中的类SqlCommand对象调用SQLServer存储过程时,存储过程执行成功,但是对象方法ExecuteNonQuery()返回-1
- mySQL数据库Sql语句执行效率检查--Explain命令
- redis原理-对象以及命令解析与执行
- web 中关于清除session会话 中保存的对象的时候 执行删除语句操作
- mySQL数据库Sql语句执行效率检查--Explain命令
- 使用SqlCommand对象执行存储过程
- C#数据库入门-011:执行命令SqlCommand