netnr 2020年12月03日 20天前 650 0
0 0

项目多框架判断引用

<ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
    <PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.3.0" />
</ItemGroup>

.NET 关闭全球化运行 System.Globalization.Invariant

Couldn't find a valid ICU package installed on the system

export DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1 # 设置环境变量

上面是通过设置环境变量,下面是添加到项目属性

<PropertyGroup>
    <InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>

https://docs.microsoft.com/zh-cn/dotnet/core/runtime-config/globalization


NPOI(操作 Excel)

依赖 System.Drawing.Common 包,该包在 Linux 上需要 GDI (libgdiplus) 组件


Visual Studio 修改 NuGet 包路径

%AppData%\NuGet\NuGet.Config 创建或编辑配置文件,配置以下信息

<configuration>
  <config>
    <add key="globalPackagesFolder" value="E:\package\nuget" />
  </config>
</configuration>

参考:https://www.cnblogs.com/RainFate/p/12836146.html


MySQL 字符串换行拼接

SELECT concat ('1', CHAR (10), '2')

MySQL GROUP_CONCAT 分组拼接方法设置最大长度,会话有效

SET SESSION group_concat_max_len = 1000000;

MySqlBulkLoader 批量写入配置

开启允许本地导入数据的配置

SET GLOBAL local_infile=1;  --  1 表示开启,0 表示关闭

SHOW VARIABLES LIKE '%local%';  -- 查看配置

连接字符串追加 AllowLoadLocalInfile=true
数据库和应用不在同一台服务器需要设置 MySqlBulkLoaderLocal = true


PowerDesigner MySQL 生成脚本去除 national 关键字

打开工具栏:Database -> Edit Current DBMS

Profile\Column\Extended Attributes\National  -- 勾选 Computed √

.NET 5.0 Oracle ODP.NET 连接失败(2020-12-03)

BinaryFormatter serialization and deserialization are disabled within this application

暂时解决办法(等 Oracle 更新会修复)

<PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>

https://stackoverflow.com/questions/64790197/odp-net-driver-throwing-exception-on-net-core-5-0

2021-05-13 更新:Oracle.EntityFrameworkCore v5.21.1 已经修复该问题


Oracle 查询 Long 类型返回空字符串

var dbc = new OracleCommand ();
dbc.InitialLONGFetchSize = -1;

https://stackoverflow.com/questions/3699143/selecting-the-default-value-of-an-oracle-column-in-net


Oracle Empty CLOB IS NOT NULL

using var reader = dbCommand.ExecuteReader (CommandBehavior.KeyInfo);

do
{
    var dt = new DataTable ();

    dt.Load (reader); // ERROR
} while (!reader.IsClosed);

数据载入表报错,换成行读取并处理值

using var reader = dbCommand.ExecuteReader(CommandBehavior.KeyInfo | CommandBehavior.CloseConnection);

do
{
    var dt = DbHelper.ReaderTableSchema(reader); //获取空表结构

    while (reader.Read())
    {
        var dr = dt.NewRow();
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            var col = dt.Columns[i];
            var cellValue = reader[i];

            if (cellValue != DBNull.Value)
            {
                dr[i] = cellValue;
            }
            else if (col.AllowDBNull == false)
            {
                dr[i] = cellValue.ToString();

                //col.AllowDBNull = true;

                /*
                 * 目前遇到 Oracle 的两种情况:
                 * Oracle 的字段约束状态为 Disable
                 * Oracle 不为 Null 的 Empty CLOB
                 */
            }
        }

        dt.Rows.Add(dr.ItemArray);
    }
} while (reader.NextResult());

在 SQL 92 标准中,长度为零的初始化 LOB 传递给 IS NULL 函数时,将返回 FALSE

https://docs.oracle.com/cd/E11882_01/appdev.112/e18294/adlob_sql_semantics.htm#ADLOB45598


以管理员(sys)账号连接 Oracle

Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL)));User Id=sys;Password=oracle;DBA Privilege=SYSDBA;

https://www.connectionstrings.com/oracle/


EF Core 搜索 SQLServer 的 nvarchar (max) 类型报错

不支持使用 Contains 搜索 nvarchar (max) 类型
https://github.com/dotnet/efcore/issues/19327


Microsoft.Data.SqlClient v4.0.0 默认 Encrypt=true

报错提示:证书链是由不受信任的颁发机构颁发的
连接字符串添加 TrustServerCertificate=True;

参考:https://www.cnblogs.com/nethrd/p/10973662.html


SQL Server 2017 (14.x) 递增跳 1000

实例重启(故障)造成,可设置禁用缓存,但自增性能会下降

ALTER DATABASE SCOPED CONFIGURATION SET IDENTITY_CACHE = OFF -- 关闭缓存

-- 查询配置
select * from sys.database_scoped_configurations where name = 'IDENTITY_CACHE'

参考:https://docs.microsoft.com/zh-cn/sql/t-sql/statements/alter-database-scoped-configuration-transact-sql


Npgsql 连接 PostgreSQL 报错 28000 no pg_hba.conf ... SSL off

Host=127.0.0.1;Port=5432;Username=postgres;Password=123;Database=dbname;SslMode=Require;Trust Server Certificate=true;

连接字符串添加 SslMode=Require;Trust Server Certificate=true;

参考:https://stackoverflow.com/questions/44161509


.NET Runtime Optimization Service

占用 CPU,进程杀不死,加速优化执行

cd c:\Windows\Microsoft.NET\Framework\v4.0.30319 
cd c:\Windows\Microsoft.NET\Framework64\v4.0.30319

ngen.exe executequeueditems

ref: https://appuals.com/fix-high-cpu-usage-by-net-runtime-optimization-service/

IIS 部署站点访问失败且停止了应用程序池

有可能是 ASP.NET State Service 服务没有启动,设置为自动启动

有可能是 ** 死循环 ** 造成(事件查看器有错误级别日志:w3wp.exe clr.dll 之类的错误)


JS、CSS 相对路径资源

html 中引用的 js 文件中的各种相对路径是以 html 的 url 为基准路径
css 相对路径是以 css 文件所在路径为基准,而 js 中的路径则是以导入此 js 的网页文件所在的位置为基准

Please login as the user "ubuntu" rather than the user "root"

vi /root/.ssh/authorized_keys # 编辑,去除前面,保留 ssh-rsa 开头

Amazon EC2、Oracle Cloud 允许 root 登录

参考:https://askubuntu.com/questions/1156830


E: Unable to locate package vim

Docker 容器内安装 vim 提示错误的解决办法

apt-get update && apt-get install apt-file -y && apt-file update && apt-get install vim -y

参考:https://unix.stackexchange.com/questions/336392


git config --global --add safe.directory

git config --global --add safe.directory '*' # Linux

git config --global --add safe.directory "*" # Windows

参考:https://stackoverflow.com/questions/71849415


IPv6

八段数字+字母组成
IPv6 非 f 开头为公网 IP
fe80 开头的地址不能上网
电信为 240e 开头的(240e::/20)
移动为 2409 开头的(2409:8000::/20)
联通为 2408 开头的(2408:8000::/20)


Edge 浏览器等待 browser.events.data.msn.com

在 hosts 添加记录如下:

0.0.0.0 pipe.aria.microsoft.com
0.0.0.0 ntp.msn.com
0.0.0.0 c.msn.com
0.0.0.0 web.vortex.data.microsoft.com
0.0.0.0 browser.events.data.msn.com
0.0.0.0 www.msn.com

委托(回调)

using System;

public class Program
{
    public static void Main ()
    {
        Method (1, 2, (sum, str) =>
        {
            Console.WriteLine (sum);
            Console.WriteLine (str);
        });
    }

    public static void Method (int v1, int v2, Action<int, string> actionCallback)
    {
        var sum = v1 + v2;
        actionCallback (sum, "hello world");
    }
}

委托(封装代码,共享部分代码)

/// <summary>
/// 连接包装(无返回值)
/// </summary>
/// <param name="action"></param>
public void SafeConn (Action action)
{
    SafeConn (() => { action (); return 0; });
}

/// <summary>
/// 连接包装(有返回值)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="action"></param>
/// <returns></returns>
public T SafeConn<T>(Func<T> action)
{
    try
    {
        if (Connection.State == ConnectionState.Closed)
        {
            Connection.Open ();
        }

        return action ();
    }
    catch (Exception ex)
    {
        Transaction?.Rollback ();
        Console.WriteLine (ex);
        throw;
    }
    finally
    {
        Transaction?.Dispose ();
        if (Connection.State == ConnectionState.Open)
        {
            Connection.Close ();
        }
    }
}

// 使用
public int SqlExecute (string sql, DbParameter [] parameters = null)
{
    return SafeConn (() =>
    {
        var num = GetCommand (sql, parameters).ExecuteNonQuery ();
        return num;
    });
}

https://stackoverflow.com/questions/6046382/suggestions-for-making-a-reusable-try-catch-block-in-c


链接返回顶部