首页 > 数据库 >上位机_Winform系列总结(winform注入sqlsugar)

上位机_Winform系列总结(winform注入sqlsugar)

时间:2023-08-08 09:13:48浏览次数:33  
标签:set SqlSugarClient get db static Winform public sqlsugar winform

1、引入SqlSugar

 2、新建SqlSugarConfig类

 public class SqlSugarConfig
    {
        private static readonly string connectionString = "Data Source=localhost;Database=h2test;User Id=root;Password=root;charset=utf8;port=3306";

        public static SqlSugarClient GetInstance()
        {
            var db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = connectionString,
                DbType = DbType.MySql,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute
            });

            return db;
        }
    }

3、新建实体Student类

public class Student
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int ID { get; set; }        
        public string Name { get; set; }
        public string Pwd { get; set; }
        public int Role { get; set; }
    }

4、在程序启动的Program类中依赖注入Sqlsugar

 internal static class Program
    {
        public static IContainer Container { get; set; }
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            var builder = new ContainerBuilder();

            // Register SqlSugarClient as a singleton
            builder.Register(c => SqlSugarConfig.GetInstance()).As<SqlSugarClient>().SingleInstance();

            // Register forms
            builder.RegisterType<Form1>().AsSelf().InstancePerDependency();

            Container = builder.Build();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(Container.Resolve<Form1>());

        }
    }

5、在使用的窗体构造函数中引入

 private readonly SqlSugarClient mysqldb;
        public Form1(SqlSugarClient db)
        {
            InitializeComponent();
            this.mysqldb = db;
        }

6、开始使用

 public BindingList<Student> Students;
        private void Form1_Load(object sender, EventArgs e)
        {
            //创建表
            //创建表:根据实体类CodeFirstTable1  (所有数据库都支持)    
            mysqldb.CodeFirst.InitTables(typeof(Student));//这样一个表就能成功创建了
            Students =new BindingList<Student>( mysqldb.Queryable<Student>().ToList());
            dataGridView1.DataSource = Students;
        }

 

标签:set,SqlSugarClient,get,db,static,Winform,public,sqlsugar,winform
From: https://www.cnblogs.com/appform/p/17613267.html

相关文章

  • C# winform 实现逐个显示文字
    如题,可以使用timer控件来实现,可以通过设置timer控件属性中的Interval来设置时间间隔(使用VS2019)usingSystem;usingSystem.Diagnostics;usingSystem.Windows.Forms;namespaceTest{publicpartialclassForm3:Form{privateconststringflash="逐个......
  • Winform 富文本框字体高亮
    1.RichTextBox控件-WindowsForms.NETFramework|MicrosoftLearn2.c#代码编辑控件(代码着色控件)ICSharpCode.TextEditor简单应用示例_风雪子郁的博客-CSDN博客3.FastColoredTextBoxforSyntaxHighlighting-CodeProject 展示效果  二:ICSharpCode.TextE......
  • c#串口通信讲解(一)(winform、wpf)
    转载:https://blog.csdn.net/weixin_30466421/article/details/99278174串口操作需要注意的几点如下:1、如果是USB转串口;则需要安装USB转串口驱动,附件有此驱动。2、串口打开状态最好不要直接插拔串口,可能会导致中控板或者串口线烧坏。3、使用串口调试工具CEIWEI,下一章节会贴上......
  • .NET ORM 鉴别器 和 TDengine 使用 -SqlSugar
    SqlSugarORMSqlSugar是一款老牌.NET开源多库架构ORM框架,一套代码能支持多种数据库像Admin.net、Blog.Core、CoreShop等知名开源项目都采用了SqlSugar作为底层特色1:超级简单在不用任何设计模式,任何框架的情况下都可以拥有最佳体验,SqlSugar做到了保姆一样的服务,直接用不需......
  • 盘点下4个Winform UI开源控件库
    今天一起来盘点下4个WinformUI开源控件库,有.NetFramework,也有.NetCore。 1、支持.Net7的开源UI组件框架项目简介这是一个基于.NetFramework、.Net6+开发的,WinForm开源UI框架,框架包含常用的控件库、工具类库、扩展类库、单窗口库、多窗口库。默认风格是模仿Element的主......
  • Winform两个项目间的调用
    Winform1:staticclassProgram   {       ///<summary>       ///应用程序的主入口点。       ///</summary>       [STAThread]       staticvoidMain(string[]args)       {           Application.Ena......
  • c# WinForm 引用 Chrome 模拟操作
    Nuget CefSharp.WinForms publicForm1(){InitializeComponent();chromiumWebBrowser1.LoadingStateChanged+=ChromiumWebBrowser1_LoadingStateChanged;}privatevoidbutton1_Click(objectsender,EventArgs......
  • WinForm RichTextBox 加载大量文本卡死和UTF-8乱码问题
    在RichTextBox控件的使用中我们会遇到加载TXT文件的问题,通常我们会有两种处理方式。一、加载TXT字符串,设置到RichTextBox//打开并且读取文件数据FileStreamfs=newFileStream(txtPath,FileMode.Open,FileAccess.Read);StreamReadersr=newStreamReader(fs,Encoding.U......
  • SqlSugar.SqlSugarException: 连接数据库过程中发生错误,证书链是由不受信任的颁发机
    本地代码调试时报错 解决办法:直接在“数据库连接字符串最后面”增加证书信任的配置。;TrustServerCertificate=true ......
  • SqlSugar的仓储搭建
    直接去百度网盘获取:链接:https://pan.baidu.com/s/105JxRaqZcTGIrT365BzRRw 提取码:wzkm使用的是.netCore6所以在Program中的配置如下://注册上下文:AOP里面可以获取IOC对象,如果有现成框架比如Furion可以不写这一行builder.Services.AddHttpContextAccessor();......