通过实体框架选择多行字符串
本文关键字:字符串 选择 框架 实体 | 更新日期: 2023-09-27 17:55:49
我在C#中使用实体框架4(是的,我知道我应该升级,但这不是重点)。
我在数据库中存储了 70,000 个私有 RSA 密钥格式是这样的:注意 - 这是一个演示密钥
-----BEGIN RSA PRIVATE KEY-----
MIICXQIDEMODEMODEMODEMODEMOEbGslt0N21onxRjBUwXFKI7CN6clxPAS+nEUK
BZAEiJUDJLqcGa37EZv40UQnmGKpLZO3ypee9qvKRVC4Q3Oi3S+Ukfet7eJYvCwr
WlneDOqvzyDz8cwlLcE2YhOnCMLYJ3v3YSt0n10oryaKyADDbCd5PmBbUwIEB7CO
+QKBgCtLDEMODEMODEMODEMODEMOnNVbWYfVLhFJpdS1+5dTZbkOg6zaP+A/VKf1w
3psFl7VfbggqiEvAi+4hTULVZu0W0wqPQUkw+rGzVT8Fg1uwBrzUKScJbscuRE/J
1I6iizZ0sudjUynn+EORc12p1NRslf2BUW3nFIognt849r9JAkEA1V+5aGmJLZPP
xZ/ETst7tb1WMeOvNxu+s/h2kcW6+WCvlUErYiB2/E/LkTbYGXu5/OHIMDgbQbB+
28Bu7lYmUwJBAM12343234235AEIDmvGJChsPOaqwnClV/fSEx/6E8DXMhDHTENA
STt+v4k1rlkSlM2aliZ8kz2u8K45SepsVwECQQCmECecqVtfuIvC+8EKvlM536OT
XrLIeDOkRak8yRqQFEZzAlOXkOtKIxHBJuVT39sPK+xzU30FwRdLE858eBphAkB+
LExh7ejTrxN3dHr1mDhJWoLzm59dzgwfmuEznnH+OmMZSAY21IouIAXjnKfXeCLo
fRvUv5EuQkvGUJi1/25JAkAXZy7yjsJxaNBGB9TxakoHHnI2yWI/R9yewhzLorNo
Oj49W6l9uaWaNxxmCrNZRIZoDEMODEMODEMODEMODEMO
-----END RSA PRIVATE KEY-----
注意:由于某种原因,END RSA 行在堆栈溢出上格式不正确。
但是,当我尝试通过实体框架选择密钥时,我只得到以下内容:-----开始 RSA 私钥
-----我相信这可能是SQL Server(在我的例子中是2008)在新行字符(''')处终止字符串因此,我编写了一个存储的过程来尝试替换它在服务器端,然后再返回到调用应用程序。
结果是这样
Declare @str as nvarchar(max)
Declare @strReplaced as nvarchar(max)
set @str = (select PrivateKey from PrivateKeys where Id = @ID)
set @strReplaced = (select (REPLACE(@str, CHAR(10), '<<newline>>')))
select @strReplaced
在sql服务器(存储的过程)上运行时 - 它为我提供了带有适当<>替换的私钥但是,当我通过以下 C# 代码执行存储的过程时,它仍然终止键第一行的结果。
var str = o.ExecuteStoreQuery<string>("Execute dbo.GetPrivateKey " + Id.ToString());
Console.WriteLine(str.ToList()[0]);
有人可以建议一个解决方案来允许我的 C# 代码访问整个 RSA 密钥吗?
在 SQL Server Management Studio 中执行存储的过程时:我得到 -----BEGIN RSA PRIVATE KEY-----<<newline>>MIICXwIBAAKBgQDuCFrUx55g9yqezNvzSpX0YegUDnrp0IP7jsMab0NVqAgl/azO<<newline>>qbp5SNe10mAYJNDsxBS0AHYSOrAHApgKPgWTKOE5gFUKxaB1eJgIDEMODEMODEMODEMO<<newline>>0PU0Kma2/8Znv23JFUDrO3Sk8Ft++aT8crCs45ygnJVNSdgsU0MDEMODEMODEMODEMO<<newline>>awKBgAXv6PqRNvFuEg5Pt+0IjwTMEHLqPBPEh5k2eS1aRYKyl/T40fe5SaXiMxWb<<newline>>f8SUoLXDPdOdkXqBqZfOs94hjtk6mRDZuBN9MWL7F4D+b6/n4s+YDEMODEMODEMODEMO<<newline>>kEr7DrtF/n/kDrH/Pty6pyHCfGLVLMtG6cFYIPncyYzViLTDAkEA+riR/e+elYVe<<newline>>bc+KPq9NlSXAFz21R00puCUJhrVBNcVZnKw2hV8D9Ie4b+WMaPcGh/7gHAiUC84o<<newline>>JpLA8EjGPQJBAPMLZGRwcvNiUW7gl2EBIq33gDZvefzQC4kr5J2SFLaA4lX/wKMJ<<newline>>ymHJdjQlmo5nKd7qdmg0DwbUSOjCcux/RpkCQQDXde/XX9kIxD9NQ2wV0DhQ4LFU<<newline>>J/OXlxZ2yD+Tnr7lFFN/QghU8Vuc0G4eRPR2kg5X6FBya3nQ5miDgl8j76pDAkEA<<newline>>pud2zrTMxBul8BDP4+gqll18jcRt0sV7fERvaMT+0gL/M2QamRevgd22ipKbBAUV<<newline>>P4f1Jtj6aFqVQ9n8RewtuwJBAL4iLMt/GWg71PBQhPlZlkDlmDGoDF1Ou6MYlONv<<newline>>z24JA/EMdx3Jx1/WG7xyzqmEJcVGk/8O/NWtP5DXQEFBLnw=<<newline>>-----END RSA PRIVATE KEY-----<<newline>>
通过 C# 代码执行存储的过程时:'string sConnectionString = "InsertConnectionStringHere"; using (var conn = new System.Data.SqlClient.SqlConnection(sConnectionString)) using (var command = new System.Data.SqlClient.SqlCommand("GetPrivateKey", conn) { 命令类型 = 命令类型.存储过程
})
{
conn.Open();
command.Parameters.Add(new System.Data.SqlClient.SqlParameter("@DomainID", vTransaction.Domain.Id));
var vReader = command.ExecuteReader();
vReader.Read();
var vResult = vReader.GetValue(0);
Console.WriteLine(vResult);
conn.Close();
}`
它给了我-----开始RSA私钥-----无论原因是什么,它都会在低级别影响 SQL。因此,它既会影响直接原始 SQL,也会影响基于它的实体框架。
编辑 - 仔细检查,我使用的是SQL Server Express-它是否有可能影响返回的字符串的限制?
编辑----我修改了存储过程以将文本字段转换为二进制数据,然后将其发送给调用方。通过此代码 select CONVERT(VARBINARY(max), @str )
它可以在SQL Server上运行,但是当通过C#代码远程调用时,当转换为字符串时,它只能获得62字节的数据 - 这些字节再次构造"-----开始RSA私钥-----"
我不知所措...
使用正则表达式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string key =
"-----BEGIN RSA PRIVATE KEY-----'n" +
"MIICXQIDEMODEMODEMODEMODEMOEbGslt0N21onxRjBUwXFKI7CN6clxPAS+nEUK'n" +
"BZAEiJUDJLqcGa37EZv40UQnmGKpLZO3ypee9qvKRVC4Q3Oi3S+Ukfet7eJYvCwr'n" +
"WlneDOqvzyDz8cwlLcE2YhOnCMLYJ3v3YSt0n10oryaKyADDbCd5PmBbUwIEB7CO'n" +
"+QKBgCtLDEMODEMODEMODEMODEMOnNVbWYfVLhFJpdS1+5dTZbkOg6zaP+A/VKf1w'n" +
"3psFl7VfbggqiEvAi+4hTULVZu0W0wqPQUkw+rGzVT8Fg1uwBrzUKScJbscuRE/J'n" +
"1I6iizZ0sudjUynn+EORc12p1NRslf2BUW3nFIognt849r9JAkEA1V+5aGmJLZPP'n" +
"xZ/ETst7tb1WMeOvNxu+s/h2kcW6+WCvlUErYiB2/E/LkTbYGXu5/OHIMDgbQbB+'n" +
"28Bu7lYmUwJBAM12343234235AEIDmvGJChsPOaqwnClV/fSEx/6E8DXMhDHTENA'n" +
"STt+v4k1rlkSlM2aliZ8kz2u8K45SepsVwECQQCmECecqVtfuIvC+8EKvlM536OT'n" +
"XrLIeDOkRak8yRqQFEZzAlOXkOtKIxHBJuVT39sPK+xzU30FwRdLE858eBphAkB+'n" +
"LExh7ejTrxN3dHr1mDhJWoLzm59dzgwfmuEznnH+OmMZSAY21IouIAXjnKfXeCLo'n" +
"fRvUv5EuQkvGUJi1/25JAkAXZy7yjsJxaNBGB9TxakoHHnI2yWI/R9yewhzLorNo'n" +
"Oj49W6l9uaWaNxxmCrNZRIZoDEMODEMODEMODEMODEMO'n" +
"-----END RSA PRIVATE KEY-----'n";
Regex expr = new Regex("-.*$", RegexOptions.Multiline);
key = expr.Replace(key, "").Trim();
}
}
}