Example #1
0
        /// <summary>
        /// 使用离线数据库连接器,无需使用Open方法,执行命令并返回影响行数
        /// </summary>
        /// <param name="sql">命令</param>
        /// <param name="Parameters">参数</param>
        /// <returns></returns>
        public virtual int ExecuteNonQueryOffline(string sql, Dictionary <string, object> Parameters)
        {
            int ret = new int();

            try
            {
                this.ConnectionOffline.Open();
                this.CommandOffline.CommandType = CommandType.Text;
                this.CommandOffline.CommandText = sql;
                this.CommandOffline.Parameters.Clear();
                foreach (string key in Parameters.Keys)
                {
                    if (DBType.ToLower() == "SqlServer".ToLower())
                    {
                        this.CommandOffline.Parameters.Add(new System.Data.SqlClient.SqlParameter(key, Parameters[key]));
                    }
                    else if (DBType.ToLower() == "MySql".ToLower())
                    {
                        this.CommandOffline.Parameters.Add(new MySql.Data.MySqlClient.MySqlParameter(key, Parameters[key]));
                    }
                    else
                    {
                        this.CommandOffline.Parameters.Add(new System.Data.Odbc.OdbcParameter(key, Parameters[key]));
                    }
                }

                ret = this.CommandOffline.ExecuteNonQuery();
            }
            catch { throw; }
            finally
            {
                this.ConnectionOffline.Close();
            }
            return(ret);
        }
Example #2
0
        /// <summary>
        /// 获取第一行第一列对象
        /// </summary>
        /// <param name="sql">命令</param>
        /// <param name="Parameters">参数</param>
        /// <returns></returns>
        public virtual object ExecuteScalar(string sql, Dictionary <string, object> Parameters)
        {
            object ret = new object();

            this.Command.CommandType = CommandType.Text;
            this.Command.CommandText = sql;
            this.Command.Parameters.Clear();
            foreach (string key in Parameters.Keys)
            {
                if (DBType.ToLower() == "SqlServer".ToLower())
                {
                    this.CommandOffline.Parameters.Add(new System.Data.SqlClient.SqlParameter(key, Parameters[key]));
                }
                else if (DBType.ToLower() == "MySql".ToLower())
                {
                    this.CommandOffline.Parameters.Add(new MySql.Data.MySqlClient.MySqlParameter(key, Parameters[key]));
                }
                else
                {
                    this.CommandOffline.Parameters.Add(new System.Data.Odbc.OdbcParameter(key, Parameters[key]));
                }
            }

            ret = this.Command.ExecuteScalar();

            return(ret);
        }
Example #3
0
        /// <summary>
        /// 使用离线数据库连接器,无需使用Open方法,获取DataTable对象
        /// </summary>
        /// <param name="sql">命令</param>
        /// <param name="Parameters">参数</param>
        /// <returns></returns>
        public virtual DataTable GetDataTable(string sql, Dictionary <string, object> Parameters)
        {
            DataTable dt = new DataTable();
            DataSet   ds = new DataSet();

            try
            {
                this.ConnectionOffline.Open();
                this.CommandOffline.CommandType = CommandType.Text;
                this.CommandOffline.CommandText = sql;
                this.CommandOffline.Parameters.Clear();
                foreach (string key in Parameters.Keys)
                {
                    if (DBType.ToLower() == "SqlServer".ToLower())
                    {
                        this.CommandOffline.Parameters.Add(new System.Data.SqlClient.SqlParameter(key, Parameters[key]));
                    }
                    else if (DBType.ToLower() == "MySql".ToLower())
                    {
                        this.CommandOffline.Parameters.Add(new MySql.Data.MySqlClient.MySqlParameter(key, Parameters[key]));
                    }
                    else
                    {
                        this.CommandOffline.Parameters.Add(new System.Data.Odbc.OdbcParameter(key, Parameters[key]));
                    }
                }

                this.DataAdapterOffline.SelectCommand = this.CommandOffline;

                this.DataAdapterOffline.Fill(ds);
                dt = ds.Tables[0];
            }
            catch { throw; }
            finally
            {
                ds.Dispose();
                this.ConnectionOffline.Close();
            }
            return(dt);
        }
Example #4
0
        /// <summary>
        /// 使用在线连接器获取第一行的DataReader,使用完毕后记得用Close
        /// </summary>
        /// <param name="sql">命令</param>
        /// <param name="Parameters">参数</param>
        public virtual void ExecuteReaderSingleRow(string sql, Dictionary <string, object> Parameters)
        {
            this.Command.CommandType = CommandType.Text;
            this.Command.CommandText = sql;
            this.Command.Parameters.Clear();
            foreach (string key in Parameters.Keys)
            {
                if (DBType.ToLower() == "SqlServer".ToLower())
                {
                    this.CommandOffline.Parameters.Add(new System.Data.SqlClient.SqlParameter(key, Parameters[key]));
                }
                else if (DBType.ToLower() == "MySql".ToLower())
                {
                    this.CommandOffline.Parameters.Add(new MySql.Data.MySqlClient.MySqlParameter(key, Parameters[key]));
                }
                else
                {
                    this.CommandOffline.Parameters.Add(new System.Data.Odbc.OdbcParameter(key, Parameters[key]));
                }
            }

            this.DataReader = this.Command.ExecuteReader(CommandBehavior.SingleRow);
        }