【发布时间】:2018-08-27 23:47:00
【问题描述】:
我通过将文件转换为字节将其保存在数据库中。已成功保存,但无法找回。
这是我用来转换和保存的代码:
public static void databaseFilePut(string varFilePath)
{
byte[] file;
using (var stream = new FileStream(varFilePath, FileMode.Open, FileAccess.Read))
{
using (var reader = new BinaryReader(stream))
{
file = reader.ReadBytes((int)stream.Length);
}
}
using (SqlConnection varConnection = new SqlConnection("Data Source=FARHANB;Initial Catalog=save;Integrated Security=True"))
using (var sqlWrite = new SqlCommand("INSERT INTO Raporty (RaportPlik) Values(@File)", varConnection))
{
varConnection.Open();
sqlWrite.Parameters.Add("@File", SqlDbType.VarBinary, file.Length).Value = file;
sqlWrite.ExecuteNonQuery();
varConnection.Close();
}
}
我正在使用此代码从数据库中检索它:
public static void databaseFileRead(string varID, string varPathToNewLocation)
{
using (SqlConnection varConnection = new SqlConnection("Data Source=FARHANB;Initial Catalog=save;Integrated Security=True"))
using (var sqlQuery = new SqlCommand(@"SELECT [RaportPlik] FROM [dbo].[Raporty] WHERE [RaportID] = @varID", varConnection))
{
varConnection.Open();
sqlQuery.Parameters.AddWithValue("@varID", varID);
using (var sqlQueryResult = sqlQuery.ExecuteReader())
if (sqlQueryResult != null)
{
sqlQueryResult.Read();
var blob = new Byte[(sqlQueryResult.GetBytes(0, 0, null, 0, int.MaxValue))];
sqlQueryResult.GetBytes(0, 0, blob, 0, blob.Length);
using (var fs = new FileStream(varPathToNewLocation, FileMode.Create, FileAccess.Write))
fs.Write(blob, 0, blob.Length);
varConnection.Close();
}
}
}
或
public static void databaseFileRead(string varID, string varPathToNewLocation)
{
using (SqlConnection varConnection = new SqlConnection("Data Source=FARHANB;Initial Catalog=save;Integrated Security=True"))
using (var sqlQuery = new SqlCommand(@"SELECT [RaportPlik] FROM [dbo].[Raporty] WHERE [RaportID] = @varID", varConnection))
{
varConnection.Open();
sqlQuery.Parameters.AddWithValue("@varID", varID);
using (var sqlQueryResult = sqlQuery.ExecuteReader())
if (sqlQueryResult != null)
{
sqlQueryResult.Read();
var blob = new Byte[(sqlQueryResult.GetBytes(0, 0, null, 0, int.MaxValue))];
sqlQueryResult.GetBytes(0, 0, blob, 0, blob.Length);
using (var fs = new FileStream(varPathToNewLocation, FileMode.Create, FileAccess.Write))
fs.Write(blob, 0, blob.Length);
}
varConnection.Close();
}
}
我已经一一使用了这两个代码块,但它们都不起作用。
Link One(Original) Link Two(Modification)
首先,我遇到了问题
using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))
所以我把它改成了sqlconnection。它正在成功保存字节,所以我认为这里没有问题。
我遇到的第二个问题是“访问被拒绝”。我收到拒绝访问错误,所以我通过将文件保存在 bin 项目中来解决它。
现在没有错误但指定文件夹中也没有文件。
这是我在方法中传递的引用。
databaseFileRead("1",@"\here");
我该怎么办?请指导我,我是数据库新手。
谢谢
【问题讨论】:
-
but none of them is working.什么具体不起作用?First of all, I got the problem in具体是什么问题?Second problem i got was, Access denied. I was getting access denied error so i solved it by saving the file in the bin project.哪一行代码抛出了异常?Now there is no error but there is also no file in the specified folder.指定的文件夹是什么? -
我建议确保
varPathToNewLocation包含一个完全限定路径。 -
@mjwills 我发布了两种几乎相同的方法,我从两个不同的来源获得它们,但它们都不起作用。
-
听起来不像是 sql 问题 - 纯粹是流写入器/访问/路径问题。认为问题的标题是错误的。
-
@mjwills 谢谢你是对的,这是路径的问题。我解决了:)
标签: c# sql-server database access-denied bytestowrite