/////////////////SQL//////////////////// if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_person_isp]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_person_isp] GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Person]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[Person] GOCREATE TABLE [dbo].[Person] ( [PersonID] [int] IDENTITY (1, 1) NOT NULL , [PersonEmail] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL , [PersonName] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL , [PersonSex] [char] (1) COLLATE Chinese_PRC_CI_AS NULL , [PersonDOB] [datetime] NULL , [PersonImage] [image] NULL , [PersonImageType] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GOSET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GOCreate Proc sp_person_isp @PersonEmail Varchar(255), @PersonName Varchar(255), @PersonSex Char(1), @PersonDOB DateTime, @PersonImage Image, @PersonImageType Varchar(255) As Begin Insert into Person (PersonEmail, PersonName, PersonSex, PersonDOB, PersonImage, PersonImageType) Values (@PersonEmail, @PersonName, @PersonSex, @PersonDOB, @PersonImage, @PersonImageType) End GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /////////////////SQL////////////////////
//////////////////WebForm1.aspx///////// <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Data.SqlClient" %> <%@ Import Namespace="System.Data" %> <%@ Page Language="vb" %> <HTML> <HEAD> <title>向SQL Server插入图片</title> <script runat="server"> Public Sub AddPerson(sender As Object, e As EventArgs) Dim intImageSize As Int64 Dim strImageType As String Dim ImageStream As Stream ' 获得图片的大小 intImageSize = PersonImage.PostedFile.ContentLength ' 获得图片类型 strImageType = PersonImage.PostedFile.ContentType '读取图片 ImageStream = PersonImage.PostedFile.InputStream Dim ImageContent(intImageSize) As Byte Dim intStatus As Integer intStatus = ImageStream.Read(ImageContent, 0, intImageSize) ' 创建Connection和Command对象 Dim strCnn As String = "Data Source=.;Initial Catalog=Test;User Id=sa;Password=sa;" Dim myConnection As New SqlConnection(strCnn) Dim myCommand As New SqlCommand("sp_person_isp", myConnection) ' 使用存储过程 myCommand.CommandType = CommandType.StoredProcedure ' 向存储过程添加参数 Dim prmEmail As New SqlParameter("@PersonEmail", SqlDbType.VarChar, 255) prmEmail.Value = txtPersonEmail.Text myCommand.Parameters.Add(prmEmail) Dim prmName As New SqlParameter("@PersonName", SqlDbType.VarChar, 255) prmName.Value = txtPersonName.Text myCommand.Parameters.Add(prmName) Dim prmSex As New SqlParameter("@PersonSex", SqlDbType.Char, 1) If sexMale.Checked Then prmSex.Value = "M" Else prmSex.Value = "F" End If myCommand.Parameters.Add(prmSex)
Dim prmPersonDOB As New SqlParameter("@PersonDOB", SqlDbType.DateTime) prmPersonDOB.Value = txtPersonDob.Text myCommand.Parameters.Add(prmPersonDOB) Dim prmPersonImage As New SqlParameter("@PersonImage", SqlDbType.Image) prmPersonImage.Value = ImageContent myCommand.Parameters.Add(prmPersonImage) Dim prmPersonImageType As New SqlParameter("@PersonImageType", SqlDbType.VarChar, 255) prmPersonImageType.Value = strImageType myCommand.Parameters.Add(prmPersonImageType) Try myConnection.Open() myCommand.ExecuteNonQuery() myConnection.Close() Response.Write("添加成功!") Catch SQLexc As SqlException Response.Write("添加失败,原因:" & SQLexc.ToString()) End Try End Sub </script> </HEAD> <body style="FONT: 9pt 宋体"> <form enctype="multipart/form-data" runat="server" ID="Form1"> <asp:Table Runat="server" Width="50%" BorderWidth="1" BackColor="Beige" ID="Table1" Font-Name="宋体" Font-Size="9pt"> <asp:TableRow> <asp:TableCell ColumnSpan="2" BackColor="#ff0000"> <asp:Label ForeColor="#ffffff" font-bold="True" Runat="server" Text="添加新用户" ID="Label1" /> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell HorizontalAlign="Right"> <asp:Label Runat="server" Text="姓名" ID="Label2" /> </asp:TableCell> <asp:TableCell> <asp:TextBox id="txtPersonName" Runat="server" /> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell HorizontalAlign="Right"> <asp:Label Runat="server" Text="电子邮件" ID="Label3" /> </asp:TableCell> <asp:TableCell> <asp:TextBox id="txtPersonEmail" Runat="server" /> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell HorizontalAlign="Right"> <asp:Label Runat="server" Text="性别" ID="Label4"/> </asp:TableCell> <asp:TableCell> <asp:RadioButton GroupName="sex" Text="男" ID="sexMale" Runat="server" /> <asp:RadioButton GroupName="sex" Text="女" ID="sexFeMale" Runat="server" /> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell HorizontalAlign="Right"> <asp:Label Runat="server" Text="出生日期" ID="Label5"/> </asp:TableCell> <asp:TableCell> <asp:TextBox id="txtPersonDOB" Runat="server" /> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell HorizontalAlign="Right"> <asp:Label Runat="server" Text="照片" ID="Label6"/> </asp:TableCell> <asp:TableCell> <input type="file" id="PersonImage" runat="server" NAME="PersonImage" /></asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell ColumnSpan="2" HorizontalAlign="Center"> <asp:Button Text=" 添 加 " OnClick="AddPerson" Runat="server" ID="Button1"/> </asp:TableCell> </asp:TableRow> </asp:Table> </form> </body> </HTML>//////////////////WebForm1.aspx///////// //////////////////WebForm2.aspx///////// <%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="Image005.WebForm2" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>WebForm2</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <FONT face="宋体"> <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 208px; POSITION: absolute; TOP: 56px" runat="server" Text="Button" Width="144px" Height="72px"></asp:Button></FONT> </form> </body> </HTML>//////////////////WebForm2.aspx///////////////////////////WebForm2.aspx.cs////// using System; using System.Collections; using System.ComponentModel; using System.Data; 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.Data.SqlClient;namespace Image005 { /// <summary> /// WebForm2 的摘要说明。 /// </summary> public class WebForm2 : System.Web.UI.Page { protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); }
http://blog.csdn.net/knight94/archive/2006/03/24/637800.aspx
http://blog.csdn.net/knight94/archive/2006/03/31/645987.aspx
我给的方法是可以的。
byte[] imagebytes=null;
FileStream fs=new FileStream(Image_path,FileMode.Open);
BinaryReader br=new BinaryReader(fs);
imagebytes=br.ReadBytes(br.Length);
SqlParameter parInput22=cmd.Parameters.Add("@员工图片",SqlDbType.Image);
parInput22.Direction=ParameterDirection.Input;
cmd.Parameters["@员工图片"].Value=imagebytes;
cmd.ExecuteNonQuery();
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_person_isp]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_person_isp]
GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Person]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Person]
GOCREATE TABLE [dbo].[Person] (
[PersonID] [int] IDENTITY (1, 1) NOT NULL ,
[PersonEmail] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
[PersonName] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
[PersonSex] [char] (1) COLLATE Chinese_PRC_CI_AS NULL ,
[PersonDOB] [datetime] NULL ,
[PersonImage] [image] NULL ,
[PersonImageType] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GOSET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GOCreate Proc sp_person_isp
@PersonEmail Varchar(255),
@PersonName Varchar(255),
@PersonSex Char(1),
@PersonDOB DateTime,
@PersonImage Image,
@PersonImageType Varchar(255)
As
Begin
Insert into Person
(PersonEmail, PersonName, PersonSex,
PersonDOB, PersonImage, PersonImageType)
Values
(@PersonEmail, @PersonName, @PersonSex,
@PersonDOB, @PersonImage, @PersonImageType)
End
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/////////////////SQL////////////////////
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<%@ Page Language="vb" %>
<HTML>
<HEAD>
<title>向SQL Server插入图片</title>
<script runat="server">
Public Sub AddPerson(sender As Object, e As EventArgs)
Dim intImageSize As Int64
Dim strImageType As String
Dim ImageStream As Stream
' 获得图片的大小
intImageSize = PersonImage.PostedFile.ContentLength
' 获得图片类型
strImageType = PersonImage.PostedFile.ContentType
'读取图片
ImageStream = PersonImage.PostedFile.InputStream
Dim ImageContent(intImageSize) As Byte
Dim intStatus As Integer
intStatus = ImageStream.Read(ImageContent, 0, intImageSize)
' 创建Connection和Command对象
Dim strCnn As String = "Data Source=.;Initial Catalog=Test;User Id=sa;Password=sa;"
Dim myConnection As New SqlConnection(strCnn)
Dim myCommand As New SqlCommand("sp_person_isp", myConnection)
' 使用存储过程
myCommand.CommandType = CommandType.StoredProcedure
' 向存储过程添加参数
Dim prmEmail As New SqlParameter("@PersonEmail", SqlDbType.VarChar, 255)
prmEmail.Value = txtPersonEmail.Text
myCommand.Parameters.Add(prmEmail) Dim prmName As New SqlParameter("@PersonName", SqlDbType.VarChar, 255)
prmName.Value = txtPersonName.Text
myCommand.Parameters.Add(prmName)
Dim prmSex As New SqlParameter("@PersonSex", SqlDbType.Char, 1) If sexMale.Checked Then
prmSex.Value = "M"
Else
prmSex.Value = "F"
End If
myCommand.Parameters.Add(prmSex)
Dim prmPersonDOB As New SqlParameter("@PersonDOB", SqlDbType.DateTime)
prmPersonDOB.Value = txtPersonDob.Text
myCommand.Parameters.Add(prmPersonDOB) Dim prmPersonImage As New SqlParameter("@PersonImage", SqlDbType.Image)
prmPersonImage.Value = ImageContent
myCommand.Parameters.Add(prmPersonImage) Dim prmPersonImageType As New SqlParameter("@PersonImageType", SqlDbType.VarChar, 255)
prmPersonImageType.Value = strImageType
myCommand.Parameters.Add(prmPersonImageType) Try
myConnection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()
Response.Write("添加成功!")
Catch SQLexc As SqlException
Response.Write("添加失败,原因:" & SQLexc.ToString())
End Try
End Sub
</script>
</HEAD>
<body style="FONT: 9pt 宋体">
<form enctype="multipart/form-data" runat="server" ID="Form1">
<asp:Table Runat="server" Width="50%" BorderWidth="1" BackColor="Beige" ID="Table1"
Font-Name="宋体" Font-Size="9pt">
<asp:TableRow>
<asp:TableCell ColumnSpan="2" BackColor="#ff0000">
<asp:Label ForeColor="#ffffff" font-bold="True" Runat="server" Text="添加新用户" ID="Label1" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell HorizontalAlign="Right">
<asp:Label Runat="server" Text="姓名" ID="Label2" />
</asp:TableCell>
<asp:TableCell>
<asp:TextBox id="txtPersonName" Runat="server" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell HorizontalAlign="Right">
<asp:Label Runat="server" Text="电子邮件" ID="Label3" />
</asp:TableCell>
<asp:TableCell>
<asp:TextBox id="txtPersonEmail" Runat="server" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell HorizontalAlign="Right">
<asp:Label Runat="server" Text="性别" ID="Label4"/>
</asp:TableCell>
<asp:TableCell>
<asp:RadioButton GroupName="sex" Text="男" ID="sexMale" Runat="server" />
<asp:RadioButton GroupName="sex" Text="女" ID="sexFeMale" Runat="server" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell HorizontalAlign="Right">
<asp:Label Runat="server" Text="出生日期" ID="Label5"/>
</asp:TableCell>
<asp:TableCell>
<asp:TextBox id="txtPersonDOB" Runat="server" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell HorizontalAlign="Right">
<asp:Label Runat="server" Text="照片" ID="Label6"/>
</asp:TableCell>
<asp:TableCell>
<input type="file" id="PersonImage" runat="server" NAME="PersonImage" /></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell ColumnSpan="2" HorizontalAlign="Center">
<asp:Button Text=" 添 加 " OnClick="AddPerson" Runat="server" ID="Button1"/>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</form>
</body>
</HTML>//////////////////WebForm1.aspx/////////
//////////////////WebForm2.aspx/////////
<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="Image005.WebForm2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 208px; POSITION: absolute; TOP: 56px" runat="server"
Text="Button" Width="144px" Height="72px"></asp:Button></FONT>
</form>
</body>
</HTML>//////////////////WebForm2.aspx///////////////////////////WebForm2.aspx.cs//////
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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.Data.SqlClient;namespace Image005
{
/// <summary>
/// WebForm2 的摘要说明。
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
//int ImgID = Convert.ToInt32(ImgIDTextBox.Text); string ConnStr = "Data Source=.;Initial Catalog=Test;User Id=sa;Password=sa;";
string query = "SELECT * FROM Person WHERE PersonID = @ImageID"; SqlCommand myCommand = new SqlCommand(query, new SqlConnection(ConnStr));
myCommand.Parameters.Add("@ImageID", SqlDbType.Int);
myCommand.Parameters["@ImageID"].Value = 1;
myCommand.Connection.Open(); SqlDataReader dr = myCommand.ExecuteReader(); if(dr.Read())
{
Response.ContentType = (string)dr["PersonImageType"];
Response.OutputStream.Write((byte[])dr["PersonImage"], 0, 200000);
}
else
{
Response.Write("没有这个图片的ID号");
Response.End();
} dr.Close();
myCommand.Connection.Close(); }
}
}//////////////////WebForm2.aspx.cs//////