Trang chủ
Bài viết mới
Diễn đàn
Bài mới trên hồ sơ
Hoạt động mới nhất
VIDEO
Mùa Tết
Văn Học Trẻ
Văn Học News
Media
New media
New comments
Search media
Đại Học
Đại cương
Chuyên ngành
Triết học
Kinh tế
KHXH & NV
Công nghệ thông tin
Khoa học kĩ thuật
Luận văn, tiểu luận
Phổ Thông
Lớp 12
Ngữ văn 12
Lớp 11
Ngữ văn 11
Lớp 10
Ngữ văn 10
LỚP 9
Ngữ văn 9
Lớp 8
Ngữ văn 8
Lớp 7
Ngữ văn 7
Lớp 6
Ngữ văn 6
Tiểu học
Thành viên
Thành viên trực tuyến
Bài mới trên hồ sơ
Tìm trong hồ sơ cá nhân
Credits
Transactions
Xu: 0
Đăng nhập
Đăng ký
Có gì mới?
Tìm kiếm
Tìm kiếm
Chỉ tìm trong tiêu đề
Bởi:
Hoạt động mới nhất
Đăng ký
Menu
Đăng nhập
Đăng ký
Install the app
Cài đặt
Chào mừng Bạn tham gia Diễn Đàn VNKienThuc.com -
Định hướng Forum
Kiến Thức
- HÃY TẠO CHỦ ĐỀ KIẾN THỨC HỮU ÍCH VÀ CÙNG NHAU THẢO LUẬN Kết nối:
VNK X
-
VNK groups
| Nhà Tài Trợ:
BhnongFood X
-
Bhnong groups
-
Đặt mua Bánh Bhnong
CÔNG NGHỆ
Công Nghệ Thông Tin
Web Development
Cách lưu hình ảnh vào database trong C Sharp
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Trả lời chủ đề
Nội dung
<blockquote data-quote="hoangphuong2003" data-source="post: 67710" data-attributes="member: 1882"><p><strong>Database là Microsoft SQL Server</strong></p><p><strong>1</strong>. Tạo database có tên: TestImageDB với 1 bảng có tên tblImages và có cấu trúc như hình sau:</p><p><img src="https://thanglong-aptech.com/wp-content/uploads/2010/12/dbimage_cs_03.png" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></p><p></p><p><strong>2</strong>. Tạo stored project có tên InsertImage với sql script như sau:</p><p>CREATE PROCEDURE InsertImage</p><p>@filename nvarchar(250),</p><p>@blobdata image</p><p>AS</p><p>insert into tblImages values(</p><p>@filename, @blobdata</p><p>)</p><p><strong>3.</strong> Tạo Windows Form Application Project có tên AnotherWay.</p><p><strong>4</strong>. Tạo lớp ConnectDB.cs có nội dung như sau:</p><p></p><p>using System;</p><p>using System.Collections.Generic;</p><p>using System.IO;</p><p>using System.Data;</p><p>using System.Data.SqlClient;</p><p>namespace AnotherWay</p><p>{</p><p>class ConnectDB</p><p>{</p><p>private SqlConnection conn;</p><p>private string connectionString = "Server=.;UID=sa;PWD=;Initial Catalog=TestImageDB";</p><p>public ConnectDB()</p><p>{</p><p>conn = new SqlConnection(connectionString);</p><p>}</p><p>public void StorePicture(string filename)</p><p>{</p><p>byte[] imageData = null;</p><p>// Read the file into a byte array</p><p>using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))</p><p>{</p><p>imageData = new Byte[fs.Length];</p><p>fs.Read(imageData, 0, (int)fs.Length);</p><p>}</p><p>using (SqlConnection conn = new SqlConnection(connectionString))</p><p>{</p><p>SqlCommand cmd = new SqlCommand("InsertImage", conn);</p><p>cmd.CommandType = CommandType.StoredProcedure;</p><p>cmd.Parameters.AddWithValue("@filename", filename);</p><p>cmd.Parameters["@filename"].Direction = ParameterDirection.Input;</p><p>cmd.Parameters.Add("@blobdata", SqlDbType.Image);</p><p>cmd.Parameters["@blobdata"].Direction = ParameterDirection.Input;</p><p>// Store the byte array within the image field</p><p>cmd.Parameters["@blobdata"].Value = imageData;</p><p>conn.Open();</p><p>cmd.ExecuteNonQuery();</p><p>}</p><p>}</p><p>public byte[] RetrieveImage()</p><p>{</p><p>byte[] imageData = null;</p><p>conn.Open();</p><p>SqlCommand cmd = new SqlCommand("select blobdata from tblImages", conn);</p><p>// Assume previously established command and connection</p><p>// The command SELECTs the IMAGE column from the table</p><p>using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))</p><p>{</p><p>reader.Read();</p><p>// Get size of image data - pass null as the byte array parameter</p><p>long bytesize = reader.GetBytes(0, 0, null, 0, 0);</p><p>// Allocate byte array to hold image data</p><p>imageData = new byte[bytesize];</p><p>long bytesread = 0;</p><p>int curpos = 0;</p><p>int chunkSize = 1;</p><p>while (bytesread < bytesize)</p><p>{</p><p>// chunkSize is an arbitrary application defined value</p><p>bytesread += reader.GetBytes(0, curpos, imageData, curpos, chunkSize);</p><p>curpos += chunkSize;</p><p>}</p><p>}</p><p>conn.Close();</p><p>// byte array 'imageData' now contains BLOB from database</p><p>return imageData;</p><p>}</p><p>}</p><p>}</p><p><strong>5</strong>. Thiết kế Form như hình</p><p><img src="https://thanglong-aptech.com/wp-content/uploads/2010/12/dbimage_cs_04.png" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></p><p></p><p><strong>6</strong>. Code cho Form:</p><p></p><p>using System;</p><p>using System.Collections.Generic;</p><p>using System.ComponentModel;</p><p>using System.Data;</p><p>using System.Drawing;</p><p>using System.IO;</p><p>using System.Windows.Forms;</p><p>namespace AnotherWay</p><p>{</p><p>public partial class Form1 : Form</p><p>{</p><p>private ConnectDB conDB;</p><p>public Form1()</p><p>{</p><p>InitializeComponent();</p><p>conDB = new ConnectDB();</p><p>}</p><p>private void button1_Click(object sender, EventArgs e)</p><p>{</p><p>OpenFileDialog dlg = new OpenFileDialog();</p><p>dlg.Filter = "JPG Files(*.JPG)|*.JPG|GIF Files(*.GIF)|*.GIF";</p><p>if (dlg.ShowDialog(this) == DialogResult.OK)</p><p>{</p><p>conDB.StorePicture(dlg.FileName);</p><p>}</p><p>}</p><p>private void button2_Click(object sender, EventArgs e)</p><p>{</p><p>byte[] img = conDB.RetrieveImage();</p><p>MemoryStream str = new MemoryStream(img);</p><p>pictureBox1.Image = Image.FromStream(str);</p><p>}</p><p>}</p><p>}</p><p><strong>7</strong>. Thực thi</p><p>Trước hết nhấn nút Lưu, sau đó muốn xem lại mẫu tin đã lưu, nhấn nút load.</p><p><img src="https://thanglong-aptech.com/wp-content/uploads/2010/12/dbimage_cs_05.png" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></p><p></p><p>Chúc thành công!</p><p>Theo vovanhai</p><p><strong>Nguồn</strong>: <strong><em>Cách lưu hình ảnh vào database trong C sharp (P2)</em></strong></p><p>________________________________________________________________________________</p><p>Nghề kiểm thử phần mềm bạn đã biết chưa?</p></blockquote><p></p>
[QUOTE="hoangphuong2003, post: 67710, member: 1882"] [B]Database là Microsoft SQL Server[/B] [B]1[/B]. Tạo database có tên: TestImageDB với 1 bảng có tên tblImages và có cấu trúc như hình sau: [IMG]https://thanglong-aptech.com/wp-content/uploads/2010/12/dbimage_cs_03.png[/IMG] [B]2[/B]. Tạo stored project có tên InsertImage với sql script như sau: CREATE PROCEDURE InsertImage @filename nvarchar(250), @blobdata image AS insert into tblImages values( @filename, @blobdata ) [B]3.[/B] Tạo Windows Form Application Project có tên AnotherWay. [B]4[/B]. Tạo lớp ConnectDB.cs có nội dung như sau: using System; using System.Collections.Generic; using System.IO; using System.Data; using System.Data.SqlClient; namespace AnotherWay { class ConnectDB { private SqlConnection conn; private string connectionString = "Server=.;UID=sa;PWD=;Initial Catalog=TestImageDB"; public ConnectDB() { conn = new SqlConnection(connectionString); } public void StorePicture(string filename) { byte[] imageData = null; // Read the file into a byte array using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) { imageData = new Byte[fs.Length]; fs.Read(imageData, 0, (int)fs.Length); } using (SqlConnection conn = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand("InsertImage", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@filename", filename); cmd.Parameters["@filename"].Direction = ParameterDirection.Input; cmd.Parameters.Add("@blobdata", SqlDbType.Image); cmd.Parameters["@blobdata"].Direction = ParameterDirection.Input; // Store the byte array within the image field cmd.Parameters["@blobdata"].Value = imageData; conn.Open(); cmd.ExecuteNonQuery(); } } public byte[] RetrieveImage() { byte[] imageData = null; conn.Open(); SqlCommand cmd = new SqlCommand("select blobdata from tblImages", conn); // Assume previously established command and connection // The command SELECTs the IMAGE column from the table using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)) { reader.Read(); // Get size of image data - pass null as the byte array parameter long bytesize = reader.GetBytes(0, 0, null, 0, 0); // Allocate byte array to hold image data imageData = new byte[bytesize]; long bytesread = 0; int curpos = 0; int chunkSize = 1; while (bytesread < bytesize) { // chunkSize is an arbitrary application defined value bytesread += reader.GetBytes(0, curpos, imageData, curpos, chunkSize); curpos += chunkSize; } } conn.Close(); // byte array 'imageData' now contains BLOB from database return imageData; } } } [B]5[/B]. Thiết kế Form như hình [IMG]https://thanglong-aptech.com/wp-content/uploads/2010/12/dbimage_cs_04.png[/IMG] [B]6[/B]. Code cho Form: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Windows.Forms; namespace AnotherWay { public partial class Form1 : Form { private ConnectDB conDB; public Form1() { InitializeComponent(); conDB = new ConnectDB(); } private void button1_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "JPG Files(*.JPG)|*.JPG|GIF Files(*.GIF)|*.GIF"; if (dlg.ShowDialog(this) == DialogResult.OK) { conDB.StorePicture(dlg.FileName); } } private void button2_Click(object sender, EventArgs e) { byte[] img = conDB.RetrieveImage(); MemoryStream str = new MemoryStream(img); pictureBox1.Image = Image.FromStream(str); } } } [B]7[/B]. Thực thi Trước hết nhấn nút Lưu, sau đó muốn xem lại mẫu tin đã lưu, nhấn nút load. [IMG]https://thanglong-aptech.com/wp-content/uploads/2010/12/dbimage_cs_05.png[/IMG] Chúc thành công! Theo vovanhai [B]Nguồn[/B]: [B][I]Cách lưu hình ảnh vào database trong C sharp (P2)[/I][/B] ________________________________________________________________________________ Nghề kiểm thử phần mềm bạn đã biết chưa? [/QUOTE]
Tên
Mã xác nhận
Gửi trả lời
CÔNG NGHỆ
Công Nghệ Thông Tin
Web Development
Cách lưu hình ảnh vào database trong C Sharp
Top