gpt4 book ai didi

c# - 是否可以将大字符串写入 Firebird blob?

转载 作者:行者123 更新时间:2023-11-30 21:10:19 24 4
gpt4 key购买 nike

Firebird 的文档暗示您可以将大(> 60K)字符串写入表中的 blob 值。所以如果你有这个:

CREATE TABLE MyBlobTable (
theId int PRIMARY KEY NOT NULL,
theBlob BLOB SUB_TYPE 1
)

那么这应该可以工作:

insert into MyBlobTable (theId, theBlob) values (1, '[60K characters in a string]')

(受 http://web.firebirdsql.org/dotnetfirebird/blob-sub_type-1-reading-example-csharp.html 启发的示例)

但是我发现C#驱动和FlameRobin都不能写入这个值。您得到“意外的命令结束”(指向字符串中大约 32K 的位置,这有点可疑)

我认为有一种特殊的方式来引用或转义数据值,或者可能是此 Java 代码 (http://www.firebirdfaq.org/faq372/) 的 C# 等价物,其中二进制文件直接读入陈述。我没有对文本数据做任何花哨的事情,所以如果需要,我愿意将其存储为二进制 blob。

谢谢!

更新:“参数化查询”是我正在寻找的短语。我在做什么:

FbParameter param = new FbParameter("@blobVal", FbDbType.Text);
param.Value = myLargeString;
String query = "insert into MyBlobTable (theId, theBlob) values (1, @blobVal)";
using (FbConnection conn = [something from my pool]) {
using (FbCommand cmd = new FbCommand(query, conn)) {
cmd.Parameters.Add(param);
cmd.ExecuteNonQuery();
}
}

最佳答案

您正在查询中添加内联文本。然后是限制:Firebird 2.5 及更早版本中的第一个查询文本限制为 64 KB,在 Firebird 3.0 中增加到 10 MB,但某些工具可能仍会以将其限制为 64KB 的方式使用 API。字符串文字大小也有限制:32KB,或者如果它是 blob 值的文字,则从 Firebird 3.0 开始为 64KB。

如果您想向 blob 添加更多数据,则需要使用参数化查询将其流式传输到 blob。参见 this example :

public static void Main(string[] args) {
// Set the ServerType to 1 for connect to the embedded server
string connectionString =
"User=SYSDBA;" +
"Password=masterkey;" +
"Database=SampleDatabase.fdb;" +
"DataSource=localhost;" +
"Port=3050;" +
"Dialect=3;" +
"Charset=NONE;" +
"Role=;" +
"Connection lifetime=15;" +
"Pooling=true;" +
"Packet Size=8192;" +
"ServerType=0";

FbConnection myConnection = new FbConnection(connectionString);
myConnection.Open();

FbTransaction myTransaction = myConnection.BeginTransaction();

FbCommand myCommand = new FbCommand();

myCommand.CommandText =
"UPDATE TEST_TABLE_01 SET CLOB_FIELD = @CLOB_FIELD WHERE INT_FIELD = @INT_FIELD";
myCommand.Connection = myConnection;
myCommand.Transaction = myTransaction;

myCommand.Parameters.Add("@INT_FIELD", FbType.Integer, "INT_FIELD");
myCommand.Parameters.Add("@CLOB_FIELD", FbType.Text, "CLOB_FIELD");

myCommand.Parameters[0].Value = 1;
myCommand.Parameters[1].Value = GetFileContents(@"GDS.CS");

// Execute Update
myCommand.ExecuteNonQuery();

// Commit changes
myTransaction.Commit();

// Free command resources in Firebird Server
myCommand.Dispose();

// Close connection
myConnection.Close();
}

public static string GetFileContents(string fileName) {
StreamReader reader = new StreamReader(new FileStream(fileName, FileMode.Open));
string contents = reader.ReadToEnd();
reader.Close();
return contents;
}

关于c# - 是否可以将大字符串写入 Firebird blob?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8553421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com