首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:区域设置。如何修复此错误?

错误:区域设置。如何修复此错误?
EN

Stack Overflow用户
提问于 2020-02-10 14:57:08
回答 1查看 57关注 0票数 0

我正在尝试将varbinarymax列保存到数据库中。为此,我有以下代码:

代码语言:javascript
复制
    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”在当前上下文中不存在。请告诉我如何解决这个问题。我已经使用了所有的命名空间。我试着找,但什么也找不到。请帮帮忙。

EN

回答 1

Stack Overflow用户

发布于 2020-02-10 16:34:43

为了了解本例中的名称Locale使用的库或命名空间的类型,还应提供该库的名称。

但是,您可以使用推荐的方法在应用程序中实例化SQL连接。为此,您可以使用以下代码:

代码语言:javascript
复制
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实例化连接:中的文档也会很有帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60145436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档