Database Name: MoviesDirectory
Table Name: picture
Column: img DataType:bytea
First, I store my uploaded images which may be either .jpg,.gif,.png with the following codes:
Expand|Select|Wrap|Line Numbers
- ....
- ....
- ....
- if (FileUpload1.PostedFile.ContentLength != 0)
- {
- byte[] b = new byte[FileUpload1.PostedFile.ContentLength];
- FileUpload1.PostedFile.InputStream.Read(b, 0, b.Length);
- string basestring = System.Convert.ToBase64String(b, 0, b.Length);
- TextBox1.Text = basestring;
- }
- ....
- ...
- ...
- ConnStr = ConfigurationManager.ConnectionStrings["connect2MD"].ToString();
- Conn = new NpgsqlConnection(ConnStr);
- Conn.Open();
- try
- {
- strSQL = new StringBuilder();
- strSQL.Append("INSERT INTO picture(img) VALUES (@img)");
- command = new NpgsqlCommand(strSQL.ToString(), Conn);
- param = new NpgsqlParameter();
- param.ParameterName = "@img";
- param.Value = TextBox1.Text;
- command.Parameters.Add(param);
- command.ExecuteNonQuery();
- }
- catch (Exception ex)
- {
- throw (ex);
- }
- finally
- {
- if (Conn.State == ConnectionState.Open)
- Conn.Close();
- }
- ...
- ...
- ..