SqlSugar/Src/Asp.NetCore2/DorisTest/Program.cs

93 lines
3.1 KiB
C#
Raw Normal View History

2024-06-30 21:37:32 +08:00
using DorisTest;
using SqlSugar;
2024-03-26 17:03:30 +08:00
using System;
2024-07-01 14:36:12 +08:00
using System.ComponentModel;
2024-03-26 17:03:30 +08:00
namespace OrmTest
{
public class Program
{
static void Main(string[] args)
2024-03-26 20:34:22 +08:00
{
2024-06-30 21:39:51 +08:00
var db = DbHelper.GetNewDb();
2024-07-01 14:36:12 +08:00
2024-06-30 21:37:32 +08:00
db.CodeFirst.InitTables<LogEntity>();
2024-03-26 20:34:22 +08:00
2024-08-23 14:04:41 +08:00
2024-07-01 14:36:12 +08:00
db.CodeFirst.InitTables<Student112>();//drop colum
db.CodeFirst.InitTables<Student11>();//add column
2024-08-23 14:04:41 +08:00
db.DbMaintenance.TruncateTable<Student112>();//truncate data
2024-07-01 14:36:12 +08:00
db.Insertable(new Student11() { Id = 1, Age = 1, Name = "a",DateTime=DateTime.Now })
2024-03-26 20:34:22 +08:00
.ExecuteCommand();
2024-08-23 14:04:41 +08:00
db.Insertable(new Student11() { Id = 2, Age = 1, Name = "a", DateTime = DateTime.Now })
.ExecuteCommand();
2024-03-26 20:34:22 +08:00
var list=db.Queryable<Student11>().ToList();
2024-08-23 14:04:41 +08:00
var rows1 = db.Updateable(list.FirstOrDefault()).ExecuteCommand();
//var rows2 = db.Updateable(list).ExecuteCommand();
2024-03-26 20:34:22 +08:00
db.Deleteable(list).ExecuteCommand();
2024-03-26 17:03:30 +08:00
}
2024-07-01 14:36:12 +08:00
}
[SugarTable("Student1101")]
public class Student112
{
[SqlSugar.SugarColumn(IsPrimaryKey = true)]
public int Id { get; set; } // 学生ID (Student ID)
public string Name { get; set; } // 学生姓名 (Student Name)
public int Age { get; set; } // 学生年龄 (Student Ag
}
[SugarTable("Student1101")]
2024-03-26 20:30:57 +08:00
public class Student11
{
[SqlSugar.SugarColumn(IsPrimaryKey = true )]
public int Id { get; set; } // 学生ID (Student ID)
public string Name { get; set; } // 学生姓名 (Student Name)
public int Age { get; set; } // 学生年龄 (Student Age)
2024-07-01 14:36:12 +08:00
[SugarColumn(IsNullable =true)]
public DateTime DateTime { get; set; }
2024-03-26 17:03:30 +08:00
}
/// <summary>
/// Helper class for database operations
/// 数据库操作的辅助类
/// </summary>
public class DbHelper
{
/// <summary>
/// Database connection string
/// 数据库连接字符串
/// </summary>
2024-07-01 14:36:12 +08:00
public readonly static string Connection = "server=39.170.74.1;port=9031;Database=test;Uid=root;Pwd=lq!1211q!;Pooling=false;";
2024-03-26 17:03:30 +08:00
/// <summary>
/// Get a new SqlSugarClient instance with specific configurations
/// 获取具有特定配置的新 SqlSugarClient 实例
/// </summary>
/// <returns>SqlSugarClient instance</returns>
public static SqlSugarClient GetNewDb()
{
var db = new SqlSugarClient(new ConnectionConfig()
{
IsAutoCloseConnection = true,
DbType = DbType.Doris,
ConnectionString = Connection,
LanguageType = LanguageType.Default//Set language
},
it => {
// Logging SQL statements and parameters before execution
// 在执行前记录 SQL 语句和参数
it.Aop.OnLogExecuting = (sql, para) =>
{
Console.WriteLine(UtilMethods.GetNativeSql(sql, para));
};
2024-06-30 21:37:32 +08:00
it.Aop.OnError = ex =>
{
};
2024-03-26 17:03:30 +08:00
});
return db;
}
}
}