| | |
Save and Retrive Image
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
0
#11 Oct 22nd, 2009
A lot of people are against storing binary data in an SQL Server. "Dont use it is a file system!". I personally don't share those opinions and often will store images in an SQL database when I can't ensure a UNC path will be accessible to all machines running the application.
So once you answer DdoubleD's question about storing the image inside of the database we should be better able to help you.
So once you answer DdoubleD's question about storing the image inside of the database we should be better able to help you.
•
•
Join Date: Sep 2009
Posts: 14
Reputation:
Solved Threads: 0
0
#12 Oct 23rd, 2009
sknake: Now all i want to do is to retrieve the filename from the database and add it to my path to a folder. I am not sure I want to go through the long process of trying to retrieve the image from the database. So i need help with executing the query after successfully retrieving the filename from the database and then assign it to my picturebox Memberpics.
0
#13 Oct 23rd, 2009
Try something like this. You need to use a "Where" clause in your SQL Query so you don't select every row since you probably only want the image for a particular user. Here is the test table I created:
And the code:
sql Syntax (Toggle Plain Text)
--IF OBJECT_ID('MemberInfo', 'U') IS NOT NULL DROP TABLE MemberInfo CREATE TABLE MemberInfo ( MemberId INT PRIMARY KEY, MemberSname VARCHAR(50), MemberFName VARCHAR(50), Picture VARCHAR(50), FName VARCHAR(50) )
And the code:
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Data.SqlClient; namespace daniweb { public partial class frmInsertImage : Form { const string connStr = @"Data Source=apex2006sql;Initial Catalog=ServManIRC;Integrated Security=True;"; public frmInsertImage() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { using (OpenFileDialog openFD = new OpenFileDialog()) { openFD.Title = "Insert an image "; openFD.InitialDirectory = "c:"; openFD.FileName = ""; openFD.Filter = "JPEG Image|*.jpg|GIF Image|*.gif|PNG Image|*.png"; openFD.Multiselect = false; if (openFD.ShowDialog() != DialogResult.OK) return; const string new_dir = @"C:\pictures\"; //I use C, not D string fName = System.IO.Path.Combine(new_dir, System.IO.Path.GetFileName(openFD.FileName)); if (File.Exists(fName)) File.Delete(fName); System.IO.File.Copy(openFD.FileName, fName); string msg = string.Format("Copied {0} to {1}", openFD.FileName, fName); MessageBox.Show(msg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); //Insert into database int memberId = 1; string membersname = "knake"; string memberfname = "scott"; string picture = "my profile picture"; InsertUpdateMemberInfo(memberId, membersname, memberfname, picture, fName); } } private void button2_Click(object sender, EventArgs e) { pictureBox1.Image = GetMemberImage(1); } private static void InsertUpdateMemberInfo(int MemberId, string MemberSname, string MemberFName, string Picture, string FName) { List<string> Sql = new List<string>(); Sql.Add("IF EXISTS (Select * From MemberInfo (NOLOCK) Where MemberId = @MemberId)"); Sql.Add("BEGIN"); Sql.Add(" Update MemberInfo"); Sql.Add(" Set "); Sql.Add(" MemberSname = @MemberSname,"); Sql.Add(" MemberFName = @MemberFName,"); Sql.Add(" Picture = @Picture,"); Sql.Add(" FName = @FName"); Sql.Add(" Where MemberId = @MemberId"); Sql.Add(" Select @MemberId"); Sql.Add("END ELSE"); Sql.Add("BEGIN"); Sql.Add(" Insert Into MemberInfo (MemberId, MemberSname, MemberFName, Picture, FName) Values (@MemberId, @MemberSname, @MemberFName, @Picture, @FName)"); Sql.Add(" Select Cast(SCOPE_IDENTITY() as int)"); Sql.Add("END"); string query = GetText(Sql); using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.Parameters.Add(new SqlParameter("@MemberId", SqlDbType.Int)).Value = MemberId; cmd.Parameters.Add(new SqlParameter("@MemberSname", SqlDbType.VarChar)).Value = MemberSname; cmd.Parameters.Add(new SqlParameter("@MemberFName", SqlDbType.VarChar)).Value = MemberFName; cmd.Parameters.Add(new SqlParameter("@Picture", SqlDbType.VarChar)).Value = Picture; cmd.Parameters.Add(new SqlParameter("@FName", SqlDbType.VarChar)).Value = FName; cmd.ExecuteNonQuery(); } } } private static string GetText(List<string> Sql) { StringBuilder sb = new StringBuilder(); foreach (string s in Sql) sb.AppendLine(s); return sb.ToString().Trim(); } private static string GetMemberFilePath(int memberid) { const string query = @"Select FName From MemberInfo Where MemberId = @MemberId"; using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.Parameters.Add(new SqlParameter("@MemberId", SqlDbType.Int)).Value = memberid; string result = Convert.ToString(cmd.ExecuteScalar()); return result; } } } private static Image GetMemberImage(int memberid) { string fileName = GetMemberFilePath(memberid); try { if (string.IsNullOrEmpty(fileName) || !File.Exists(fileName)) { return null; } } catch (IOException) { return null; } return Image.FromFile(fileName); } } }
•
•
Join Date: Sep 2009
Posts: 14
Reputation:
Solved Threads: 0
0
#14 Oct 23rd, 2009
Below is the code to read from the database into the controls i have on the form. everything is reading except the image. For that i get "Illegal characters in path" exception. can anyone help with the reader for the image?
I just want to read the filename and assign it to the path
C# Syntax (Toggle Plain Text)
public void loaddetails() { MySqlDataReader reader = null; try { accessDB.connection(); //this.reset(); String search = "SELECT memberSname,memberFname,memberOname,membersex,fileName FROM memberInfo WHERE memberid = '" + txtmid.Text + "'"; accessDB.myCmd(search).ExecuteNonQuery(); reader = accessDB.myCmd(search).ExecuteReader(); reader.Read(); //while () //{ txtmSname.Text = reader["memberSname"].ToString(); txtFname.Text = reader["memberFname"].ToString(); txtmOname.Text = reader["memberOname"].ToString(); dateDOB.Text = reader["memberDOB"].ToString(); cmbGender.Text = reader["membersex"].ToString(); string img = reader["fileName"].ToString(); try { String path = ("d:\\Pictures\\" + img + ".jpg"); path = path.Trim(Path.GetInvalidFileNameChars()); path = path.Trim(Path.GetInvalidPathChars()); MemberPics.Image = Image.FromFile(path); } catch (Exception ex) { MessageBox.Show("Sorry, the pics could not be loaded " + ex.ToString()); } reader.Close();
I just want to read the filename and assign it to the path
0
#15 Oct 23rd, 2009
Then you have illegal characters in the path of your filename, ie you probably inserted the wrong data in the table.
"C:\@$1298035@!#@%%%__@(\filename.txt" <-- illegal characters in path
"C:\@$1298035@!#@%%%__@(\filename.txt" <-- illegal characters in path
•
•
Join Date: Jul 2009
Posts: 908
Reputation:
Solved Threads: 145
0
#17 Oct 23rd, 2009
Post your current code, indicating the line you get the error, and also indicate the values being used in the call.
•
•
Join Date: Sep 2009
Posts: 14
Reputation:
Solved Threads: 0
0
#18 Oct 23rd, 2009
C# Syntax (Toggle Plain Text)
1. public void loaddetails() 2. { 3. 4. MySqlDataReader reader = null; 5. 6. try 7. { 8. accessDB.connection(); 9. //this.reset(); 10. 11. String search = "SELECT memberSname,memberFname,memberOname,membersex,fileName FROM memberInfo WHERE memberid = '" + txtmid.Text + "'"; 12. 13. accessDB.myCmd(search).ExecuteNonQuery(); 14. 15. reader = accessDB.myCmd(search).ExecuteReader(); 16. 17. reader.Read(); 18. //while () 19. //{ 20. txtmSname.Text = reader["memberSname"].ToString(); 21. txtFname.Text = reader["memberFname"].ToString(); 22. txtmOname.Text = reader["memberOname"].ToString(); 23. dateDOB.Text = reader["memberDOB"].ToString(); 24. cmbGender.Text = reader["membersex"].ToString(); 25. string img = reader["fileName"].ToString(); 26. 27. try 28. { 29. String path = ("d:\\Pictures\\" + img + ".jpg"); 30. path = path.Trim(Path.GetInvalidFileNameChars()); 31. path = path.Trim(Path.GetInvalidPathChars()); 32. MemberPics.Image = Image.FromFile(path); 33. } 34. catch (Exception ex) 35. { 36. 37. MessageBox.Show("Sorry, the pics could not be loaded " + ex.ToString()); 38. } 39. 40. reader.Close();
Above is the code and the error is in line 32: Illegal character in path
•
•
Join Date: Sep 2009
Posts: 14
Reputation:
Solved Threads: 0
0
#20 Oct 23rd, 2009
After running the code, I see path = "d:\\Pictures\\DA\0\0\0\0\0\0\0\0\0\0\0.jpg" instend of d:\\Pictures\\DA.jpg. Can someone help me to trim the unwanted characters from the path cos the fileName is DA so the path should read d:\\Pictures\DA.jpg. And I see only DA in the table not DA\0\0\0\0\0\0\0\0\0\0\0.
![]() |
Similar Threads
- how to retrive image file from mysql databse using php (PHP)
- How to store a byte array in a database and retrive it back as byte array? (C#)
- How to Save data and image in a MSAccess Database using vb.net (VB.NET)
- save GDI+ image into sql 2005 database (C#)
Other Threads in the C# Forum
- Previous Thread: how to filter strange characters?
- Next Thread: SQL querry Combo Box Question
| Thread Tools | Search this Thread |
animation api applet array back backup bmp broken button byte clone cloning code directory disk displayimageinsteadofflash drive file flash gdi ghost google hard header hosting image images java jpanel jpeg link mediawiki method multimedia mysql open panel php picture picturebox problem random reading reputationmanagement screen scroll search shot subdomain swf swf. swing transfer upload url web website windows







