1. 程式人生 > 其它 >sqlsugar與資料庫之間的相互操作

sqlsugar與資料庫之間的相互操作

sqlsugar與資料庫之間的相互操作

sqlsugar這個框架查詢資料速度很快, 接近原生, 所以就決定用它咯 它和ef一樣 有codefirst和dbfirst

不多說 直接上程式碼

首先 寫連線資料庫的類

public class SqlSugarInstance
    {
     
       public static SqlSugarClient GetInstance()
        {
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString=@"xxxxxx", //必填
                DbType = DbType.SqlServer, //必填   
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute  // Attribute用於DbFirst  從資料庫生成model的
//InitKeyType = InitKeyType.SystemTable SystemTable用於Codefirst 從model庫生成資料庫表的
}); return db; } }

然後就是程式碼了

 using (SqlSugarClient db =SqlSugarInstance.GetInstance())
            {
                try
                {
                    // db.CodeFirst.InitTables(typeof(xxxx));
                     db.DbFirst.Where("xxxx").CreateClassFile("D:\\Demo\\2.txt");//生成檔案的·地址 
                }
                catch (Exception ex)
                {

                    throw;
                }
              
            }

好了 差不多就是這樣了 很方便快捷 生成檔案的地址也可以放在你專案檔案指定的地址下 不過沒試過 不知道能不能成功 哈哈哈