Example #1
0
        /// <summary>
        /// 直接运行SQL语句
        /// </summary>
        /// <param name="ConnectionName">连接名</param>
        /// <param name="sql">sql语句</param>
        /// <param name="opType">操作类型</param>
        /// <returns></returns>
        public static object RunSQL(string connectionName, string sql, emOperationType opType)
        {
            string wrongMessage = "";
            object obj          = null;

            DBLInit _DBLInit = new DBLInit(connectionName);

            switch (opType)
            {
            case emOperationType.select:
                obj = _DBLInit.GetDataTable(sql, out wrongMessage);
                break;

            case emOperationType.delete:
            case emOperationType.insert:
            case emOperationType.update:
                obj = _DBLInit.ProcessSql(sql, opType, out wrongMessage);
                break;
            }

            MyORM.Log.WriteInfo(connectionName + "-" + opType.ToString() + "-" + sql);

            if (!string.IsNullOrEmpty(wrongMessage))
            {
                MyORM.Log.WriteError(wrongMessage);
            }

            return(obj);
        }
Example #2
0
        /// <summary>
        /// 运行SQL语句
        /// </summary>
        /// <param name="cmdText">SQL语句</param>
        /// <param name="eOType">操作方法</param>
        /// <param name="WrongMessage">返回的错误信息</param>
        /// <returns>当方法为insert的时候 返回新建索引</returns>
        public int ProcessSql(string cmdText, emOperationType eOType, out string WrongMessage)
        {
            WrongMessage = null;
            int tid = 0;

            switch (_dbType)
            {
            case emDBType.SqlServer:
                if (eOType == emOperationType.insert)
                {
                    cmdText += ";select SCOPE_IDENTITY()";
                }
                using (SqlConnection conn = new SqlConnection(_ConnectionString))
                {
                    SqlTransaction tx = null;
                    try
                    {
                        conn.Open();
                        tx = conn.BeginTransaction();
                        SqlCommand cmd = new SqlCommand(cmdText, conn)
                        {
                            Transaction = tx
                        };
                        object objValue = cmd.ExecuteScalar();
                        if (objValue != null)
                        {
                            tid = Convert.ToInt32(objValue);
                        }
                        tx.Commit();
                        cmd.Connection.Close();
                    }
                    catch (Exception ex)
                    {
                        WrongMessage = ex.Message;
                        tx.Rollback();
                    }
                }
                break;
            }
            return(tid);
        }