首页 > 编程 > .NET > 正文

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

2024-07-10 13:06:15
字体:
来源:转载
供稿:网友
<%@ 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 >
<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
}
}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表