C#之执行SQL语句:Command对象

       在使用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对象

            try
            {
                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对象

            try
            {
                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>

        运行时出现这样的结果:

 

        编译时并没有出现错误,仔细检查后才知道是update语句拼写错误,就因为一个小小的拼写错误,费了半天劲才找到,把updata改成update即可,再运行的结果为:

 

        在CustomerManagement数据库中的manager数据表的内容已更改:

 

        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对象

            try
            {
                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>

        运行的结果为:

 

        CustomerManagement数据库中的manager数据表中的记录系数为:

 

            3,ExecuteReader方法

        ExecuteReader方法返回一个DataReader对象,可用于迭代返回记录,有关DataReader独享的内容将会在《C#之读取数据:DataReader对象》详细介绍和应用。


 

 

 

        

 


 

 

 

 

 

       

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值