Esempio n. 1
0
        public DbCommand GetStoredProcCommandWithSourceColumns(string storedProcedureName, params string[] sourceColumns)
        {
            CheckUtil.ArgumentNotNullOrEmpty(storedProcedureName, "storedProcedureName");
            if (sourceColumns == null)
            {
                throw new ArgumentNullException("sourceColumns");
            }

            DbCommand dbCommand = GetStoredProcCommand(storedProcedureName);

            using (DbConnection connection = CreateConnection())
            {
                dbCommand.Connection = connection;
                DiscoverParameters(dbCommand);
            }

            int iSourceIndex = 0;

            foreach (IDataParameter dbParam in dbCommand.Parameters)
            {
                if ((dbParam.Direction == ParameterDirection.Input) | (dbParam.Direction == ParameterDirection.InputOutput))
                {
                    dbParam.SourceColumn = sourceColumns[iSourceIndex];
                    iSourceIndex++;
                }
            }

            return(dbCommand);
        }
Esempio n. 2
0
        void DoLoadDataSet(IDbCommand command, DataSet dataSet, string[] tableNames)
        {
            CheckUtil.ArgumentNotNullOrEmptyForCollection(tableNames, "tableNames");

            for (int i = 0; i < tableNames.Length; i++)
            {
                CheckUtil.ArgumentNotNullOrEmpty(tableNames[i], string.Concat("tableNames[", i, "]"));
                //if (string.IsNullOrEmpty(tableNames[i])) throw new ArgumentException("值不能为空或者空字符串", string.Concat("tableNames[", i, "]"));
            }

            try
            {
                using (DbDataAdapter adapter = GetDataAdapter(UpdateBehavior.Standard))
                {
                    ((IDbDataAdapter)adapter).SelectCommand = command;

                    //DateTime startTime = DateTime.Now;
                    string systemCreatedTableNameRoot = "Table";
                    for (int i = 0; i < tableNames.Length; i++)
                    {
                        string systemCreatedTableName = (i == 0) ? systemCreatedTableNameRoot : systemCreatedTableNameRoot + i;

                        adapter.TableMappings.Add(systemCreatedTableName, tableNames[i]);
                    }

                    adapter.Fill(dataSet);
                    //instrumentationProvider.FireCommandExecutedEvent(startTime);
                }
            }
            catch (Exception e)
            {
                //instrumentationProvider.FireCommandFailedEvent(command.CommandText, ConnectionStringNoCredentials, e);
                throw;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 解密为字符串
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public string DecryptString(string s)
        {
            CheckUtil.ArgumentNotNullOrEmpty(s, "s");

            byte[] inputData = Convert.FromBase64String(s);
            string result = string.Empty;

            using (MemoryStream ms = new System.IO.MemoryStream(inputData))
            {
                symmetricAlgorithmProvider.Key = GetLegalKey();
                symmetricAlgorithmProvider.IV = GetLegalIV();

                ICryptoTransform encrypto = symmetricAlgorithmProvider.CreateDecryptor();

                CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(cs);
                result = sr.ReadLine();

                //CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
                //byte[] outputData = new byte[inputData.Length];
                //cs.Read(outputData, 0, outputData.Length);

                //result = Encoding.UTF8.GetString(outputData).TrimEnd(new char[] { '\0' });

                cs.Clear();
                cs.Close();
            }

            return result;
        }
Esempio n. 4
0
        /// <summary>
        /// 计算字符串的哈希值
        /// </summary>
        /// <param name="s"></param>
        /// <param name="hashAlgorithmType"></param>
        /// <param name="encoding">指定字符串的编码</param>
        /// <returns></returns>
        public static byte[] GetHash(string s, HashAlgorithmType hashAlgorithmType, Encoding encoding)
        {
            CheckUtil.ArgumentNotNullOrEmpty(s, "s");

            byte[] data = encoding.GetBytes(s);
            return(GetHash(data, hashAlgorithmType));
        }
Esempio n. 5
0
        /// <summary>
        /// 加密为数据
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public byte[] EncryptData(string s)
        {
            CheckUtil.ArgumentNotNullOrEmpty(s, "s");

            byte[] inputData = Encoding.UTF8.GetBytes(s);
            byte[] outputData = null;

            using (MemoryStream ms = new System.IO.MemoryStream())
            {
                symmetricAlgorithmProvider.Key = GetLegalKey();
                symmetricAlgorithmProvider.IV = GetLegalIV();

                ICryptoTransform cryptoTransform = symmetricAlgorithmProvider.CreateEncryptor();

                using (CryptoStream cs = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Write))
                {
                    cs.Write(inputData, 0, inputData.Length);
                    cs.FlushFinalBlock();

                    outputData = ms.ToArray();
                }
            }

            return outputData;
        }
Esempio n. 6
0
        /// <summary>
        /// 构造函数,指定数据提供程序工厂实例
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="dbProviderFactory"></param>
        public Database(string connectionString, DbProviderFactory dbProviderFactory)
        {
            CheckUtil.ArgumentNotNullOrEmpty(connectionString, "connectionString");
            CheckUtil.ArgumentNotNull(dbProviderFactory, "dbProviderFactory");

            this._ConnectionString  = connectionString;
            this._DbProviderFactory = dbProviderFactory;
        }
Esempio n. 7
0
        int DoUpdateDataSet(UpdateBehavior behavior, DataSet dataSet, string tableName, IDbCommand insertCommand, IDbCommand updateCommand, IDbCommand deleteCommand, int?updateBatchSize)
        {
            CheckUtil.ArgumentNotNullOrEmpty(tableName, "tableName");
            CheckUtil.ArgumentNotNull(dataSet, "dataSet");

            if (insertCommand == null && updateCommand == null && deleteCommand == null)
            {
                throw new ArgumentException(Resources.Data.MustInitAtLeastOneCommand);
            }

            using (DbDataAdapter adapter = GetDataAdapter(behavior))
            {
                IDbDataAdapter explicitAdapter = adapter;
                if (insertCommand != null)
                {
                    explicitAdapter.InsertCommand = insertCommand;
                }
                if (updateCommand != null)
                {
                    explicitAdapter.UpdateCommand = updateCommand;
                }
                if (deleteCommand != null)
                {
                    explicitAdapter.DeleteCommand = deleteCommand;
                }

                if (updateBatchSize != null)
                {
                    adapter.UpdateBatchSize = (int)updateBatchSize;
                    if (insertCommand != null)
                    {
                        adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
                    }
                    if (updateCommand != null)
                    {
                        adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
                    }
                    if (deleteCommand != null)
                    {
                        adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.None;
                    }
                }

                try
                {
                    //DateTime startTime = DateTime.Now;
                    int rows = adapter.Update(dataSet.Tables[tableName]);
                    //instrumentationProvider.FireCommandExecutedEvent(startTime);
                    return(rows);
                }
                catch (Exception e)
                {
                    //instrumentationProvider.FireCommandFailedEvent("DbDataAdapter.Update() " + tableName, ConnectionStringNoCredentials, e);
                    throw;
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        /// 计算字符串的哈希值并将其转换为使用Base64编码的字符串
        /// </summary>
        /// <param name="s"></param>
        /// <param name="hashAlgorithmType"></param>
        /// <returns></returns>
        public static string GetHash2Base64(string s, HashAlgorithmType hashAlgorithmType, Encoding encoding)
        {
            CheckUtil.ArgumentNotNullOrEmpty(s, "s");

            byte[] data   = GetHash(s, hashAlgorithmType, encoding);
            string result = Convert.ToBase64String(data);

            return(result);
        }
Esempio n. 9
0
        /// <summary>
        /// 计算字符串的哈希值并将其转换为字符串
        /// </summary>
        /// <param name="s"></param>
        /// <param name="hashAlgorithmType"></param>
        /// <param name="encoding">指定字符串的编码</param>
        /// <returns></returns>
        public static string GetHash2String(string s, HashAlgorithmType hashAlgorithmType, Encoding encoding)
        {
            CheckUtil.ArgumentNotNullOrEmpty(s, "s");

            string result = string.Empty;

            byte[] bytes = GetHash(s, hashAlgorithmType, encoding);

            foreach (byte b in bytes)
            {
                result += Convert.ToString(b, 16).ToUpper(CultureInfo.InvariantCulture).PadLeft(2, '0');
            }

            return(result);
        }
Esempio n. 10
0
        /// <summary>
        /// 为指定的存储过程创建一个DbCommand对象实例
        /// </summary>
        public virtual DbCommand GetStoredProcCommand(string storedProcedureName, params object[] parameterValues)
        {
            CheckUtil.ArgumentNotNullOrEmpty(storedProcedureName, "storedProcedureName");

            DbCommand command = CreateCommandByCommandType(CommandType.StoredProcedure, storedProcedureName);

            parameterCache.SetParameters(command, this);

            if (SameNumberOfParametersAndValues(command, parameterValues) == false)
            {
                throw new InvalidOperationException(Resources.Data.ParameterMatchFailure);
            }

            AssignParameterValues(command, parameterValues);
            return(command);
        }
Esempio n. 11
0
        /// <summary>
        /// 解密为数据
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public byte[] DecryptData(string s)
        {
            CheckUtil.ArgumentNotNullOrEmpty(s, "s");

            byte[] inputData = Convert.FromBase64String(s);
            byte[] outputData = null;

            using (MemoryStream ms = new System.IO.MemoryStream(inputData))
            {
                symmetricAlgorithmProvider.Key = GetLegalKey();
                symmetricAlgorithmProvider.IV = GetLegalIV();

                ICryptoTransform encrypto = symmetricAlgorithmProvider.CreateDecryptor();

                CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
                outputData = new byte[inputData.Length];
                cs.Read(outputData, 0, outputData.Length);

                cs.Clear();
                cs.Close();
            }

            return outputData;
        }
Esempio n. 12
0
 /// <summary>
 /// 为指定的存储过程创建一个DbCommand对象实例
 /// </summary>
 public virtual DbCommand GetStoredProcCommand(string storedProcedureName)
 {
     CheckUtil.ArgumentNotNullOrEmpty(storedProcedureName, "storedProcedureName");
     return(CreateCommandByCommandType(CommandType.StoredProcedure, storedProcedureName));
 }
Esempio n. 13
0
 /// <summary>
 /// 为SQL查询创建一个DbCommand对象实例
 /// </summary>
 public DbCommand GetSqlStringCommand(string query)
 {
     CheckUtil.ArgumentNotNullOrEmpty(query, "query");
     return(CreateCommandByCommandType(CommandType.Text, query));
 }
Esempio n. 14
0
        /// <summary>
        /// 创建一个数据提供程序实例
        /// </summary>
        /// <param name="connectionStringName"></param>
        /// <returns></returns>
        public static Database Create(string connectionStringName)
        {
            CheckUtil.ArgumentNotNullOrEmpty(connectionStringName, "connectionStringName");

            ConnectionStringSettings css = ConfigurationManager.ConnectionStrings[connectionStringName];

            if (css == null)
            {
                throw new Exception(string.Format(Resources.Data.ConnectionStringNameNotFound, connectionStringName));
            }

            string            connectionString = css.ConnectionString;
            string            providerName     = css.ProviderName;
            Database          db = new SqlServerDatabase(connectionString);
            DbProviderFactory providerFactory = null;

            if (string.IsNullOrEmpty(providerName))
            {
                return(db);
            }

            //if (css.ProviderName == "System.Data.OleDb")
            //{
            //    providerFactory = OleDbFactory.Instance;
            //}
            //else
            //{
            //    providerFactory = DbProviderFactories.GetFactory(css.ProviderName);
            //}
            //if (providerFactory == null) throw new Exception(string.Format(Resources.Data.DataProviderNotFound, css.ProviderName));

            switch (providerName)
            {
            //case "System.Data.SqlClient":
            //    break;
            case "System.Data.Odbc":
                db = new OdbcDatabase(connectionString);
                break;

            case "System.Data.OleDb":
                db = new OleDbDatabase(connectionString);
                break;

            case "System.Data.OracleClient":
                db = new OracleDatabase(connectionString);
                break;

            case "Oracle.ManagedDataAccess.Client":
                db = new OracleDatabase(connectionString);
                break;

            case "Devart.Data.Oracle": //http://evget.com/zh-CN/product/954/feature.aspx  http://www.devart.com/
            case "DDTek.Oracle":       //http://www.datadirect.com/index.html
                providerFactory = DbProviderFactories.GetFactory(providerName);
                db = new OracleDatabase(connectionString, providerFactory);
                break;

            case "System.Data.SQLite":
                providerFactory = DbProviderFactories.GetFactory(providerName);
                db = new SQLiteDatabase(connectionString, providerFactory);
                break;

            case "MySql.Data.MySqlClient":
                providerFactory = DbProviderFactories.GetFactory(providerName);
                db = new MySqlDatabase(connectionString, providerFactory);
                break;

            case "IBM.Data.DB2":
                providerFactory = DbProviderFactories.GetFactory(providerName);
                db = new DB2Database(connectionString, providerFactory);
                break;

            case "FirebirdSql.Data.FirebirdClient":
                providerFactory = DbProviderFactories.GetFactory(providerName);
                db = new FirebirdDatabase(connectionString, providerFactory);
                break;

            default:
                break;
            }

            return(db);
        }
Esempio n. 15
0
        /// <summary>
        /// 创建一个数据提供程序实例
        /// </summary>
        /// <param name="connectionStringName"></param>
        /// <returns></returns>
        public static Database Create(string connectionStringName)
        {
            CheckUtil.ArgumentNotNullOrEmpty(connectionStringName, "connectionStringName");
            DbConnConfigDAL dal = new DbConnConfigDAL();

            var model = dal.FindOne(connectionStringName);

            if (model == null)
            {
                throw new Exception(string.Format(Resources.Data.ConnectionStringNameNotFound, connectionStringName));
            }

            string            connectionString = model.ConnectionString;
            string            providerName     = model.ProviderName;
            Database          db = new SqlServerDatabase(connectionString);
            DbProviderFactory providerFactory = null;

            if (string.IsNullOrEmpty(providerName))
            {
                return(db);
            }

            //if (css.ProviderName == "System.Data.OleDb")
            //{
            //    providerFactory = OleDbFactory.Instance;
            //}
            //else
            //{
            //    providerFactory = DbProviderFactories.GetFactory(css.ProviderName);
            //}
            //if (providerFactory == null) throw new Exception(string.Format(Resources.Data.DataProviderNotFound, css.ProviderName));

            switch (providerName)
            {
            //case "System.Data.SqlClient":
            //    break;
            case "System.Data.Odbc":
                db = new OdbcDatabase(connectionString);
                break;

            case "System.Data.OleDb":
                db = new OleDbDatabase(connectionString);
                break;

            case "System.Data.SQLite":
                providerFactory = DbProviderFactories.GetFactory(providerName);
                db = new SQLiteDatabase(connectionString, providerFactory);
                break;

            case "MySql.Data.MySqlClient":
                providerFactory = DbProviderFactories.GetFactory(providerName);
                db = new MySqlDatabase(connectionString, providerFactory);
                break;

            case "IBM.Data.DB2":
                providerFactory = DbProviderFactories.GetFactory(providerName);
                db = new DB2Database(connectionString, providerFactory);
                break;

            case "FirebirdSql.Data.FirebirdClient":
                providerFactory = DbProviderFactories.GetFactory(providerName);
                db = new FirebirdDatabase(connectionString, providerFactory);
                break;

            default:
                break;
            }

            return(db);
        }
Esempio n. 16
0
        /// <summary>
        /// 创建一个数据提供程序实例
        /// </summary>
        /// <param name="connectionStringName"></param>
        /// <returns></returns>
        public static Database Create(string connectionStringName)
        {
            CheckUtil.ArgumentNotNullOrEmpty(connectionStringName, "connectionStringName");

            ConnectionStringSettings css = ConfigurationManager.ConnectionStrings[connectionStringName];

            if (css == null)
            {
                throw new Exception(string.Format(Resources.Data.ConnectionStringNameNotFound, connectionStringName));
            }

            string            connectionString = css.ConnectionString;
            string            providerName     = css.ProviderName;
            Database          db = new SqlServerDatabase(connectionString);
            DbProviderFactory providerFactory = null;

            if (string.IsNullOrEmpty(providerName))
            {
                return(db);
            }

            providerFactory = DbProviderFactories.GetFactory(css.ProviderName);
            if (providerFactory == null)
            {
                throw new Exception(string.Format(Resources.Data.DataProviderNotFound, css.ProviderName));
            }

            switch (providerName)
            {
            //case "System.Data.SqlClient":
            //    break;
            case "System.Data.Odbc":
                db = new OdbcDatabase(connectionString);
                break;

            case "System.Data.OleDb":
                db = new OleDbDatabase(connectionString);
                break;

            case "System.Data.OracleClient":
                db = new OracleDatabase(connectionString);
                break;

            case "System.Data.SQLite":
                db = new SQLiteDatabase(connectionString, providerFactory);
                break;

            case "MySql.Data.MySqlClient":
                db = new MySqlDatabase(connectionString, providerFactory);
                break;

            case "IBM.Data.DB2":
                db = new DB2Database(connectionString, providerFactory);
                break;

            case "FirebirdSql.Data.FirebirdClient":
                db = new FirebirdDatabase(connectionString, providerFactory);
                break;

            default:
                break;
            }

            return(db);
        }