自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一個(gè)C#向SQL Server數(shù)據(jù)庫保存圖片的代碼實(shí)例

數(shù)據(jù)庫 SQL Server
本文主要介紹了用C#將圖片保存到SQL Server數(shù)據(jù)庫中的過程,并給出了全部的代碼,希望能對(duì)讀者有所幫助。

我們?cè)谟?strong>C#和SQL Server數(shù)據(jù)庫開發(fā)應(yīng)用程序時(shí),常常會(huì)用到圖片處理的問題。那么C#是怎樣將圖片保存到SQL Server數(shù)據(jù)庫中的呢?本文我們通過一個(gè)實(shí)例代碼來介紹這一過程。

首先打開一個(gè)圖片文件代碼如下:

  1. private void Image(object sender, EventArgs e)  
  2.  
  3. {  
  4.  
  5. OpenFileDialog fileDialog = new OpenFileDialog();  
  6.  
  7. fileDialog.Filter = "圖片文件|*.jpg";  
  8.  
  9. fileDialog.Multiselect = false;  
  10.  
  11. if (fileDialog.ShowDialog() == DialogResult.OK)  
  12.  
  13. {  
  14.  
  15. //圖片地址  
  16.  
  17. this.textBoxImage.Text = fileDialog.FileName;  
  18.  
  19. }  
  20.  

保存圖片:

  1. private void Save(object sender, EventArgs e)  
  2.  
  3. {  
  4.  
  5. //把圖片轉(zhuǎn)換為二進(jìn)制保存  
  6.  
  7. Stream stream = new FileStream(this.textBoxImage.Text.Trim(), FileMode.Open);  
  8.  
  9. byte[] data=new byte[stream.Length];  
  10.  
  11. stream.Read(data, 0, data.Length);  
  12.  
  13. stream.Close();  
  14.  
  15. //保存到數(shù)據(jù)庫  
  16.  
  17. string connectionString = 連接字符串;  
  18.  
  19. SqlConnection connection = new SqlConnection(connectionString);  
  20.  
  21. //sql語句  
  22.  
  23. string sql="@INSERT INTO 數(shù)據(jù)庫名稱 (Image) VALUES(@Image)";  
  24.  
  25. SqlCommand cmd = new SqlCommand(sql, connection);  
  26.  
  27. SqlParameter parameter=new SqlParameter ()  
  28.  
  29. {ParameterName="@Image",Value=data,SqlDbTypeSqlDbType=SqlDbType.Image};  
  30.  
  31. cmd.Parameters.AddRange(parameters);  
  32.  
  33. if (connection.State == ConnectionState.Closed)  
  34.  
  35. {  
  36.  
  37. connection.Open();  
  38.  
  39. }  
  40.  
  41. int count = cmd.ExecuteNonQuery();  
  42.  
  43. if (count > 0)  
  44.  
  45. {  
  46.  
  47. MessageBox.Show("success");  
  48.  
  49. }  
  50.  
  51. else  
  52.  
  53. {  
  54.  
  55. MessageBox.Show("failed");  
  56.  
  57. }  
  58.  
  59. connection.Close();  
  60.  
  61. }  
  62.  

 執(zhí)行完上述代碼,就可以成功地將圖片保存到SQL Server數(shù)據(jù)庫中了。

【編輯推薦】

  1. 淺析SQL Server數(shù)據(jù)庫專用管理員連接DAC的使用
  2. 簡(jiǎn)述刪除SQL SERVER 2000數(shù)據(jù)庫日志的兩種方法
  3. 在SQL SERVER 2005執(zhí)行存儲(chǔ)過程的權(quán)限分配問題
  4. 忘記sa密碼,又刪除了administrators帳號(hào)的解決方法
  5. T-SQL行列相互轉(zhuǎn)換命令:PIVOT和UNPIVOT使用詳解
責(zé)任編輯:趙鵬 來源: 博客園
相關(guān)推薦

2011-07-18 10:01:59

C# ADO.NETSQL Server數(shù)

2011-08-22 12:01:36

SQL Server代碼優(yōu)化

2011-07-20 13:40:00

SQLite數(shù)據(jù)庫查詢數(shù)據(jù)

2009-09-04 17:29:01

C#創(chuàng)建SQL Ser

2009-08-03 14:17:18

C#連接AccessC#連接SQL Ser

2011-07-20 12:55:17

SQLite數(shù)據(jù)庫插入數(shù)據(jù)

2011-08-15 11:24:46

SQL Server事務(wù)

2011-07-20 14:57:47

SQLite數(shù)據(jù)庫ORDER BYGROUP BY

2009-07-30 18:18:27

C#時(shí)間計(jì)算

2009-08-18 17:19:33

C#事件模型

2011-04-11 14:18:44

SQL Server數(shù)圖片

2011-04-06 15:36:56

SQL Server數(shù)

2011-04-06 11:34:52

SQL Server數(shù)查詢優(yōu)化

2011-07-26 18:06:00

SQL Server數(shù)批量重命名

2010-06-28 09:53:11

SQL Server數(shù)

2009-08-12 17:02:16

.NET向SQL Se

2011-08-22 09:55:30

SQL Server 排序

2009-08-25 15:22:18

C#連接SQL數(shù)據(jù)庫

2011-07-20 13:18:01

SQLite數(shù)據(jù)庫修改和刪除數(shù)據(jù)

2009-08-19 16:30:55

C#操作Access數(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)