/****** 对象: Table [dbo].[Person] 脚本日期: 03/02/2009 19:37:42 ******/
CREATE TABLE [dbo].[Person](
[PersonID] [int] IDENTITY(1,1) NOT NULL,
[PersonEmail] [varchar](255) COLLATE Chinese_PRC_CI_AS NULL,
[PersonName] [varchar](255) COLLATE Chinese_PRC_CI_AS NULL,
[PersonDOB] [datetime] NULL,
[PersonImage] [image] NULL,
[PersonImageType] [varchar](255) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
存入图片和文字--------------AddAddressList.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddAddressList.aspx.cs" Inherits="AddAddressList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Table Runat="server" Width="100%" BorderWidth="1" BackColor="Beige" ID="Table1"
Font-Name="宋体" Font-Size="9pt" Height="150%">
<asp:TableRow ID="TableRow1" runat="server">
<asp:TableCell ID="TableCell1" ColumnSpan="2" BackColor="Red" runat="server">
<asp:Label ForeColor="White" font-bold="True" Runat="server" Text="添加新同学" ID="Label1" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow2" runat="server">
<asp:TableCell ID="TableCell2" HorizontalAlign="Right" runat="server">
<asp:Label Runat="server" Text="姓名" ID="Label2" />
</asp:TableCell>
<asp:TableCell ID="TableCell3" runat="server">
<asp:TextBox id="txtPersonName" Runat="server" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow3" runat="server">
<asp:TableCell ID="TableCell4" HorizontalAlign="Right" runat="server">
<asp:Label Runat="server" Text="电子邮件" ID="Label3" />
</asp:TableCell>
<asp:TableCell ID="TableCell5" runat="server">
<asp:TextBox id="txtPersonEmail" Runat="server" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow4" runat="server">
<asp:TableCell ID="TableCell6" HorizontalAlign="Right" runat="server">
<asp:Label Runat="server" Text="电话号码" ID="Label7" />
</asp:TableCell>
<asp:TableCell ID="TableCell7" runat="server">
<asp:TextBox id="TextBox1" Runat="server" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow6" runat="server">
<asp:TableCell ID="TableCell10" HorizontalAlign="Right" runat="server">
<asp:Label Runat="server" Text="出生日期" ID="Label5"/>
</asp:TableCell>
<asp:TableCell ID="TableCell11" runat="server">
<asp:TextBox id="txtPersonDOB" Runat="server" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow7" runat="server">
<asp:TableCell ID="TableCell12" HorizontalAlign="Right" runat="server">
<asp:Label Runat="server" Text="照片" ID="Label6"/>
</asp:TableCell>
<asp:TableCell ID="TableCell13" runat="server">
<input type="file" id="PersonImage" runat="server" /></asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow8" runat="server">
<asp:TableCell ID="TableCell14" ColumnSpan="2" HorizontalAlign="Center" runat="server">
<asp:Button Text=" 添 加 " OnClick="AddPerson" Runat="server" ID="Button1"/>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow9" runat="server">
<asp:TableCell ID="TableCell15" ColumnSpan="2" HorizontalAlign="Center" runat="server">
<asp:Button Text=" 返 回 " OnClick="Return" Runat="server" ID="Button2"/>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
</form>
</body>
</html>
------------------------------------------CS代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class AddAddressList : System.Web.UI.Page
{
public void AddPerson(object sender, System.EventArgs e)
{
Int64 intImageSize;
string strImageType;
Stream ImageStream;
//获得图片的大小
intImageSize = PersonImage.PostedFile.ContentLength;
//获得图片类型
strImageType = PersonImage.PostedFile.ContentType;
//读取图片
ImageStream = PersonImage.PostedFile.InputStream;
byte[] ImageContent = new byte[intImageSize];
int intStatus;
intStatus = ImageStream.Read(ImageContent, 0, Convert.ToInt32(intImageSize));
string strCnn = "Data Source=.;Initial Catalog=Phone;Integrated Security=True;";
SqlConnection conn = new SqlConnection(strCnn);
SqlCommand insertCmd = new SqlCommand("insert into Person(PersonEmail,PersonName ,PersonDOB ,PersonImage ,PersonImageType ) values(@PersonEmail,@PersonName ,@PersonDOB ,@PersonImage ,@PersonImageType)", conn);
insertCmd.Parameters.Add("@PersonEmail", SqlDbType.VarChar, 225);
insertCmd.Parameters.Add("@PersonName", SqlDbType.VarChar, 225);
insertCmd.Parameters.Add("@PersonDOB", SqlDbType.DateTime);
insertCmd.Parameters.Add("@PersonImage", SqlDbType.Image);
insertCmd.Parameters.Add("@PersonImageType", SqlDbType.VarChar, 225);
insertCmd.Parameters["@PersonEmail"].Value = txtPersonEmail.Text;
insertCmd.Parameters["@PersonName"].Value = txtPersonName.Text;
insertCmd.Parameters["@PersonDOB"].Value = txtPersonDOB.Text;
insertCmd.Parameters["@PersonImage"].Value = ImageContent;
insertCmd.Parameters["@PersonImageType"].Value = strImageType;
try
{
conn.Open();
int flag = insertCmd.ExecuteNonQuery();
if (flag > 0)
{
//Response.Write("<script language=javascript>alert('成功添加好友记录!')</script>");
Label1.Text = "成功添加好友记录!";
}
else
{
//Response.Write("<script language=javascript>alert('添加好友记录失败,查看输入是否正确!')</script>");
Label1.Text = "添加好友记录失败,查看输入是否正确!";
}
}
catch (System.Exception ee)
{
//Response.Write("<script language=javascript>alert('" + ee.Message.ToString() + "')</script>");
Label1.Text = ee.Message.ToString();
}
finally
{
//conn.Close();
}
}
public void Return(object sender, System.EventArgs e)
{
Response.Redirect("~/DataGridShowImage.aspx");
}
}
文字和图片取出来--------------------DataGridShowImage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataGridShowImage.aspx.cs" Inherits="DataGridShowImage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
<br />
<asp:LinkButton ID="LinkButton1" runat="server" Font-Bold="True" Font-Size="X-Large"
PostBackUrl="~/AddAddressList.aspx" Width="210px">添加新同学</asp:LinkButton><br />
<asp:DataGrid ID="DG_Persons" runat="server" AutoGenerateColumns="False" BorderColor="#000000"
HeaderStyle-BackColor="#ff0000" HeaderStyle-Font-Bold="True" HeaderStyle-ForeColor="#ffffff"
HeaderStyle-HorizontalAlign="Center" ItemStyle-BackColor="Beige" Width="99%">
<Columns>
<asp:TemplateColumn HeaderText="删除">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "PersonID") %>' Text ="删除" OnClick="DeletePerson" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="姓名">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="电子邮件">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonEmail") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="出生日期">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonDOB") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="照片">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# FormatURL(DataBinder.Eval(Container.DataItem, "PersonID")) %>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid> </div>
</form>
</body>
</html>
--------------------cs代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class DataGridShowImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
string strCnn = "Data Source=.;Initial Catalog=Phone;Integrated Security=True;";
SqlConnection conn = new SqlConnection(strCnn);
SqlCommand myCommand = new SqlCommand("SELECT * FROM Person ", conn);
myCommand.CommandType = CommandType.Text;
try
{
conn.Open();
DG_Persons.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
DG_Persons.DataBind();
}
catch (SqlException SQLexc)
{
//Response.Write("提取数据时出现错误:" + SQLexc.ToString());
Response.Write( "提取数据时出现错误:" + SQLexc.ToString());
}
}
public void DeletePerson(object sender, System.EventArgs e)
{
string id = ((System.Web.UI.WebControls.Button)(sender)).CommandArgument;
string strCnn = "Data Source=.;Initial Catalog=Phone;Integrated Security=True;";
SqlConnection myConnection = new SqlConnection(strCnn);
SqlCommand myCommand = new SqlCommand("delete from Person where PersonID='" + id + "'", myConnection);
myCommand.CommandType = CommandType.Text;
try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
BindGrid();
}
catch (SqlException SQLexc)
{
//Response.Write("提取数据时出现错误:" + SQLexc.ToString());
Response.Write("提取数据时出现错误:" + SQLexc.ToString());
}
}
protected string FormatURL(object strArgument)
{
return "ReadImage.aspx?PersonID="+ strArgument.ToString();
}
}
---------------ReadImage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReadImage.aspx.cs" Inherits="ReadImage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
-------------------------CS代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class ReadImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 在此处放置用户代码以初始化页面
string strCnn = "Data Source=.;Initial Catalog=Phone;Integrated Security=True;";
SqlConnection conn = new SqlConnection(strCnn);
string strImageID = Request.QueryString["PersonID"].ToString();
SqlCommand myCommand = new SqlCommand("Select PersonImage from Person where PersonID='" + strImageID + "'", conn);
try
{
conn.Open();
SqlDataReader myDataReader;
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
if (myDataReader.Read())
{
Response.Clear();
Response.ContentType = "image / pjpeg /jpg /bng";
Response.BinaryWrite((byte[])myDataReader["PersonImage"]);
}
conn.Close();
}
catch (SqlException SQLexc)
{
}
Response.End();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}