1.连接UltraLite数据库.
1: private void Connect()
2: {
3: // This will connect to the UltraLite database and create a ULConn connection object
4: try
5: {
6: String dbf = @"/Program Files/ImageUL/imageul.udb";
7:
8: if (System.IO.File.Exists(dbf))
9: {
10: private ULConnection ConnUL = new ULConnection();
11: ConnUL.ConnectionString = "dbf=" + dbf + ";cache_size=1M";
12: if (ConnUL.State != ConnectionState.Open)
13: {
14: ConnUL.Open();
15: }
16: ConnUL.DatabaseID = 1000;
17: }
18: }
2.从本地路径读取图片.
1: private void menuLoadPicture_Click(object sender, EventArgs e)
2: {
3: // Open a file dialog to choose a file and then load the binary data into byte[] fileData
4:
5: FileStream strm;
6: byte[] fileData = null;
7:
8: OpenFileDialog openFileDialog1 = new OpenFileDialog();
9:
10: openFileDialog1.Filter = "All images|*.bmp *.jpeg *.jpg *.gif *.JPG|All files (*.*)|*.*|JPEG files (*.jpeg)|*.jpeg|JPG files (*.jpg)|*.jpg|Bitmap files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif";
11:
12: if (openFileDialog1.ShowDialog() == DialogResult.OK)
13: {
14: String fname = openFileDialog1.FileName;
15: int n;
16:
17: string fullFileName = fname;
18:
19: // lose the extension
20: n = fname.LastIndexOf('.');
21: if (n >= 0)
22: {
23: fname = fname.Substring(0, n);
24: }
25:
26: // lose the path
27: n = fname.LastIndexOf('//');
28: if (n >= 0)
29: {
30: fname = fname.Substring(n + 1);
31: }
32:
33: myFile = fname;
34:
35: try
36: {
37: strm = new FileStream(fullFileName, System.IO.FileMode.Open);
38:
39:
40: long iLength = strm.Length;
41: fileData = new byte[(int)strm.Length];
42: strm.Read(fileData, 0, (int)strm.Length);
43: strm.Close();
44:
45: // 保存图片到UltraLite数据库
46: bool return_code = SaveImage(fileData, iLength, myFile);
47:
48: //Update the combo box
49: UpdateComboFileName();
50: comboFileName.Text = myFile;
51: ShowPreview();//从数据库读取图片显示到桌面
52: }
53: catch (Exception ex)
54: {
55: MessageBox.Show(ex.Message);
56: return;
57: }
58:
59: ShowPreview();
60: }
61: }
3.保存图片到数据库(UltraLite)
1: public bool SaveImage(byte[] fileData, long imageLen, String filename)
2: {
3: // Take the binary fileData and insert it into the database
4: ULParameter parm;
5: long RowsInserted;
6:
7: try
8: {
9:
10: using (ULCommand cmd = ConnUL.CreateCommand())
11: {
12: cmd.CommandText =
13: "INSERT INTO images(image_name, image_binary) VALUES (?, ?)";
14: cmd.Parameters.Add("image_name", filename);
15:
16: parm = new ULParameter();
17: parm.ULDbType = ULDbType.Binary;
18: parm.Direction = ParameterDirection.Input;
19: parm.Value = fileData;
20: parm.Size = Convert.ToInt32(imageLen);
21: cmd.Parameters.Add(parm);
22:
23: RowsInserted = cmd.ExecuteNonQuery();
24:
25: }
26:
27: }
28: catch (Exception err)
29: {
30: MessageBox.Show("Exception: " + err.Message);
31: }
32:
33:
34: return true;
35:
36: }
4.从数据库中读取图片显示在控件上.
1: private void ShowPreview()
2: {
3: // Based on the selected image filename, select the binary data for the
4: // image from the database and display it
5: try
6: {
7: using (ULCommand cmd = ConnUL.CreateCommand())
8: {
9:
10: cmd.CommandText = "SELECT image_binary from images WHERE image_name = '" + comboFileName.Text +"'";
11: ULDataReader rdr = cmd.ExecuteReader();
12:
13: if (rdr.Read())
14: {
15: byte[] m_image = (byte[])rdr["image_binary"];
16: MemoryStream ms = new MemoryStream(m_image);
17: Bitmap bmap = new Bitmap(ms);
18:
19: pbPreview.Image = (bmap);
20: }
21:
22: rdr.Close();
23: }
24:
25: }
26: catch (Exception ex)
27: {
28: MessageBox.Show(ex.Message);
29: }
30: }