C# 烂笔头

DB

//sqltite
   private string strConn = @"datasource = default.sqlite";
//mysql
  private string strConn="server=localhost;database=dbname;user=root;password=rootpass123456;port=3306;pooling=true;max pool size=20;persist security info=True;charset=utf8mb4;",
//sqlserver 1 本地不用密码
  private string strConn="Data Source=localhost;Initial Catalog=SP2023;Integrated Security=True;Trust Server Certificate=True;MultipleActiveResultSets=true"
   
//sqlserver 2 云端需要密码
string connectionString = "Data Source=localhost;Initial Catalog=SP2023;User ID=myUsername;Password=mySecurePassword;TrustServerCertificate=True;MultipleActiveResultSets=True;";

//可以在EF 中打印执行的sql
DbContext.Database.Log


//EF core 中 打印执行的sql
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite(strConn);
        optionsBuilder.UseLazyLoadingProxies(false);
//方法1
      //  optionsBuilder.LogTo(message => Console.WriteLine(message));
//方法2
    	optionsBuilder.LogTo(Console.WriteLine, LogLevel.Information)
        .EnableSensitiveDataLogging() //实际参数也会打印出来
        .EnableDetailedErrors();  //详细的异常报告
    }

关于.NET CORE

1,接口接受Form 表单所有参数 IFormCollection

关于 运算符号

位与运算 (&)
位与运算符"&"
对两个操作数的对应位进行逻辑与操作。如果两位都是1,则结果位上是1;否则结果位上是0。

位或运算 (|)
位或运算符"|"
对两个操作数的对应位进行逻辑或操作。如果两位中至少有一位是1,则结果位上是1;如果两位都是0,则结果位上是0。

异或运算(XOR)
也是位运算的一种,它使用位运算符"^"。当对两个二进制位进行异或运算时,如果这两个位不同,则结果为1;如果这两个位相同,则结果为0。

浏览器数据储存方式
localStorage
sessionStorage

asp .net core mysql 配置

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySql(
                ConnectionString,
                //ServerVersion.AutoDetect(ConnectionString),
                new MySqlServerVersion(new Version(5, 7, 40)),
                p =>
                {
                    p.EnableRetryOnFailure();
                    p.CommandTimeout(GlobalContext.SystemConfig.DBCommandTimeout);
                    p.MinBatchSize(1);
                    p.MaxBatchSize(100);
                });
            optionsBuilder.AddInterceptors(new DbCommandCustomInterceptor());
            //optionsBuilder.UseLoggerFactory(_loggerFactory);
            // 这里需要注意,不能采用这种写法:optionsBuilder.UseLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole()));
            // 会导致内存泄露的问题 
        }


ASP.NET CORE服务启动时

实现 : IHostedService后,
注入ICO,   services.AddHostedService<实现IHostedService的类>();
可实现服务启动与注册是触发方法

获取主机地址与端口

string str = (Request.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString() + ":" + Request.HttpContext.Connection.LocalPort);

404时代码跳转指定页

  app.UseStatusCodePagesWithReExecute("/home/NotFound");

ASP .NET CORE,,webapi 跨域

 services.AddCors(policy =>
            {
                policy.AddPolicy("CorsPolicy", opt => opt
                .AllowAnyOrigin()
                .AllowAnyHeader()
                .AllowAnyMethod()
                .WithExposedHeaders("X-Pagination"));
            });
   app.UseCors("CorsPolicy");

ASP .NET CORE,检测http状态码

//访问出错时,重定向到登录页

 app.UseStatusCodePages(
        async options =>
        {
            if (options.HttpContext.Response.StatusCode == 404)
            {
                #region 是否文件访问

                string reqUrl = options.HttpContext.Request.GetDisplayUrl();
                int index = reqUrl.LastIndexOf("/");
                string subReqUrl = reqUrl.Substring(index, reqUrl.Length - index);

                #endregion
                if (!subReqUrl.Contains("."))
                    options.HttpContext.Response.Redirect("/Home/LoginOff");
            }
        }
    );

ASP .NET CORE,,http post 访问

  services.AddMvc(option =>
            {
                //允许http post 访问 ,忽略防伪令牌属性
                option.Filters.Add(new IgnoreAntiforgeryTokenAttribute());
            });

wpf

&#xe6df;
\ue66d

wpf 跨程序集引用资源

//  pack://application:,,, 固定写法
//	/gg.DigitaPlatform.Assets; 程序集作用域
//  component/ 固定写法
//  Fonts/#iconfont 具体路径
  <FontFamily x:Key="Iconfont">pack://application:,,,/gg.DigitaPlatform.Assets;component/Fonts/#iconfont</FontFamily>
  <FontFamily x:Key="DigitalDisplay">pack://application:,,,/gg.DigitaPlatform.Assets;component/Fonts/#Digital Display</FontFamily>

WPF 找上级上下文

IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"
Command="{Binding DataContext.OpenViewCommand, RelativeSource={RelativeSource AncestorType=Window}}"
Command="{Binding DataContext.OpenViewCommand, RelativeSource={RelativeSource Mode=FindAncestor ,AncestorType=Window}}"
Background="{TemplateBinding Background}" //与父级相同
   ItemsSource="{Binding DataContext.PropOptions, ElementName=win}" //win 为其他元素的name

wpf 判断是否处于debug

System.Diagnostics.Debugger.IsAttached

WPF跨线程

    DispatcherHelper.Initialize(); //程序启动时,初始化 
   Dispatcher.Invoke((Action)delegate {});

 // 更新 UI
        DispatcherHelper.CheckBeginInvokeOnUI(() =>
        {
            // 这里的代码会在 UI 线程上执行
            Console.WriteLine("UI updated on the UI thread.");
        });

取消执行工具类

CancellationTokenSource

ABPVnext Autofac 生命周期设置

1,接口设置

ISingletonDependency: 注册生命周期为Singleton的服务
IScopedDependency: 注册生命周期为Scoped的服务
ITransientDependency: 注册生命周期为Transient的服务
IAutoFireDependency: 自动触发(与ISingletonDependency、IScopedDependency、ITransientDependency结合使用,在服务自动注册结束后触发一次获取服务操作,仅继承IAutoFireDependency不起作用)

2,特性设置

打开文件夹

SaveFileDialog 、OpenFileDialog、FolderBrowserDialog

两个对象集合,找出id相同的 交集与差集,用C#代码举例

 List<Person> persons = new List<Person>()
        {
            new Person() { Id = 1, Name = "Alice" },
            new Person() { Id = 2, Name = "Bob" },
            new Person() { Id = 3, Name = "Charlie" },
            new Person() { Id = 4, Name = "David" },
            new Person() { Id = 5, Name = "Ella" },
        };

        List<Student> students = new List<Student>()
        {
            new Student() { Id = 1, Age = "20" },
            new Student() { Id = 2, Age = "24" },
            new Student() { Id = 6, Age = "22" },
            new Student() { Id = 7, Age = "21" },
            new Student() { Id = 8, Age = "23" },
        };
  // 交集
        var intersection = persons.Join(students, p => p.Id, s => s.Id, (p, s) => p);
        
        // 差集
        var difference = persons.Except(persons.Where(p => students.Any(s => s.Id == p.Id)), new PersonComparer());

public class PersonComparer : IEqualityComparer<Person>
{
    public bool Equals(Person x, Person y)
    {
        return x.Id == y.Id;
    }

    public int GetHashCode(Person obj)
    {
        return obj.Id.GetHashCode();
    }
}

MD5值

NETCore.Encrypt.EncryptProvider.Md5("");

4 C# 与 C++

1 获取指针

   IntPtr pointer = Marshal.UnsafeAddrOfPinnedArrayElement(array, 2);
   Marshal.FreeHGlobal(pointer );

2,获取指针并释放

  var argbHandle = GCHandle.Alloc(argb, GCHandleType.Pinned);
            IntPtr argbPtr = argbHandle.AddrOfPinnedObject();
              argbHandle.Free();//释放资源
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值