0

code to upload and save an image into access database in windows application visual studio.

Please give the detailed code

I know how to do this in web application but i have never used
windows application

2
Contributors
1
Reply
5
Views
8 Years
Discussion Span
Last Post by sknake
0
private static string BuildSqlNativeConnStr(string server, string database)
    {
      return string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;", server, database);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
      byte[] bytes = File.ReadAllBytes(@"C:\picture.bmp");
      const string query = @"Insert Into Picture (Picture) Values (@Picture)";
      using (SqlConnection conn = new SqlConnection(BuildSqlNativeConnStr("apex2006sql", "Bugs")))
      {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand(query, conn))
        {
          SqlParameter parm = new SqlParameter("@Picture", SqlDbType.Image);
          parm.Value = bytes;
          cmd.Parameters.Add(parm);
          cmd.ExecuteNonQuery();
        }
      }
    }

Change the mapping types from SqlDbType to OleDbType and SqlConnection to OleDbConnection .

Here are my access connection string helpers to swap out the sql conn str:

public static string BuildAccessConnectionString(string Filename, string Username, string Password, string DatabasePassword)
    {
      return string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';User Id={1};Password={2};Jet OLEDB:Database Password={3};",
                                   Filename.Replace("'", "''"),
                                   Username,
                                   Password,
                                   DatabasePassword);
    }
    public static string BuildAccess2007ConnectionString(string Filename, string DatabasePassword)
    {
      return string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='{0}';Persist Security Info=False;Jet OLEDB:Database Password={1};",
                                 Filename.Replace("'", "''"),
                                 DatabasePassword);
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.