gpt4 book ai didi

c# - 如何在 SQL Server 中保存包含汉字的文件名 - Varchar(100)?

转载 作者:行者123 更新时间:2023-12-02 17:24:53 24 4
gpt4 key购买 nike

我的文件名是这样的:一个例子.pdf

我想将文件名保存在 SQL Server 表中并从 SQL Server 表中读取它。你是怎么做到的?

这是代码:

class Bestand
{
public void Add(string filePath, DateTime fileDate, string fileName = "")
{
Bestand bestand = this.CurrentBestand;

if (filePath.Length > 0 && CanReadFile(filePath))
{
Binary bin = new Binary();
bin.Data = new System.Data.Linq.Binary(File.ReadAllBytes(filePath));

bestand.BestandsDatum = fileDate;
bestand.BestandsNaam = String.IsNullOrEmpty(fileName) ? Path.GetFileName(filePath) : Encoding.UTF8.GetBytes(fileName)[0].ToString();

bestand.Binary = bin;
}
}

public void Save(string filePath)
{
byte[] buffer = Data.ToArray();
System.IO.File.WriteAllBytes(filePath, buffer);
}

}

并调用它来保存文件:

documents[0].Add(beFile.Value, dtpDate.Value);

并调用它来打开文件:

public static void ViewBestand(IBestand bestand)
{
string orgFilepath = Path.Combine(TempDocumentFolder, bestand.FileName);
string filepath = orgFilepath;

int tmpCounter = 0;
while (File.Exists(filepath) && tmpCounter < 100)
{
tmpCounter++;
filepath = Path.Combine(TempDocumentFolder, Path.GetFileNameWithoutExtension(orgFilepath) + "_" + tmpCounter.ToString() + Path.GetExtension(orgFilepath));
}

bestand.Save(filepath);
ViewFile(filepath);
}

最佳答案

将列类型更改为 nvarchar(100)varchar 无法存储 Unicode 字符,因为它是单字节,Unicode 是双字节。

您可以阅读更多内容 here .

关于c# - 如何在 SQL Server 中保存包含汉字的文件名 - Varchar(100)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46565156/

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