在使用ADO.NET中的连接数据库:Connection对象连接上数据库之后,我们下面所做的就是对数据库中的数据进行操作。在ADO.NET中,是通过执行SQL语句:Command对象来对数据进行操作

下面的所有的例子均使用下面的数据库:

Command对象概述

Command对象最主要的工作是通过Connection对象对数据源下达操作数据库的命令。Command对象有许多属性,但是常用的属性为:

属性

说明

ActiveConnection 设定要透过哪个连接对象下命令
CommandBehavior 设定Command对象的动作模式
CommandType(Text\TableDirect\StoredProcedure) 命令类型(SQL语句,完整表达句,存储过程)
CommandText 要下达至数据源的命令
CommandTimeout 出错时等待时间
Parameters 参数集合
RccordsAffected 受影响的记录数

例一,创建一个SqlCommand对象comText,并设置出错时等待时间为2秒。完整的代码为:

<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;//引入命名空间
namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            string conStr = "server=.;user=sa;pwd=123456;database=CustomerManagement";//连接字符串
            SqlConnection conText = new SqlConnection(conStr);//创建Connection对象
                conText.Open();//打开数据库
                string sql = "select * from manager";//创建统计语句
                SqlCommand comText = new SqlCommand(sql, conText);//创建Command对象
                comText.CommandTimeout = 2;//设置等待时间
                Console.WriteLine("创建Command成功");
            catch (Exception ex)//创建检查Exception对象
                Console.WriteLine(ex.Message.ToString());//输出错误信息
            finally
                conText.Close();//关闭连接
            Console.ReadLine();
</span>

运行结果为:

执行SQL语句

定义好命令后就应当执行命令,Command对象执行命令提供了三种方法。

方法 返回值描述
ExecuteNonQuery() 不返回任何结果
ExecuteReader() 返回一个IDataReader
ExecuteScalar() 返回一个值

1,ExecuteNonQuery方法

这个方法没有返回任何结果,所以一般用于updata,delete,insert语句中。

例二,通过ExecuteNonQuery方法更改CustomerManagement数据库中的manager数据表中id为1的用户的密码。完整的代码为:

<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;//引入命名空间
namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            string conStr = "server=.;user=sa;pwd=123456;database=CustomerManagement";//连接字符串
            SqlConnection conText = new SqlConnection(conStr);//创建Connection对象
                conText.Open();//打开数据库
                string sql = "update manager set userPwd=123456789 where id=1";//创建修改语句
                SqlCommand comText = new SqlCommand(sql, conText);//创建Command对象
                comText.ExecuteNonQuery();//执行命令
                Console.WriteLine("更改成功");
            catch (Exception ex)//创建检查Exception对象
                Console.WriteLine(ex.Message.ToString());//输出错误信息
            finally
                conText.Close();//关闭连接
            Console.ReadLine();
</span>

运行时出现这样的结果:

2,ExecuteScalar方法

这个方法返回一个值,一般用于只返回一个值的语句,如求数据统计的count语句,求最大数Max或最小数Min语句等。

例三,通过Command对象的ExecuteScalar方法统计CustomerManagement数据库中的manager数据表中的所有记录系数。完整的代码为:

<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;//引入命名空间
namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            string conStr = "server=.;user=sa;pwd=123456;database=CustomerManagement";//连接字符串
            SqlConnection conText = new SqlConnection(conStr);//创建Connection对象
                conText.Open();//打开数据库
                string sql = "select count(*) from manager";//创建统计语句
                SqlCommand comText = new SqlCommand(sql, conText);//创建Command对象
                int t = 0;
                t = (int)comText.ExecuteScalar();//执行查询并将值返回给t
                Console.WriteLine("manager数据表中的记录系数为:"+t.ToString());
            catch (Exception ex)//创建检查Exception对象
                Console.WriteLine(ex.Message.ToString());//输出错误信息
            finally
                conText.Close();//关闭连接
            Console.ReadLine();
}</span>

运行的结果为:

01 Command 对象 概述 Command 对象 是一个数据 命令 对象 ,主要功能是向数据库发送查询、更新、删除、修改操作的 SQL 语句。 Command 对象 主要有以下几种方式。 Sql C... Sql Command 这个 对象 可以让我们在数据库上做一下操作,比如说增、删、改、查。都可以使用 Sql Command 这个 对象 。 首先,要使用 Sql Command 对象 的话,必须先声明它。 Sql Command cmd = new Sql Command (str SQL , conn); 其中str SQL 是我们定义好的 SQL 语句,conn 是声明好的数据库链接。 我们来看一下如果需要在... Command 对象 需要取得将要 执行 SQL 语句,通过调用该类提供的多种方法,向数据库提交 SQL语句 。下面详细介绍 Sql Command 对象 中的几种 执行 SQL 语句的方法。01 ... 开发工具:Visual Studio 2012数据库: SQL Server 2012使用Visual Studio时还是直接和微软自家的 SQL Server 数据库连接比较方便,就像使用Eclipse时和My SQL 连接便捷一样的道理无论使用什么工具步骤都一样:首先保证相关工具都已经正确安装了 开启数据库连接服务 在开发工具中通过用户名和口令与数据库进行关联 执行 SQL语句 关闭相关连接和服务连接 DataTable dt=new DataTable(); //DataRow dr; string connString = " server =127.0.0.1;uid=sa;pwd=;dat... 有些时候我们需要在程序中编写读取 sql 脚本文件并 执行 这些 sql语句 ,但是我们在有些时候会遇到读出来的 sql语句 不能 执行 ,其实不能 执行 并不是你的 sql 脚本文件有错误,而是去 执行 sql语句 的时候,而是 C# 代码里面 执行 sql语句 的代码对 sql 里面的一些标志字符不识别罢了,下面有两种方法可以实现读取 sql 文件并 执行 :   第一种方法是将 sql 脚本文件中的 sql语句 全部读取出来,然后使用 C# 代码去 执行 sq 原文书籍:《 C# 从入门到精通》 Command 对象 是一个数据 命令 对象 ,主要功能是向数据库发送查询、更新、删除、修改操作的 SQL语句 。1、设置数据源类型 Command 对象 有3个重要的属性: 1、Connection 2、 Command Text 3、 Command TypeConnection属性用于设置 Sql Command 使用的 Sql Connection。 Command Text属性用于设置要对数据源 在ADO.NET中,查询的一般过程是创建实现IDbConnection接口的连接 对象 登录 到相应数据库,再创建实现IDb Command 接口的 命令 对象 执行 SQL 命令 或存储过程。如果 SQL语句 有返回结果,则可以整表的方式接收数据或以逐行推进的方式遍历各条记录。在参数化查询中,创建 命令 对象 后,为其指定 SQL语句 (或存储过程),然后创建 SQL语句 包含的各个参数,给参数赋值后再 执行 命令 对象 。与 命令 对象 关 3 using System.Data. Sql Client; 4 using System.Text; 5 using System.Text.RegularExpressions; 7 namespace CustomExtensions 9 p... 使用 命令 对象 命令 对象 代表一个 命令 (例如,一个 SQL 查询或一个 SQL 存储过程)。用连接 对象 的Execute方法和记录集 对象 的Open方法可以用来 执行 命令 字符串。考虑下面这两个例子:         RS.Open "SELECT * FROM Mytable",MyConn        MyConn.Execute "UPDATE Mytable SET Mycolumn=He Entity Framework是微软出品的高级ORM框架,大多数.NET开发者对这个ORM框架应该不会陌生。本文主要罗列在.NET(ASP.NET/WINFORM)应用程序开发中使用Entity Framework直接 执行 SQL语句 或者存储过程的一些代码片段。具体请见以下正文: 1.使用 Sql Query在已知的实体上 执行 SQL 查询语句 using (var context = new M...