Exemple #1
0
        /// <summary>
        /// 获取标准SQL字符串
        /// </summary>
        /// <param name="tp"></param>
        /// <param name="multiTable"></param>
        /// <returns></returns>
        public string ToSqlString(DatabaseTypes tp, bool multiTable = false)
        {
            string res = "DELETE FROM ";

            res += _table.ToSqlString(tp);

            if (!Equals(_where, null))
            {
                res += " WHERE " + _where.ToSqlString(tp);
            }

            return(res);
        }
Exemple #2
0
        /// <summary>
        /// 获取标准SQL字符串
        /// </summary>
        /// <param name="tp"></param>
        /// <param name="multiTable"></param>
        /// <returns></returns>
        public string ToSqlString(DatabaseTypes tp, bool multiTable = false)
        {
            string res = "INSERT INTO ";

            res += _table.ToSqlString(tp);

            string cols = "";
            string vals = "";

            foreach (var key in _row.Keys)
            {
                if (cols != "")
                {
                    cols += ",";
                }
                if (vals != "")
                {
                    vals += ",";
                }
                using (SqlUnits.TableField field = new SqlUnits.TableField(_table, key)) {
                    cols += field.ToSqlString(tp);
                }
                switch (tp)
                {
                case DatabaseTypes.MySQL:
                    vals += $"'{_row[key].Replace("'", "\'")}'";
                    break;

                //return $"'{_value.Replace("'", "\'")}'";
                case DatabaseTypes.Microsoft_Office_Access:
                case DatabaseTypes.Microsoft_Office_Access_v12:
                case DatabaseTypes.Microsoft_SQL_Server:
                case DatabaseTypes.SQLite:
                case DatabaseTypes.SQLite_3:
                case DatabaseTypes.PostgreSQL:
                    vals += $"'{_row[key].Replace("'", "''")}'";
                    break;

                //return $"'{_value.Replace("'", "''")}'";
                default:
                    throw new Exception($"尚未支持数据库 {tp.ToString()} 中的字符串转义。");
                }
            }

            res += $"({cols}) VALUES ({vals})";

            return(res);
        }
Exemple #3
0
        /// <summary>
        /// 获取标准SQL字符串
        /// </summary>
        /// <param name="tp"></param>
        /// <param name="multiTable"></param>
        /// <returns></returns>
        public string ToSqlString(DatabaseTypes tp, bool multiTable = false)
        {
            string res = "UPDATE ";

            res += _table.ToSqlString(tp);
            res += " SET ";

            string cols   = "";
            string keyCol = "";

            if (!Equals(_keyField, null))
            {
                keyCol = _keyField.ToString();
            }

            foreach (var key in _row.Keys)
            {
                if (key != keyCol)
                {
                    if (cols != "")
                    {
                        cols += ",";
                    }
                    using (SqlUnits.TableField field = new SqlUnits.TableField(_table, key)) {
                        cols += field.ToSqlString(tp);
                    }
                    //cols += $" = '{_row[key]}'";
                    switch (tp)
                    {
                    case DatabaseTypes.MySQL:
                        cols += $" = '{_row[key].Replace("'", "\'")}'";
                        break;

                    //return $"'{_value.Replace("'", "\'")}'";
                    case DatabaseTypes.Microsoft_Office_Access:
                    case DatabaseTypes.Microsoft_Office_Access_v12:
                    case DatabaseTypes.Microsoft_SQL_Server:
                    case DatabaseTypes.SQLite:
                    case DatabaseTypes.SQLite_3:
                    case DatabaseTypes.PostgreSQL:
                        cols += $" = '{_row[key].Replace("'", "''")}'";
                        break;

                    //return $"'{_value.Replace("'", "''")}'";
                    default:
                        throw new Exception($"尚未支持数据库 {tp.ToString()} 中的字符串转义。");
                    }
                }
            }

            res += cols;

            if (!keyCol.IsNone())
            {
                res += " WHERE ";
                using (SqlUnits.TableField field = new SqlUnits.TableField(_table, keyCol)) {
                    res += field.ToSqlString(tp);
                    switch (tp)
                    {
                    case DatabaseTypes.MySQL:
                        res += $" = '{_row[keyCol].Replace("'", "\'")}'";
                        break;

                    //return $"'{_value.Replace("'", "\'")}'";
                    case DatabaseTypes.Microsoft_Office_Access:
                    case DatabaseTypes.Microsoft_Office_Access_v12:
                    case DatabaseTypes.Microsoft_SQL_Server:
                    case DatabaseTypes.SQLite:
                    case DatabaseTypes.SQLite_3:
                    case DatabaseTypes.PostgreSQL:
                        res += $" = '{_row[keyCol].Replace("'", "''")}'";
                        break;

                    //return $"'{_value.Replace("'", "''")}'";
                    default:
                        throw new Exception($"尚未支持数据库 {tp.ToString()} 中的字符串转义。");
                    }
                    //res += $" = '{_row[keyCol]}'";
                }
            }
            else
            {
                if (!Equals(_where, null))
                {
                    res += " WHERE " + _where.ToSqlString(tp);
                }
            }


            return(res);
        }