最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

gridview 顯示圖片的實例代碼

 更新時間:2013年04月27日 14:22:02   作者:  
gridview 圖片的二進制數(shù)據(jù)庫存儲和顯示

1.將圖片以二進制存入數(shù)據(jù)庫

2.讀取二進制圖片在頁面顯示

3.設置Image控件顯示從數(shù)據(jù)庫中讀出的二進制圖片

4.GridView中ImageField以URL方式顯示圖片

5.GridView顯示讀出的二進制圖片

====================

1.將圖片以二進制存入數(shù)據(jù)庫

復制代碼 代碼如下:

//保存圖片到數(shù)據(jù)庫

protected void Button1_Click(object sender, EventArgs e)

{

   //圖片路徑

   string strPath = "~/photo/03.JPG";

   string strPhotoPath = Server.MapPath(strPath);

   //讀取圖片

   FileStream fs = new System.IO.FileStream(strPhotoPath, FileMode.Open, FileAccess.Read);

   BinaryReader br = new BinaryReader(fs);

   byte[] photo = br.ReadBytes((int)fs.Length);

   br.Close();

   fs.Close();

   //存入

   SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");

   string strComm = " INSERT INTO personPhoto(personName, personPhotoPath, personPhoto) ";

   strComm += " VALUES('wangwu', '" + strPath + "', @photoBinary )";

   SqlCommand myComm = new SqlCommand(strComm, myConn);

   myComm.Parameters.Add("@photoBinary", SqlDbType.Binary,photo.Length);

   myComm.Parameters["@photoBinary"].Value = http://www.cnblogs.com/wycoo/archive/2012/02/07/photo;

   myConn.Open();

   myComm.ExecuteNonQuery();

   myConn.Close();

}

2.讀取二進制圖片在頁面顯示

復制代碼 代碼如下:

//讀取圖片

SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");

string strComm = " SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ";

SqlCommand myComm = new SqlCommand(strComm, myConn);

myConn.Open();

SqlDataReader dr = myComm.ExecuteReader();

while (dr.Read())

{

   byte[] photo = (byte[])dr["personPhoto"];

   this.Response.BinaryWrite(photo);

}

dr.Close();

myConn.Close();



復制代碼 代碼如下:

SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");

SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ", myConn);

DataSet myds = new DataSet();

myConn.Open();

myda.Fill(myds);

myConn.Close();

byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"];

this.Response.BinaryWrite(photo);


3.設置Image控件顯示從數(shù)據(jù)庫中讀出的二進制圖片
復制代碼 代碼如下:

SqlConnection myConn = new SqlConnection("Data Source=192.168.0.36;Initial Catalog=TestDB;User ID=sa;Password=sa");

SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ", myConn);

DataSet myds = new DataSet();

myConn.Open();

myda.Fill(myds);

myConn.Close();

byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"];

//圖片路徑

string strPath = "~/photo/wangwu.JPG";

string strPhotoPath = Server.MapPath(strPath);

//保存圖片文件

BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));

bw.Write(photo);

bw.Close();


顯示圖片

復制代碼 代碼如下:

this.Image1.ImageUrl = strPath;

 

//4.GridView中ImageField以URL方式顯示圖片

----------------------------

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">

   <Columns>

   <asp:BoundField DataField="personName" HeaderText="姓名" />

   <asp:ImageField DataImageUrlField="personPhotoPath"

   HeaderText="圖片">

   </asp:ImageField>

   </Columns>

</asp:GridView>


后臺直接綁定即可

5.GridView顯示讀出的二進制圖片

復制代碼 代碼如下:

//樣板列
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">

   <Columns>

   <asp:BoundField DataField="personName" HeaderText="姓名" />

   <asp:ImageField DataImageUrlField="personPhotoPath"

   HeaderText="圖片">

   </asp:ImageField>

   <asp:TemplateField HeaderText="圖片">

   <ItemTemplate>

   <asp:Image ID="Image1" runat="server" />

   </ItemTemplate>

   </asp:TemplateField>

   </Columns>

</asp:GridView>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

   if (e.Row.RowIndex < 0)

   return;

   // System.ComponentModel.Container

   string strPersonName = (string)DataBinder.Eval(e.Row.DataItem, "personName");

   Image tmp_Image = (Image)e.Row.Cells[2].FindControl("Image1");

   if (!System.Convert.IsDBNull(DataBinder.Eval(e.Row.DataItem, "personPhoto")))

   {

   //

   byte[] photo = (byte[])DataBinder.Eval(e.Row.DataItem, "personPhoto");

   //圖片路徑

   string strPath = "~/photo/" + strPersonName.Trim() + ".JPG";

   string strPhotoPath = Server.MapPath(strPath);

   //保存圖片文件

   BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate));

   bw.Write(photo);

   bw.Close();

   //顯示圖片

   tmp_Image.ImageUrl = strPath;

   }
}

相關文章

最新評論

临漳县| 广元市| 昌吉市| 白山市| 阳城县| 册亨县| 永康市| 秦皇岛市| 林西县| 三河市| 清河县| 辉县市| 天气| 安福县| 屏东市| 鹤壁市| 洪江市| 石河子市| 罗山县| 滕州市| 三亚市| 株洲市| 台南市| 屏东市| 吉木萨尔县| 哈密市| 乐陵市| 庐江县| 淳安县| 吴江市| 关岭| 大兴区| 进贤县| 黑龙江省| 临泉县| 搜索| 旬阳县| 都匀市| 宁津县| 屏东市| 阳泉市|