ADO简介

ADO简介ADO(ActiveXDataObject)ADO对象模型:Connection:连接对象,用于创建数据源连接。Command:命令对象,用于执行动作查询,如查询更新等。Recordset:记录集对象,保存来自基本表或命令对象返回的结果,来操作查看查询结果。Field:字段对象,…

大家好,欢迎来到IT知识分享网。

ADO(ActiveX Data Object)

ADO简介

ADO对象模型:

  Connection:连接对象,用于创建数据源连接。

  Command:命令对象,用于执行动作查询,如查询更新等。

  Recordset:记录集对象,保存来自基本表或命令对象返回的结果,来操作查看查询结果。

  Field:字段对象,依赖于记录集对象使用。可使用Fields集合获取记录集中的每个字段的信息。

  Parameter:参数对象,依赖于命令对象使用,用于为参数查询提供数据。同时使用参数对象和命令对象,可使数据库对查询进行预编译,从而提高速度。

  Property:属性对象,每个联接对象、命令对象、记录集对象以及字段对象都有一个属性对象集合。使用属性对象可以访问特定对象的主要信息。

  Error:错误对象,依赖联接对象使用。

 

ADO简介

 

 

关系:

  用Connection建立与服务器的连接,然后用Command对象执行命令,如查询,更新等。用Recordset对象来操作和查看查询结果。

 Parameters集合和Parameters对象中的是Command对象的参数信息。在发生错误后,在Connection的Error集合和Error对象提供了错误信息。得到查询结果后,Filed集合和Filed对象就能通过Recordset对象使用啦,它们提供了相关的字段信息。

Connection对象包含了初始和建立连接的机制,同时也包含了执行查询、使用事务等。在缺省情况下,使用的是MS提供的ODBC驱动。

 

实例:

//给你部分类:
using System; using System.Data; using System.Data.SqlClient; using System.Collections.Generic; namespace Channels { /// <summary>
    /// 数据库操作参数列表类 /// </summary>
    public class SqlParameterList { public List<SqlParameter> plist = new List<SqlParameter>(); /// <summary>
        /// 把参数添加到参数列表 /// </summary>
        /// <param name="sp">Sql参数</param>
        public void AddAParameter(string name, SqlDbType type, int len, object value, bool output) { SqlParameter sp = new SqlParameter(name, type, len); if (len > 0) sp.Size = len; sp.Value = value; if (output) sp.Direction = ParameterDirection.Output; plist.Add(sp); } public void AddAParameter(string name, SqlDbType type, object value) { AddAParameter(name, type, 0, value, false); } public void AddAParameter(string name, SqlDbType type, int len, object value) { AddAParameter(name, type, len, value, false); } public void AddAParameter(string name, SqlDbType type, object value, bool output) { AddAParameter(name, type, 0, value, output); } /// <summary>
        /// 添加新Sql参数 /// </summary>
        /// <param name="name">参数名</param>
        /// <param name="type">参数类性</param>
        /// <param name="obj">参数值</param>
        /// <returns>Sql参数</returns>
        public static SqlParameter NewSqlParameter(string name, SqlDbType type, object obj) { SqlParameter sp = new SqlParameter(name, type); sp.Value = obj; return sp; } } } //数据库操作类
using System; using System.Data; using System.Xml; using System.Data.SqlClient; namespace Channels { /// <summary>
    /// 数据库操作类 /// </summary>
    public class DataAccessor { //private static string connString = System.Configuration.ConfigurationManager.ConnectionStrings["DoorControlService"].ConnectionString;
        private static string connString = ConfigFile.GetConnectionStringByName("ChannelControlService"); /// <summary>
        /// 添加参数到Sql语句或存储过程 /// </summary>
        /// <param name="com">SqlCommand对象</param>
        /// <param name="splist">参数列表</param>
        private static void AddParameters(SqlCommand com, SqlParameterList splist) { if (splist != null && splist.plist != null) foreach (SqlParameter sp in splist.plist) { com.Parameters.Add(sp); } } /// <summary>
        /// 仅执行设置好的command命令,无返回值 /// </summary>
        /// <param name="sql">Sql语句或存储过程</param>
        /// <param name="IsProcedure">是否存储过程</param>
        /// <param name="splist">参数列表,无参数输入null</param>
        /// <returns>返回受影响的行数</returns>
        public static int ExecuteSqlOnly(string sql, bool IsProcedure, SqlParameterList splist) { SqlConnection con = new SqlConnection(connString); SqlCommand com = new SqlCommand(sql, con); if (IsProcedure) { com.CommandType = CommandType.StoredProcedure; } if (splist != null) { AddParameters(com, splist); } if (con.State == ConnectionState.Closed) { con.Open(); } int result = -101; try { result = com.ExecuteNonQuery(); con.Close(); } catch { con.Close(); } return result; } /// <summary>
        /// 获取执行后的结果,即结果集第一行的第一列 /// </summary>
        /// <param name="sql">Sql语句或存储过程</param>
        /// <param name="IsProcedure">是否存储过程</param>
        /// <param name="splist">参数列表,无参数输入null</param>
        /// <returns>返回执行后的结果集中的第一行的第一列</returns>
        public static object GetExecuteSqlResult(string sql, bool IsProcedure, SqlParameterList splist) { SqlConnection con = new SqlConnection(connString); SqlCommand com = new SqlCommand(sql, con); if (IsProcedure) { com.CommandType = CommandType.StoredProcedure; } if (splist != null) { AddParameters(com, splist); } object result = null; if (con.State == ConnectionState.Closed) { con.Open(); } try { result = com.ExecuteScalar(); con.Close(); } catch (Exception ex) { con.Close(); throw ex; } return result; } /// <summary>
        /// 获取查询后的结果集 /// </summary>
        /// <param name="sql">Sql语句或存储过程</param>
        /// <param name="IsProcedure">是否存储过程</param>
        /// <param name="splist">参数列表,无参数输入null</param>
        /// <returns>查询的DataSet结果数据集</returns>
        public static DataSet GetExcuteDataSet(string sql, bool IsProcedure, SqlParameterList splist) { SqlConnection con = new SqlConnection(connString); SqlCommand com = new SqlCommand(sql, con); SqlDataAdapter sda = new SqlDataAdapter(com); if (IsProcedure) { com.CommandType = CommandType.StoredProcedure; } if (splist != null) { AddParameters(com, splist); } DataSet ds = new DataSet(); if (con.State == ConnectionState.Closed) { con.Open(); } try { sda.Fill(ds); con.Close(); } catch (Exception ex) { con.Close(); throw ex; } return ds; } } }

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/32496.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信