c# c# .net-core .net-core netnr 发布于:1个月前 更新于:3天前 86

项目多框架判断引用

<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


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

回复
昵称
邮箱
链接
ˆ