Ejemplo n.º 1
0
        public void CreateTable(DataTable table)
        {
            StringBuilder sb = new Text.StringBuilder();

            sb.Append("create table if not exists `");
            sb.Append(table.TableName);
            sb.AppendLine("`(");

            bool firstRecord = true;

            foreach (DataColumn col in table.Columns)
            {
                if (col.ColumnName.Trim().Length == 0)
                {
                    throw new Exception("Column name cannot be blank.");
                }
                if (firstRecord)
                {
                    firstRecord = false;
                }
                else
                {
                    sb.AppendLine(",");
                }

                sb.Append(col.ColumnName);
                sb.Append(" ");

                if (col.AutoIncrement)
                {
                    sb.Append("integer primary key autoincrement");
                    continue;
                }

                sb.Append("text");

                /* if (col.Table.PrimaryKey.)
                 *   sb.Append(" primary key");*/
            }

            sb.AppendLine(");");

            cmd.CommandText = sb.ToString();
            cmd.ExecuteNonQuery();
        }
Ejemplo n.º 2
0
        public static string ToSqlString(this IDbCommand cmd, bool doIncludeParameters = false)
        {
            var sql      = cmd.CommandText;
            var cmdParam = new Text.StringBuilder();

            foreach (IDbDataParameter p in cmd.Parameters)
            {
                var val = p.DbType.GetSqlQualifiedValue(p.Value, true);
                sql = sql.Replace(p.ParameterName, val);
                cmdParam.AppendLine($"-- {p.ParameterName}({p.DbType}): {val}");
            }
            return(sql + ";" + (doIncludeParameters ? $"-- Parameters {Environment.NewLine}/*" + cmdParam + "*/" : string.Empty));
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 返回异常所有信息
 /// </summary>
 /// <param name="webHuanHang">是否为web换行</param>
 /// <param name="style">样式: color:red;  </param>
 /// <returns></returns>
 public string GetAllExceptionMessage(bool webHuanHang = false, string style = "")
 {
     System.Text.StringBuilder message = new Text.StringBuilder("");
     if (_HasException)
     {
         int index = 0;
         _ExceptionCollection.Reverse().Distinct().ToList().ForEach(x =>
         {
             if (webHuanHang)
             {
                 message.AppendFormat(@"<p style='{2}'>【{0}】:{1}</p>", index, x.Message, style);
             }
             else
             {
                 message.AppendLine(x.Message);
             }
             index++;
         });
     }
     return(message.ToString());
 }
Ejemplo n.º 4
0
        public void CreateTable(SQLiteTable table)
        {
            StringBuilder sb = new Text.StringBuilder();

            sb.Append("create table if not exists `");
            sb.Append(table.TableName);
            sb.AppendLine("`(");

            bool firstRecord = true;

            foreach (SQLiteColumn col in table.Columns)
            {
                if (col.ColumnName.Trim().Length == 0)
                {
                    throw new Exception("Column name cannot be blank.");
                }

                if (firstRecord)
                {
                    firstRecord = false;
                }
                else
                {
                    sb.AppendLine(",");
                }

                sb.Append(col.ColumnName);
                sb.Append(" ");

                if (col.AutoIncrement)
                {
                    sb.Append("integer primary key autoincrement");
                    continue;
                }

                switch (col.ColDataType)
                {
                case ColType.Text:
                    sb.Append("text"); break;

                case ColType.Integer:
                    sb.Append("integer"); break;

                case ColType.Decimal:
                    sb.Append("decimal"); break;

                case ColType.DateTime:
                    sb.Append("datetime"); break;

                case ColType.BLOB:
                    sb.Append("blob"); break;
                }

                if (col.PrimaryKey)
                {
                    sb.Append(" primary key");
                }
                else if (col.NotNull)
                {
                    sb.Append(" not null");
                }
                else if (col.DefaultValue.Length > 0)
                {
                    sb.Append(" default ");

                    if (col.DefaultValue.Contains(" ") || col.ColDataType == ColType.Text || col.ColDataType == ColType.DateTime)
                    {
                        sb.Append("'");
                        sb.Append(col.DefaultValue);
                        sb.Append("'");
                    }
                    else
                    {
                        sb.Append(col.DefaultValue);
                    }
                }
            }

            sb.AppendLine(");");

            cmd.CommandText = sb.ToString();
            cmd.ExecuteNonQuery();
        }