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
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
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);
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.