以二进制形式将图片保存到数据库,用存诸过程实现(完整代码+sql语句)
作者:互联网
int intImageSize;
string strImageType;
Stream ImageStream;
//gets the size of the image
intImageSize =this.File1.PostedFile.ContentLength;
//gets the image type
strImageType=this.File1.PostedFile.ContentType;
//reads the image
ImageStream = this.File1.PostedFile.InputStream;
byte [] ImageContent = new byte[intImageSize];
int intStatus;
intStatus=ImageStream.Read(ImageContent,0,intImageSize);
string str=ConfigurationSettings.AppSettings["connString"];
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("sp_person_isp",con);
cmd.CommandType=CommandType.StoredProcedure;
//parameters
SqlParameter prmemail;
prmemail = new SqlParameter("@PersonEmail",SqlDbType.VarChar);
prmemail.Value=this.TextBox4.Text;
cmd.Parameters.Add(prmemail);
SqlParameter prmname;
prmname=new SqlParameter("@PersonName",SqlDbType.VarChar);
prmname.Value=this.TextBox1.Text;
cmd.Parameters.Add(prmname);
SqlParameter prmimaget;
prmimaget = new SqlParameter("@PersonImageType",SqlDbType.VarChar);
prmimaget.Value=strImageType;
cmd.Parameters.Add(prmimaget);
SqlParameter prmdata;
prmdata= new SqlParameter("@PersonDOB",SqlDbType.DateTime);
prmdata.Value=this.TextBox3.Text;
cmd.Parameters.Add(prmdata);
SqlParameter prmper ;
prmper=new SqlParameter("@PersonImage",SqlDbType.Image);
prmper.Value=ImageContent;
cmd.Parameters .Add(prmper);
SqlParameter prmSex;
prmSex=new SqlParameter("@PersonSex",SqlDbType.Char,1);
if(sexMale.Checked==true)
prmSex.Value="M";
else
prmSex.Value="F";
cmd.Parameters.Add(prmSex);
try
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write("你已成功上传了你的信息!!");
}
catch(SqlException er)
{
Response.Write("失败:"+er.Message);
}
//SQL部分
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Person]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Person]
GO
CREATE 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]
GO
--//create a procute
create 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
原文链接:https://blog.csdn.net/lovechenlila/article/details/671121
搜索
复制
标签:用存,SqlDbType,二进制,cmd,Value,sql,new,NULL,SqlParameter 来源: https://www.cnblogs.com/stdxxd/p/16218381.html