ASP.NET向SQL Server数据库添加图片

王朝c#·作者佚名  2006-12-17
窄屏简体版  字體: |||超大  

ASP.NET向SQL Server数据库添加图片

ASP.NET向SQL Server数据库添加图片 aspx:

<%@ Page language='c#' Codebehind='Register.aspx.cs' AutoEventWireup='false' Inherits='shop.Register' %>

<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN' >

<HTML>

<HEAD>

<title>注册</title>

</HEAD>

<body style='FONT-SIZE: 12px'>

<form id='Form1' method='post' runat='server'>

<FONT face='宋体'>

<TABLE id='Table1' cellSpacing='0' cellPadding='0' width='50%' align='center' bgColor='beige'

border='0'>

<TR>

<TD bgColor='#cccc66' colSpan='2' height='25' rowSpan=''>添加新用户</TD>

</TR>

<TR>

<TD>姓名</TD>

<TD>

<asp:TextBox id='txtPersonName' runat='server'></asp:TextBox></TD>

</TR>

<TR>

<TD>电子邮件</TD>

<TD>

<asp:TextBox id='txtPersonEmail' runat='server'></asp:TextBox></TD>

</TR>

<TR>

<TD>性别</TD>

<TD>

<asp:RadioButton GroupName='sex' Text='男' ID='sexMale' Runat='server' />

<asp:RadioButton GroupName='sex' Text='女' ID='sexFeMale' Runat='server' />

</TD>

</TR>

<TR>

<TD>出生日期</TD>

<TD>

<asp:TextBox id='txtPersonDOB' runat='server'></asp:TextBox></TD>

</TR>

<TR>

<TD>照片</TD>

<TD><INPUT type='file' id='personImage' name='PersonImage' runat='server'></TD>

</TR>

<TR>

<TD align='center' colSpan='2'>

<asp:Button id='Button1' OnClick='AddPerson' runat='server' Text='添加'></asp:Button>

</TD>

</TR>

</TABLE>

</FONT>

</form>

</body>

</HTML>

aspx.cs:

using System;

using System.IO;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Configuration;

namespace shop

{

/// <summary>

/// Register 的摘要说明。

/// </summary>

public class Register : System.Web.UI.Page

{

protected System.Web.UI.WebControls.TextBox txtPersonName;

protected System.Web.UI.WebControls.TextBox txtPersonEmail;

protected System.Web.UI.WebControls.RadioButton sexMale;

protected System.Web.UI.WebControls.RadioButton sexFeMale;

protected System.Web.UI.WebControls.TextBox txtPersonDOB;

protected System.Web.UI.HtmlControls.HtmlInputFile personImage;

protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)

{

// 在此处放置用户代码以初始化页面

}

public void AddPerson(object sender, System.EventArgs e)

{

int intImageSize;

string strImageType;

Stream ImageStream;

intImageSize = personImage.PostedFile.ContentLength; // 文件大小

strImageType = personImage.PostedFile.ContentType; // 文件类型

ImageStream = personImage.PostedFile.InputStream;

byte[] ImageContent = new byte[intImageSize];

int intStatus = ImageStream.Read(ImageContent, 0, intImageSize);

// 写入数据库

string strConn = ConfigurationSettings.AppSettings['ConnectionString'];

SqlConnection myConnection = new SqlConnection(strConn);

SqlCommand myCommand = new SqlCommand('sp_person_isp', myConnection);

myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.Add('@PersonEmail', SqlDbType.VarChar, 255).Value = txtPersonEmail.Text;

myCommand.Parameters.Add('@PersonName', SqlDbType.VarChar, 255).Value = txtPersonName.Text;

myCommand.Parameters.Add('@PersonSex', SqlDbType.Char, 1);

if(sexMale.Checked)

myCommand.Parameters['@PersonSex'].Value = 'M';

else

myCommand.Parameters['@PersonSex'].Value = 'F';

myCommand.Parameters.Add('@PersonDOB', SqlDbType.DateTime).Value = txtPersonDOB.Text;

myCommand.Parameters.Add('@PersonImage', SqlDbType.Image).Value = ImageContent;

myCommand.Parameters.Add('@PersonImageType', SqlDbType.VarChar, 255).Value = strImageType;

try

{

myConnection.Open();

myCommand.ExecuteNonQuery();

myConnection.Close();

Response.Write('添加成功!');

}

catch(System.Exception SQLExe)

{

Response.Write('添加失败!原因:'+SQLExe.ToString());

}

}

#region Web 窗体设计器生成的代码

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

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航