csharp csharp database database linux linux netnr 发布于:2020年12月03日 更新于:25天前 448

项目多框架判断引用

<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 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


NPOI(操作 Excel)

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


以管理员(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/


委托(回调)

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


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

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

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


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

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


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


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


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

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

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

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


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


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

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

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


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