Lưu trữ và hiển thị hình ảnh trong Database
lượt xem 33
download
Download
Vui lòng tải xuống để xem tài liệu đầy đủ
Lưu trữ và hiển thị hình ảnh trong Database - ASP.NET Đầu tiên là phải thiết kế CSDL, ở đay tôi tạo 1 database mới có tên là Employee code 1 2 3 4 5 6 7 8 9 10 CREATE DATABASE [Employee] GO USE [Employee] GO CREATE TABLE EmpDetails
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Lưu trữ và hiển thị hình ảnh trong Database
- Lưu trữ và hiển thị hình ảnh trong Database - ASP.NET Đầu tiên là phải thiết kế CSDL, ở đay tôi tạo 1 database mới có tên là Employee ? code 1 CREATE DATABASE [Employee] 2 GO 3 USE [Employee] 4 GO 5 CREATE TABLE EmpDetails ( 6 empid int IDENTITY NOT NULL, 7 empname varchar(20), 8 empimg image 9 ) 10 B1: Tạo 1 Website mới và Import Namespace System.Data.SqlClient dùng để truy xuất CSDL B2: Vào Design kéo tha 2 Label & 1 Textbox Control + 1 File Upload Control + 1 Image Control (để hiển thị hình ảnh sau khi Upload) ? code 1 2 Save Retrieve Images 3 4 5 6 7 8 9 10 11 12 13 14 16 17   19
- 20 21 22 23 24 25 26 27 28 29 B3: Viết sự kiện Click cho btnSubmit ? code 1 2 protected void btnSubmit_Click(object sender, EventArgs e) 3 { 4 SqlConnection connection = null; 5 try { 6 FileUpload img = (FileUpload)imgUpload; 7 Byte[] imgByte = null; 8 if (img.HasFile && img.PostedFile != null) 9 { 10 //To create a PostedFile HttpPostedFile File = imgUpload.PostedFile; 11 //Create byte Array with file len 12 imgByte = new Byte[File.ContentLength]; 13 //force the control to load data in array 14 File.InputStream.Read(imgByte, 0, File.ContentLength); 15 } // Insert the employee name and image into db 16 string conn = ConfigurationManager.ConnectionStrings 17 ["EmployeeConnString"].ConnectionString; 18 connection = new SqlConnection(conn); 19 20 connection.Open(); string sql = "INSERT INTO EmpDetails(empname,empimg) VALUES(@enm, @eimg) SELECT 21 @@IDENTITY"; 22 SqlCommand cmd = new SqlCommand(sql, connection); 23 cmd.Parameters.AddWithValue("@enm", txtEName.Text.Trim()); 24 cmd.Parameters.AddWithValue("@eimg", imgByte); 25 int id = Convert.ToInt32(cmd.ExecuteScalar()); lblResult.Text = String.Format("Employee ID is {0}", id); 26 } 27 catch 28 { 29 lblResult.Text = "There was an error"; } 30 finally 31 { 32 connection.Close(); 33 } 34 35 } 36 37
- 38 B4: Bạn Click chuột phải vào Website tạo 1 file Generic Handler có tên là ShowImage.ashx chẳng hạn ? code using System; 1 using System.Configuration; 2 using System.Web; 3 using System.IO; 4 using System.Data; using System.Data.SqlClient; 5 6 public class ShowImage : IHttpHandler 7 { 8 public void ProcessRequest(HttpContext context) 9 { Int32 empno; 10 if (context.Request.QueryString["id"] != null) 11 empno = Convert.ToInt32(context.Request.QueryString["id"]); 12 else 13 throw new ArgumentException("No parameter specified"); 14 15 context.Response.ContentType = "image/jpeg"; Stream strm = ShowEmpImage(empno); 16 byte[] buffer = new byte[4096]; 17 int byteSeq = strm.Read(buffer, 0, 4096); 18 19 while (byteSeq > 0) 20 { 21 context.Response.OutputStream.Write(buffer, 0, byteSeq); byteSeq = strm.Read(buffer, 0, 4096); 22 } 23 //context.Response.BinaryWrite(buffer); 24 } 25 26 public Stream ShowEmpImage(int empno) 27 { string conn = ConfigurationManager.ConnectionStrings 28 ["EmployeeConnString"].ConnectionString; 29 SqlConnection connection = new SqlConnection(conn); 30 string sql = "SELECT empimg FROM EmpDetails WHERE empid = @ID"; 31 SqlCommand cmd = new SqlCommand(sql,connection); cmd.CommandType = CommandType.Text; 32 cmd.Parameters.AddWithValue("@ID", empno); 33 connection.Open(); 34 object img = cmd.ExecuteScalar(); 35 try 36 { return new MemoryStream((byte[])img); 37 } 38 catch 39 { 40 return null; } 41 finally 42 { 43 connection.Close(); 44 } 45 } 46
- public bool IsReusable 47 { 48 get 49 { 50 return false; } 51 } 52 53 54 } 55 56 57 58 59 60 61 62 63 64 File này có nhiệm vụ Request thằng ID trên QueryString để SELECT trong bảng EmpDetail và lấy về trường Image và gán nó vào 1 Stream có tên là ShowEmpImage. Từ Stream đó thì quá dễ để bạn Write ra file ảnh jpg hoặc gif rùi B5 Bước cuối cùng: trong sự kiện btnSubmit_Click (dòng trước Catch) bạn set lại thuộc tính ImageURL cho Control Image1 ? code Image1.ImageUrl = "~/ShowImage.ashx?id=" + id; 1 Download Source Code tại đây. Theo dotnetcurry
CÓ THỂ BẠN MUỐN DOWNLOAD
-
ĐỀ THI TRẮC NGHIỆM TIN HỌC VĂN PHÒNG
11 p |
373
|
111
-
Lưu trữ và hiển thị hình ảnh trong Database - ASP.NET
4 p |
272
|
51
-
Trình bày dữ liệu với DataGridView và ComboBox
10 p |
473
|
51
-
Lưu trữ hình ảnh vào cơ sở dữ liệu với C#
5 p |
232
|
40
-
Lấy dữ liệu dùng Stored Procedure
14 p |
185
|
33
-
Các hướng dẫn sử dụng Mapinfo
27 p |
168
|
20
-
Oracle9i Kiến thức và quản trị- P14
4 p |
102
|
12
-
PHÂN TÍCH THIẾT KẾ HỆ THỐNG THÔNG TIN - CHƯƠNG 4: PHÂN TÍCH – THIẾT KẾ THÀNH PHẦN DỮ LIỆU PHÂN TÍCH – THIẾT KẾ THÀNH PHẦN DỮ LIỆU
12 p |
134
|
8
-
Giáo trình Lập trình thiết bị di động 2 (Nghề: Ứng dụng phần mềm - Trình độ: Cao đẳng) - Trường Cao đẳng nghề Cần Thơ
266 p |
13
|
8
-
Những thủ thuật hữu dụng trên Android 4.0 Ice Cream Sandwich
3 p |
84
|
8
-
Lưu và quản lý bookmark với Transferr
3 p |
65
|
7
-
Xem truyện tranh trên máy tính với CDisplay và ComicRack
8 p |
59
|
6
-
Analyzing SQL
4 p |
75
|
6
-
Tải tất cả các tài liệu lưu trữ trên Google Docs về máy tính
4 p |
127
|
5
-
Tạo ảnh sao lưu cho các thiết bị lưu trữ qua giao tiếp USB
2 p |
70
|
5
-
Bài giảng Nhập môn Tin học: Chương 5 - Từ Thị Xuân Hiền
38 p |
74
|
5
-
Forward thiết bị lưu trữ tới máy tính điều khiển từ xa qua Remote Desktop
7 p |
78
|
3
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn