六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

.Net在SqlServer中的圖片存取技術(shù)

[摘要]本文總結(jié)如何在.Net Winform和.Net webform(asp.net)中將圖片存入sqlserver中并讀取顯示的方法1,使用asp.net將圖片上傳并存入SqlServer中,然后從SqlServer中讀取并顯示出來一,上傳并存入SqlServer 數(shù)據(jù)庫結(jié)構(gòu) create tab...
本文總結(jié)如何在.Net Winform和.Net webform(asp.net)中將圖片存入sqlserver中并讀取顯示的方法
1,使用asp.net將圖片上傳并存入SqlServer中,然后從SqlServer中讀取并顯示出來
一,上傳并存入SqlServer
數(shù)據(jù)庫結(jié)構(gòu)
create table test
{
id identity(1,1),
FImage image
}
相關(guān)的存儲過程
Create proc UpdateImage
(
@UpdateImage Image
)
As
Insert Into test(FImage) values(@UpdateImage)
GO

在UpPhoto.aspx文件中添加如下:
<input id="UpPhoto" name="UpPhoto" runat="server" type="file">
<asp:Button id="btnAdd" name="btnAdd" runat="server" Text="上傳"></asp:Button>

然后在后置代碼文件UpPhoto.aspx.cs添加btnAdd按鈕的單擊事件處理代碼:
private void btnAdd_Click(object sender, System.EventArgs e)
{
//獲得圖象并把圖象轉(zhuǎn)換為byte[]
HttpPostedFile upPhoto=UpPhoto.PostedFile;
int upPhotoLength=upPhoto.ContentLength;
byte[] PhotoArray=new Byte[upPhotoLength];
Stream PhotoStream=upPhoto.InputStream;
PhotoStream.Read(PhotoArray,0,upPhotoLength);

//連接數(shù)據(jù)庫
SqlConnection conn=new SqlConnection();
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";

SqlCommand cmd=new SqlCommand("UpdateImage",conn);
cmd.CommandType=CommandType.StoredProcedure;

cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);
cmd.Parameters["@UpdateImage"].Value=PhotoArray;

//如果你希望不使用存儲過程來添加圖片把上面四句代碼改為:
//string strSql="Insert into test(FImage) values(@FImage)";
//SqlCommand cmd=new SqlCommand(strSql,conn);
//cmd.Parameters.Add("@FImage",SqlDbType.Image);
//cmd.Parameters["@FImage"].Value=PhotoArray;

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}

二,從SqlServer中讀取并顯示出來
在需要顯示圖片的地方添加如下代碼:
<asp:image id="imgPhoto" runat="server" ImageUrl="ShowPhoto.aspx"></asp:image>

ShowPhoto.aspx主體代碼:
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
SqlConnection conn=new SqlConnection()
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";

string strSql="select * from test where id=2";//這里假設(shè)獲取id為2的圖片
SqlCommand cmd=new SqlCommand()
reader.Read();
Response.ContentType="application/octet-stream";
Response.BinaryWrite((Byte[])reader["FImage"]);
Response.End();
reader.Close();
}
}


3,在winform中將圖片存入sqlserver,并從sqlserver中讀取并顯示在picturebox中

1,存入sqlserver
數(shù)據(jù)庫結(jié)構(gòu)和使用的存儲過過程,同上面的一樣
1.1,在窗體中加一個(gè)OpenFileDialog控件,命名為ofdSelectPic
1.2,在窗體上添加一個(gè)打開文件按鈕,添加如下單擊事件代碼:
Stream ms;
byte[] picbyte;
//ofdSelectPic.ShowDialog();
if (ofdSelectPic.ShowDialog()==DialogResult.OK)
{
if ((ms=ofdSelectPic.OpenFile())!=null)
{
//MessageBox.Show("ok");
picbyte=new byte[ms.Length];
ms.Position=0;
ms.Read(picbyte,0,Convert.ToInt32(ms.Length));
//MessageBox.Show("讀取完畢!");

//連接數(shù)據(jù)庫
SqlConnection conn=new SqlConnection();
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";

SqlCommand cmd=new SqlCommand("UpdateImage",conn);
cmd.CommandType=CommandType.StoredProcedure;

cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);
cmd.Parameters["@UpdateImage"].Value=picbyte;

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

ms.Close();
}
}

2,讀取并顯示在picturebox中
2.1 添加一個(gè)picturebox,名為ptbShow
2.2 添加一個(gè)按鈕,添加如下響應(yīng)事件:
SqlConnection conn=new SqlConnection();
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";

string strSql="select FImage from test where id=1";

SqlCommand cmd=new SqlCommand(strSql,conn);

conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();

MemoryStream ms=new MemoryStream((byte[])reader["FImage"]);


Image image=Image.FromStream(ms,true);

reader.Close();
conn.Close();

ptbShow.Image=i


主站蜘蛛池模板: 中文区永久区乱码六区 | 视频久久 | 亚洲a级毛片| 涩涩久久 | 日本一道在线 | 伊人久久青青 | 天天躁夜夜躁狠狠躁2021a | 特黄一级视频 | 日本欧美一区二区三区高清 | 亚洲视频免费观看 | 午夜一级视频 | 亚洲综合亚洲综合网成人 | 天天躁夜夜躁狠狠躁2021西西 | 在线免费观看亚洲视频 | 天天干天天操天天操 | 在线看一区| 亚洲国产精品久久 | 夜夜嗨视频 | 天天天天添天天拍天天谢 | 日韩福利视频精品专区 | 日韩欧美高清在线 | 日本永久视频 | 亚洲资源在线 | 亚洲三级欧美 | 三级在线免费观看 | 天天躁夜夜躁狠狠躁2021 | 色窝窝免费播放视频在线 | 亚洲伊人久久大香线蕉综合图片 | 任我爽在线视频精品凹凸精品分类 | 日本道v高清免费 | 午夜免费啪在线观看视频网站 | 欧美一级看片a免费观看 | 亚洲第一区精品日韩在线播放 | 青青青激情视频在线最新 | 欧美亚洲另类图片 | 四虎国产永久免费久久 | 日韩毛片在线视频 | 午夜无遮挡怕怕怕免费视频 | 爽爽影院在线看 | 小明看国产| 天天干天天舔 |