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

aspx:

1@ Page language="c#" Codebehind="Register.aspx.cs" AutoEventWireup="false" Inherits="shop.Register" 
 1<html>
 2<head>
 3<title>注册</title>
 4</head>
 5<body style="FONT-SIZE: 12px">
 6<form id="Form1" method="post" runat="server">
 7<font face="宋体">
 8<table align="center" bgcolor="beige" border="0" cellpadding="0" cellspacing="0" id="Table1" width="50%">
 9<tr>
10<td bgcolor="#cccc66" colspan="2" height="25" rowspan="">添加新用户</td>
11</tr>
12<tr>
13<td>姓名</td>
14<td>
15<asp:textbox id="txtPersonName" runat="server"></asp:textbox></td>
16</tr>
17<tr>
18<td>电子邮件</td>
19<td>
20<asp:textbox id="txtPersonEmail" runat="server"></asp:textbox></td>
21</tr>
22<tr>
23<td>性别</td>
24<td>
25<asp:radiobutton groupname="sex" id="sexMale" runat="server" text="男"></asp:radiobutton>
26<asp:radiobutton groupname="sex" id="sexFeMale" runat="server" text="女"></asp:radiobutton>
27</td>
28</tr>
29<tr>
30<td>出生日期</td>
31<td>
32<asp:textbox id="txtPersonDOB" runat="server"></asp:textbox></td>
33</tr>
34<tr>
35<td>照片</td>
36<td><input id="personImage" name="PersonImage" runat="server" type="file"/></td>
37</tr>
38<tr>
39<td align="center" colspan="2">
40<asp:button id="Button1" onclick="AddPerson" runat="server" text="添加"></asp:button>
41</td>
42</tr>
43</table>
44</font>
45</form>
46</body>
47</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
{
///

1<summary>   
2/// Register 的摘要说明。   
3/// </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);
}

///

1<summary>   
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改   
3/// 此方法的内容。   
4/// </summary>

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

Published At
Categories with Web编程
Tagged with
comments powered by Disqus