C#_ACCP_Windows_ADO.NET(二)

ADO.NET (二)     Command 对象

Command 对象可以对数据库执行增删改查的命令 建立数据库连接后就可以使用 Command 对象对数据库进行操作了
使用 Command 对象的操作步骤
首先创建 Connection 对象连接数据库
然后定义 SQL 语句
第三步创建 Command 对象
使用 Command 对象方法执行 SQL 命令
例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace ADO_Command
{
    class Program
    {
        static void Main(string[] args)
        {
            string conStr = "Data Source=.;Initial Catalog=Test;User Id=sa;Pwd=leeho";
            SqlConnection conn = new SqlConnection(conStr);

            //SQL查询语句
            String sqlStr="select count(1) from TestTable";

            //创建Command 对象 第一个参数是SQL语句 第二个参数是用于连接数据库的 Connection 对象
            SqlCommand comm=new SqlCommand(sqlStr,conn);
            int result=0;
            try
            {
                //必须先打开数据库连接才能执行SQL语句
                conn.Open();

                //Command 对象的 ExecuteScalar()方法返回单个值 返回类型为 Object 使用时需要强制转换
                result = (int)comm.ExecuteScalar();

            }
            catch (Exception ex)
            {
            }
            finally
            {
                conn.Close();
            }
            Console.WriteLine("TestTable 表中共有{0}条记录",result);
            Console.ReadKey();
        }
    }
}


Command 对象方法 ExecuteScalar() 返回单个值 类型为 Object
如果查询到多个值 取第一行第一列的值

例子

TestTable 表中记录
ID     Name     Sex      Age
1       张三        男      25
2       李四        男      26

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace ADOCommand2
{
    class Program
    {
        static void Main(string[] args)
        {
            string conStr = "Data Source=.;Initial Catalog=Test;User Id=sa;Pwd=leeho";
            SqlConnection conn = new SqlConnection(conStr);

            //查询所有ID列的值 有2个 值为1 和 2
            String sqlStr = "select ID from TestTable";

            SqlCommand comm = new SqlCommand(sqlStr, conn);
            int id = 0;
            try
            {
                conn.Open();

                id = (int)comm.ExecuteScalar();

            }
            catch (Exception ex)
            {
            }
            finally
            {
                conn.Close();
            }
            //输出1 
            Console.WriteLine("TestTable ID列的值有{0}", id);
            Console.ReadKey();
        }
    }
}

Command 对象属性和方法

CommandText //变更数据库操作字符串
ExecuteScalar() //执行查询命令 返回单个值
ExecuteNonQuery() //执行 Update Insert Delete 等不需要返回值的数据库操作 仅仅返回受影响的行数
ExecuteReader() //执行查询命令 返回 DataReader 对象

使用 Command 对象 ExecuteScalar() 和 ExecuteNonQuery() 方法操作数据库

例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace ADOCommand3
{
    class Program
    {
        static void Main(string[] args)
        {
            string conStr = "Data Source=.;Initial Catalog=Test;User Id=sa;Pwd=leeho";
            SqlConnection conn = new SqlConnection(conStr);

            //查询张三的年龄
            string sqlStr = "select Age from TestTable where Name='张三'";

            SqlCommand comm = new SqlCommand(sqlStr, conn);

            int age = 0;
            int newAge = 0;
            int result = 0;
            try
            {
                conn.Open();
                age = (int)comm.ExecuteScalar();
                Console.WriteLine("张三的年龄是{0}",age);

                string sqlStr2 = "update TestTable set Age=28 where Name='张三'";
                //变更comm的SQL执行语句
                comm.CommandText = sqlStr2;

                //ExecuteNonQuery() 执行不返回查询结果的语句 比如 Update Insert Delete 语句
                //ExecuteNonQuery() 仅仅返回受影响的行数
                result = (int)comm.ExecuteNonQuery();
                Console.WriteLine("{0}行受到影响",result);

                string sqlStr3 = "select Age from TestTable where Name='张三'";
                comm.CommandText = sqlStr3;

                newAge =(int)comm.ExecuteScalar();
                Console.WriteLine("现在张三的年龄为{0}岁", newAge);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                conn.Close();
            }
            Console.ReadKey();
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值