新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
一個C#向SQLServer數(shù)據(jù)庫保存圖片的代碼實(shí)例
我們在用C#和SQL Server數(shù)據(jù)庫開發(fā)應(yīng)用程序時,常常會用到圖片處理的問題。那么C#是怎樣將圖片保存到SQL Server數(shù)據(jù)庫中的呢?本文我們通過一個實(shí)例代碼來介紹這一過程。

10余年的寧城網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整寧城建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“寧城網(wǎng)站設(shè)計(jì)”,“寧城網(wǎng)站推廣”以來,每個客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
首先打開一個圖片文件代碼如下:
- private void Image(object sender, EventArgs e)
- {
- OpenFileDialog fileDialog = new OpenFileDialog();
- fileDialog.Filter = "圖片文件|*.jpg";
- fileDialog.Multiselect = false;
- if (fileDialog.ShowDialog() == DialogResult.OK)
- {
- //圖片地址
- this.textBoxImage.Text = fileDialog.FileName;
- }
- }
保存圖片:
- private void Save(object sender, EventArgs e)
- {
- //把圖片轉(zhuǎn)換為二進(jìn)制保存
- Stream stream = new FileStream(this.textBoxImage.Text.Trim(), FileMode.Open);
- byte[] data=new byte[stream.Length];
- stream.Read(data, 0, data.Length);
- stream.Close();
- //保存到數(shù)據(jù)庫
- string connectionString = 連接字符串;
- SqlConnection connection = new SqlConnection(connectionString);
- //sql語句
- string sql="@INSERT INTO 數(shù)據(jù)庫名稱 (Image) VALUES(@Image)";
- SqlCommand cmd = new SqlCommand(sql, connection);
- SqlParameter parameter=new SqlParameter ()
- {ParameterName="@Image",Value=data,SqlDbTypeSqlDbType=SqlDbType.Image};
- cmd.Parameters.AddRange(parameters);
- if (connection.State == ConnectionState.Closed)
- {
- connection.Open();
- }
- int count = cmd.ExecuteNonQuery();
- if (count > 0)
- {
- MessageBox.Show("success");
- }
- else
- {
- MessageBox.Show("failed");
- }
- connection.Close();
- }
- }
執(zhí)行完上述代碼,就可以成功地將圖片保存到SQL Server數(shù)據(jù)庫中了。
分享名稱:一個C#向SQLServer數(shù)據(jù)庫保存圖片的代碼實(shí)例
URL標(biāo)題:http://m.5511xx.com/article/cdhphss.html


咨詢
建站咨詢
