You are on page 1of 3

lu v dc nh bng C# Ti xin gii thiu vi cc bn v cch chng ta ghi v c vi kiu d liu Image trong Sql Server vi ngn ng C# .

Bt u d n Bn to 1 d n bn to 1 Form vi cc Controls nh sau 1 Button vi tn bt_addpicture lu nh vo database 1 Button vi tn bt_Dispaly hin th nh 1 ListBox cha tn nh 1 PictureBox hin th nh vi ch Auto Size Chui kt ni : String connectionstring = @Server=(local),uid=sa;database=Northwind;Lu bn cn thay i li chui kt ni ca bn cho ph hp Cu lnh to bng Picture string cmd = "create table Picture" + "(" + "PictureID int IDENTITY(0,1) NOT NULL PRIMARY KEY, " + "PictureName varchar(40) NOT NULL, " + "Picture Image NOT NULL" + ");"; SqlConnection c = new SqlConnection(c_string); SqlCommand cm = new SqlCommand(cmd,c); try { c.Open(); cm.ExecuteNonQuery(); } catch (SqlException ee) { MessageBox.Show(ee.Message); } finally { c.Close(); }Tip theo chng ta thc hin insert Image vo trong databse. Ta vit code cho Event bt_add_Click nh sau: Khi thc hin Click vo bt _add th hin th 1 OpenDileDialog cho php chng ta la chn nh. OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "All Pictures|*.bmp;*.gif;*.jpg|Bitmaps|*.bmp|GIFs|*.gi f|JPEGs|*.jpg"; if ( dlg.ShowDialog() ==DialogResult.OK ) { pictureBox1.Image = new Bitmap(dlg.FileName); string name=dlg.FileName.Substring(dlg.FileName.LastIndex Of(@"\")+1 ,dlg.FileName.Length-dlg.FileName.LastIndexOf(@"\")-1); } Tip theo chng ta to 1 i tng MemoryStream c Image thnh byte[]

MemoryStream mstr = new MemoryStream(); pictureBox1.Image.Save(mstr, pictureBox1.Image.RawFormat); byte[] arrImage = mstr.GetBuffer();Chui cu lnh insert image vo database, vi 2 tham s @PName, @Pic string cmd = "insert into Picture (PictureName, Picture) values (@PName, @Pic)";Tip theo chng ta thc hin Insert Image v database ng thi Add tn Image ny ln ListBox listImageName SqlConnection c = new SqlConnection(c_string); // to i tng kt ni mi SqlCommand comm = new SqlCommand(cmd,c); comm.Parameters.Add( new SqlParameter("@PName", SqlDbType.VarChar,40)).Value = name; comm.Parameters.Add( new SqlParameter("@Pic",SqlDbType.Image)).Value = arrImage; try { c.Open(); comm.ExecuteNonQuery(); } catch(SqlException err) { MessageBox.Show(err.Message); } finally { c.Close(); } listImageName.Items.Add(name); thc hin xong qu trnh lu nh vo database. Vic tip theo chng ta phi lm l c Image t Database, chng ta vit Code cho Event Click bt_Dispaly nh sau: if (listImageName.SelectedIndex<0) { MessageBox.Show("Please select a picture from the ListBox"); } else { string cmd = "SELECT picture FROM Picture where PictureID="+ listImageName.SelectedIndex +";"; SqlConnection cc = new SqlConnection(c_string); SqlCommand com = new SqlCommand(cmd,cc); try { cc.Open(); byte[] b = (byte [])com.ExecuteScalar(); // c d liu kiu byte MemoryStream mem = new MemoryStream(b); pictureBox1.Image = Image.FromStream(mem); }

catch (Exception ee) { MessageBox.Show(ee.Message); } finally { cc.Close(); }Lu vic chy li Chng trnh ln 2 s pht sinh ra li khi thc hin cu lnh to bng v bng Picture tn ti. Nn bn c th to trc trong Database Sql Server. Hoc bn cng c th tham kho thm on code sau, n dng xa 1 bng trong Databse. string cmd = "drop table Picture;"; SqlConnection c = new SqlConnection(c_string); SqlCommand cm = new SqlCommand(cmd,c); try { c.Open(); cm.ExecuteNonQuery(); } catch (SqlException ee) { MessageBox.Show(ee.Message);. Ct li trong bi ny l chng ta c th hc c cch lu v c nh trc tip vo c s d liu, ch khng phi mt cng lu ng dn ca n nh 1 s bn vn lm na. Chc cc bn thnh cng Code

You might also like