1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| using NewLife.Log; using System; using XCode.DataAccessLayer;
namespace ConsoleApp1 { internal class Program { static void Main(string[] args) { XTrace.UseConsole();
TestXCode.TestSqlite(); TestXCode.TestMysql();
Console.WriteLine("Done"); Console.ReadLine(); } }
public class Database {
public string id { get; set; }
public DateTime datetime { get; set; }
public int int_index { get; set; }
public short short_index { get; set; }
public float value { get; set; }
public double dd_value { get; set; }
public static string GetCreateTableString_Sqlite(string table) { return $"CREATE TABLE IF NOT EXISTS {table}(" + $"id VARCHAR(20) NULL," + $"datetime DATETIME NOT NULL," + $"int_index INT NULL DEFAULT NULL," + $"short_index SMALLINT DEFAULT NULL," + $"value FLOAT(9, 3) NULL DEFAULT NULL," + $"dd_value DOUBLE(9, 3) NULL DEFAULT NULL);"; }
public static string GetCreateTableString_Mysql(string database, string table) { return $"CREATE TABLE IF NOT EXISTS {database}.{table}(" + $"`id` VARCHAR(20) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci' NULL COMMENT 'id'," + $"`datetime` DATETIME NOT NULL COMMENT 'datetime'," + $"`int_index` INT NULL DEFAULT NULL COMMENT 'int_index'," + $"`short_index` SMALLINT NULL DEFAULT NULL COMMENT 'short_index'," + $"`value` FLOAT(9, 1) NULL DEFAULT NULL COMMENT 'value'," + $"`dd_value` DOUBLE(9, 1) NULL DEFAULT NULL COMMENT 'dd_value');"; }
public string GetInsertString(string table) { return $"INSERT INTO {table} values" + ValueToString(); }
private string ValueToString() { return $"('{id}','{datetime}','{int_index}','{short_index}','{value}','{dd_value}')"; }
}
public static class TestXCode { public static void TestSqlite() { const string database = "testsqlite";
DAL.AddConnStr(database, $"Data Source={database}.db;", null, "Sqlite");
var dal = DAL.Create(database);
dal.Execute(Database.GetCreateTableString_Sqlite(nameof(Database)));
for (int i = 0; i < 10; i++) { var data = new Database { id = NewLife.Security.Rand.NextString(7), datetime = DateTime.Now, int_index = NewLife.Security.Rand.Next(int.MinValue, int.MaxValue), short_index = (short)NewLife.Security.Rand.Next(short.MinValue, short.MaxValue), value = (float)(NewLife.Security.Rand.Next(0, 999999999) * Math.Pow(10, -NewLife.Security.Rand.Next(2, 5))), dd_value = (double)(NewLife.Security.Rand.Next(0, 999999999) * Math.Pow(10, -NewLife.Security.Rand.Next(2, 5))), }; dal.Execute(data.GetInsertString(nameof(Database))); } }
public static void TestMysql() { const string database = "testmysql";
DAL.AddConnStr(database, $"Server=127.0.0.1;Port=3306;User ID=root;Password=root;DataBase=sys;", null, "Mysql"); var dal = DAL.Create(database);
dal.Execute($"CREATE SCHEMA IF NOT EXISTS {database} DEFAULT CHARACTER SET utf8mb4;");
dal.Execute(Database.GetCreateTableString_Mysql(database, nameof(Database)));
DAL.AddConnStr(database, $"Server=127.0.0.1;Port=3306;User ID=root;Password=root;DataBase={database};", null, "Mysql");
for (int i = 0; i < 10; i++) { var data = new Database { id = NewLife.Security.Rand.NextString(7), datetime = DateTime.Now, int_index = NewLife.Security.Rand.Next(int.MinValue, int.MaxValue), short_index = (short)NewLife.Security.Rand.Next(short.MinValue, short.MaxValue), value = (float)(NewLife.Security.Rand.Next(0, 999999999) * Math.Pow(10, -NewLife.Security.Rand.Next(2, 5))), dd_value = (double)(NewLife.Security.Rand.Next(0, 999999999) * Math.Pow(10, -NewLife.Security.Rand.Next(2, 5))), }; dal.Execute(data.GetInsertString($"{database}.{nameof(Database)}")); } }
}
}
|