Asp.net 错误:区域设置。如何修复此错误?
我试图在数据库中保存varbinary[max]列。为此,我有以下代码:Asp.net 错误:区域设置。如何修复此错误?,asp.net,varbinary,varbinarymax,Asp.net,Varbinary,Varbinarymax,我试图在数据库中保存varbinary[max]列。为此,我有以下代码: public static void databaseFilePut(string varFilePath) { byte[] file; using (var stream = new FileStream(varFilePath, FileMode.Open, FileAccess.Read)) { using (var reade
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 (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))
using (var sqlWrite = new SqlCommand("INSERT INTO tblFiles2 (Data) Values(@File)",
varConnection))
{
sqlWrite.Parameters.Add("@File", SqlDbType.VarBinary, file.Length).Value = file;
sqlWrite.ExecuteNonQuery();
}
}
这给了我错误。当前上下文中不存在名称“locale”。请告诉我如何解决这个问题。我已经使用了所有的名称空间。我试着找,但找不到任何东西。请提供帮助。为了了解名称区域设置在您的案例中使用的是什么类型的库或名称空间,您还应该提供此库的名称 但是,您可以使用推荐的方法在应用程序中建立SQL连接。为此,您可以使用以下代码:
static private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file, using the
// System.Configuration.ConfigurationSettings.AppSettings property
return "Data Source=(local);Initial Catalog=AdventureWorks;"
+ "Integrated Security=SSPI;";
}
public static void databaseFilePut(string varFilePath)
{
string connectionString = GetConnectionString();
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 connection = new SqlConnection(connectionString))
using (var sqlWrite = new SqlCommand("INSERT INTO tblFiles2 (Data) Values(@File)",
connection))
{
conn.Open();
sqlWrite.Parameters.Add("@File", SqlDbType.VarBinary, file.Length).Value = file;
sqlWrite.ExecuteNonQuery();
}
}
此外,查看通过asp.net实例化连接的文档也会有所帮助: