0

I am trying to save the varbinary[max] column in my database. For this, I have this code below:

    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();
        }
    }

It is giving me error. The name 'locale' does not exist in the current context. Please tell me how can I fix this. I have used all the namespaces. I tried searching but could not find a single thing. Kindly help.

Unknown
  • 145
  • 2
  • 11

1 Answers1

0

In order to understand what type of Library or namespace is used by the name Locale in your case, you should provide also the name of this library.

However, you can use the recommended method of instintiating the SQL connection inside your application. For that you can use below code:

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();
    }
}


Also it will be helpful to see the documentation in instantiating connection through asp.net: Documentation

shrzd
  • 102
  • 2
  • 3
  • 6