csharp csharp database database netnr 发布于:2020年12月03日 更新于:18天前 239

项目多框架判断引用

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