1.建立一個aspx頁面,html程式碼2.cs程式碼
使用 System.Data.SqlClient;
使用系統.IO;
protected void Page_Load(物件發送者,EventArgs e)
{
if (!Page.IsPostBack)
{
綁定資料();
}
如果(ID!=“”)
{
取得描述ID(ID);
}
}
屬性#區域屬性
私有字串ID
{
得到
{
if (請求["ID"] != null && 請求["ID"].ToString() != "")
{
返回請求[“ID”];
}
別的
{
返回 ””;
}
}
}
#endregion
GetDescriptionByID#region GetDescriptionByID
私有無效 GetDescriptionByID(字串 ID)
{
字串 connStr = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection conn = new SqlConnection(connStr);
string sql = "select * from testimage where userid='" + ID + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
字串s = @"<table cellspacing='0' cellpadding='4' width='300' height='200' border='0' id='GridView1' style='color:#333333;border-collapse: collapse ;'>";
if(博士.Read())
{
s += "<tr style='color:#333333;background-color:#FFFBD6;'>";
s += "<td width='50'>名稱:</td>";
s += "<td>" + dr["使用者名稱"] + "</td>";
s+=“</tr>”;
s += "<tr style='color:#333333;background-color:White;'>";
s += "<tdscope='col'>描述:</td>";
s += "<td>" + dr["描述"] + "</td>";
s+=“</tr>”;
}
s += "</table>";
博士.關閉();
conn.Close();
this.Response.Write(s);
this.Response.End();
}
#endregion
儲存圖片#region儲存圖片
protected void Button2_Click(物件發送者,EventArgs e)
{
流圖像流;
string Path = FileUpload1.PostedFile.FileName;//檔案名稱
int Size = FileUpload1.PostedFile.ContentLength; // 檔案大小
字串型別 = FileUpload1.PostedFile.ContentType; // 檔案類型
ImageStream = FileUpload1.PostedFile.InputStream;
byte[] 內容 = 新位元組[大小];
int Status = ImageStream.Read(內容, 0, 大小);
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand comm = new SqlCommand("插入 testimage (使用者名稱,映像,路徑,類型,描述)值(@使用者名稱,@映像,@路徑,@類型,@描述)", conn);
comm.CommandType = CommandType.Text;
comm.Parameters.Add("@UserName", SqlDbType.VarChar, 255).Value = txtUserName.Text;
comm.Parameters.Add("@Image", SqlDbType.Image).Value = 內容;
comm.Parameters.Add("@Path", SqlDbType.VarChar, 255).Value = Path;
comm.Parameters.Add("@Type", SqlDbType.VarChar, 255).Value = 型;
comm.Parameters.Add("@Description", SqlDbType.VarChar, 2000).Value = this.TextBox1.Text;
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
#endregion
BindData#region BindData
私有無效 BindData()
{
string sql = "從測試影像中選擇*";
資料集 ds = GetDataSet(sql);
this.DataList1.DataSource = ds;
this.DataList1.DataBind();
}
#endregion
GetDataSet#region GetDataSet
私有資料集 GetDataSet(字串 sql)
{
字串 constring = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlDataAdapter sda = new SqlDataAdapter(sql, constring);
資料集 ds = new 資料集();
sda.Fill(ds);
返回ds;
}
#endregion3.資料庫腳本
如果存在(從 dbo.sysobjects 選擇 *,其中 id = object_id(N'[dbo].[TestImage]') 和 OBJECTPROPERTY(id, N'IsUserTable') = 1)
刪除表 [dbo].[TestImage]
去
建立表 [dbo].[TestImage] (
[用戶ID] [int] IDENTITY (1, 1) NOT NULL ,
[使用者名稱] [nvarchar] (500) COLLATE 中文_PRC_CI_AS NULL ,
[圖像] [圖像] NULL ,
[路徑] [nvarchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[類型] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[描述] [nvarchar] (2000) COLLATE 中文_PRC_CI_AS NULL
) 在 [主] TEXTIMAGE_ON [主]
去
<html>
<頭>
<標題>WebForm1</標題>
<style type="text/css">.logo {}{ 位置:絕對 }.dek {}{ Z-INDEX:200;可見性:隱藏;位置:絕對 }</style>
</頭>
<正文>
<表單runat =「伺服器」>
<DIV class="dek" id="dek"></DIV>
<腳本語言=“javascript”>
X偏移=-20;
Y偏移= 20;
var nav,yyy=-1000;
var skn=dek.style;
document.onmousemove=get_mouse;
//ajax
var xmlHttp;
函數 createXMLHttpRequest()
{
if (視窗.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
否則如果 (window.XMLHttpRequest)
{
xmlHttp = 新的 XMLHttpRequest();
}
}
函數startRequest(id)
{
建立XMLHttpRequest();
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("GET", "?ID="+id, true);
xmlHttp.send(null);
}
變數內容;
函數handleStateChange()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
內容=xmlHttp.responseText;
}
}
}
//工具提示
函數彈出(id)
{
開始請求(id);
yyy=Y偏移;
document.all("dek").innerHTML=內容;
skn.visibility =“可見”
函數
get_mouse(e)
{
var x=event.x+document.body.scrollLeft;
skn.left=x+Xoffset;
var y=event.y+document.body.scrollTop;
skn.top=y+yyy;
}
函數kill()
{
yyy=-1000;
skn.visibility="隱藏";
}
</腳本>
<div>
<asp:FileUpload ID="FileUpload1" runat="server" /><br>名稱:<asp:TextBox ID="txtUserName"
runat="server"></asp:TextBox><br>
描述:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="儲存" />
<asp:DataList id="DataList1"
邊框顏色=“黑色”
單元格填充=“1”
CellSpacing =“4”HorizontalAlign =“居中”
重複列=“4”
重複佈局=“表格”
runat="伺服器" ShowFooter="true" ShowHeader="true"
寬度=“100%”>
<專案範本>
<%# DataBinder.Eval(Container.DataItem, "使用者名稱")%><br>
<img ID="img1" onmouseover="popup(<%# DataBinder.Eval(Container.DataItem, "UserID")%>);" onmouseout="kill();" src='<%# DataBinder.Eval(Container.DataItem, "Path") %>' height='150'/>
</項目模板>
</asp:資料列表>
</div>
</表格>
</正文>
</html>