向SQL Server数据库添加图片和文字的功能
来源:互联网 发布:金庸群侠传mac脚本 编辑:程序博客网 时间:2024/04/30 00:36
下面的代码实现向SQL Server数据库添加图片和文字的功能。
首先,在SQL查询分析器中执行下面的语句,以创建表和存储过程。
下面就是完整的代码,拷贝即可运行:
//------------------------------------------------------------------------------------------
C#版的(翻译孟老师的^_^)
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
}
}
- 向SQL Server数据库添加图片和文字的功能
- 向SQL Server数据库添加图片和文字---孟子E章
- 向SQLServer数据库添加图片和文字
- 向SQL Server数据库添加图片
- 向SQL Server数据库添加图片
- 向SQL Server数据库添加图片
- 向SQL Server数据库添加图片
- ASP.NET向SQL Server数据库添加图片
- ASP.NET向SQL Server数据库添加图片
- 用存储过程向SQL Server数据库添加图片
- ASP.NET向SQL Server数据库添加图片
- 向SQL Server数据库中添加数据
- C# 向Sql-server数据库保存图片
- SQL Server数据库添加图片读取图片
- Sql Server数据库的添加、查询、修改功能
- 向SQL Server 数据库中使用SQL Script向表添加列并指定添加列的位置
- 向SQL Server数据库中使用SQL Script向表添加列并指定添加列的位置
- SQL server 2000创建数据库角色以及向数据库角色添加用户的命令
- 从JAR包中获取资源的方法
- 有效和正确定义hashCode()和equals()
- 高智能必须具备感情吗?
- 太岁
- 请问mstcpip.h和mstcpip.cpp两个文件哪里能找到啊
- 向SQL Server数据库添加图片和文字的功能
- 显示时间
- 离职人员显示问题
- ASP.NET网站建设之代码分离
- ASP.NET网站建设之代码分离
- 我的电驴偷了, 小偷猛于虎啊
- 对oracle的blob字段的操作(插入,更新,删除)
- 不小心删了快速启动栏中的显示桌面图标,如何恢复?
- 每个.Net开发人员应该下载的十种必备工具