本站首页
|
代码实验室
|
CSS3 中文参考
|
书籍出版
|
Google 英文翻译版
|
给我留言
文章标题
文章内容
.NET 相关
ASP.NET
ASP.NET MVC
Sliverlight
Linq
C#
F#
VB.NET
Web Services
XML/XSL
.NET FAQ
Web 开发
Internet Explorer
CSS
Javascript
SQL Server
Java应用
友情链接
关于 向SQL Server数据库添加图片 的评论:
评论日期:2004年04月20日 00:00:00
评论人:smx
Dim intStatus As Integer
intStatus = ImageStream.Read(ImageContent, 0, intImageSize)
这两句不太懂!谢谢!
评论日期:2004年07月21日 00:00:00
评论人:ke
这里面的代码有错误的地方吧,我怎么把图片放不上去呀?
评论日期:2004年08月22日 00:00:00
评论人:wolfsky
孟子大哥,如果我想在有图片数据的时候显示图片,没有图片数据的纪录用其他的图片来代替,这样的话,显示图片的代码应该怎么写呢?
评论日期:2004年08月24日 00:00:00
评论人:lingwu05
孟老大:如果把图片放入到数据库里是不是读出来的时候太慢?能不能把它存入一个指定的文件夹里,名字任意,而数据库里只保存它的文件名作为链接,那么如何才能实现呢?
评论日期:2004年08月25日 00:00:00
评论人:zlb
大哥:发布一个C#版好吗?很需要啊,谢谢……
评论日期:2004年08月27日 00:00:00
评论人:cyc
给一个C#版的好吗? 谢谢了
评论日期:2004年10月05日 00:00:00
评论人:sharpcn
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
}
}
评论日期:2006年02月06日 00:00:00
评论人:leiyis
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 48: myCommand.Parameters.Add("@bookContext", SqlDbType.VarChar, 1000).Value = txtbookContext.Text;
行 49: myCommand.Parameters.Add("@bookImage", SqlDbType.Image).Value = ImageContent;
行 50: myCommand.Parameters.Add("@bookImageType", SqlDbType.VarChar, 255).Value = strImageType;
行 51:
行 52: try
这是什么错误?
评论日期:2006年02月06日 00:00:00
评论人:leiyis
<form id="Form1" method="post" runat="server" enctype="multipart/form-data">
评论日期:2006年07月11日 00:00:00
评论人:soldier
文件过大的时候会超时!传不上去!最好不要超过5m
评论日期:2007年08月30日 00:00:00
评论人:moonreplace
方法很好,不过有点小注意如果我们直接使用SQL语句往里插,而不用存储过程或者参数插就会出现问题!图片显示不出来!这是为什么?
评论日期:2008年03月19日 00:00:00
评论人:
有没有ASP的呀,孟子大哥,很需要呀,求你啦,
评论日期:2009年01月12日 11:49:35
评论人:晕死我。。。原来有C#版的
原先只是看到VB的,就将VB的翻译成了C#,搞定了,一拉到后面一看,啊。。C#版,晕死我。看到好东西太心急了~~~~~~~~~~
发表评论:
(由于众所周知的原因,评论需要审核后才能显示,请勿重复发评论。谢谢!!)
标题:
用户名:
内容:
验证码:
请输入右边图像中的文字。