请教 如何在数据库里面添加图片 如何吊出显示在网页上面

请教 如何在数据库里面添加图片 如何吊出显示在网页上面,第1张

添加和显示的时候都要用流文件的格式。

保存images进SQL Server数据库

为了保存到table你首先得从客户端上传它们到你的web服务器。你可以创建一个web form,用TextBox得到的标题,用HTML File Server Control得到文件。确信你设定了Form的encType属性为multipart/form-data。

Stream imgdatastream = File1PostedFileInputStream;

int imgdatalen = File1PostedFileContentLength;

string imgtype = File1PostedFileContentType;

string imgtitle = TextBox1Text;

byte[] imgdata = new byte[imgdatalen];

int n = imgdatastreamRead(imgdata,0,imgdatalen);

string connstr=

((NameValueCollection)ContextGetConfig

("appSettings"))["connstr"];

SqlConnection connection = new SqlConnection(connstr);

SqlCommand command = new SqlCommand

("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)

VALUES ( @imgtitle, @imgtype,@imgdata )", connection );

SqlParameter paramTitle = new SqlParameter

("@imgtitle", SqlDbTypeVarChar,50 );

paramTitleValue = imgtitle;

commandParametersAdd( paramTitle);

SqlParameter paramData = new SqlParameter

( "@imgdata", SqlDbTypeImage );

paramDataValue = imgdata;

commandParametersAdd( paramData );

SqlParameter paramType = new SqlParameter

( "@imgtype", SqlDbTypeVarChar,50 );

paramTypeValue = imgtype;

commandParametersAdd( paramType );

connectionOpen();

int numRowsAffected = commandExecuteNonQuery();

connectionClose();

一段net windows应用程序的代码

#region//将写入数据库

        /// <summary>

        /// 存储

        /// </summary>

        /// <param name="openF">打开文件对话框</param>

        /// <param name="cb">下拉列表框</param>

        /// <param name="pb">框</param>

        public void SaveImage(OpenFileDialog openF, ComboBox cb, PictureBox pb)//将以二进制存入数据库中

        {

            try

            {

                string strimg = openFFileNameToString();  //记录的所在路径

                FileStream fs = new FileStream(strimg, FileModeOpen, FileAccessRead); //将以文件流的形式进行保存

                BinaryReader br = new BinaryReader(fs);

                byte[] imgBytesIn = brReadBytes((int)fsLength);  //将流读入到字节数组中

                connOpen();

                StringBuilder strSql = new StringBuilder();

                strSqlAppend("insert into tb_pic(name,img) values(@Name,@Photo)");

                SqlCommand cmd = new SqlCommand(strSqlToString(), conn);

                cmdParametersAdd("@Photo", SqlDbTypeBinary)Value = imgBytesIn;

                cmdParametersAdd("@Name", SqlDbTypeText)Value = strimg;

                if (cmdExecuteNonQuery() == 1)

                    MessageBoxShow("上传成功");

                connClose();

                BindPicId(cb);

            }

            catch (Exception exp)

            {

                MessageBoxShow(expToString());

            }

        }

        #endregion

通常对用户上传的需要保存到数据库中。解决方法一般有两种:一种是将保存的路径存储到数据库;另一种是将以二进制数据流的形式直接写入数据库字段中。以下为具体方法:

一、保存的上传路径到数据库:

string uppath="";//用于保存上传路径

//获取上传的文件名

string fileFullname = thisFileUpload1FileName;

//获取上传的时间,以时间作为的名字可以防止重名

string dataName = DateTimeNowToString("yyyyMMddhhmmss");

//获取的文件名(不含扩展名)

string fileName = fileFullnameSubstring(fileFullnameLastIndexOf("\\") + 1);

//获取扩展名

string type = fileFullnameSubstring(fileFullnameLastIndexOf("") + 1);

//判断是否为要求的格式

if (type == "bmp" || type == "jpg" || type == "jpeg" || type == "gif" || type == "JPG" || type == "JPEG" || type == "BMP" || type == "GIF")

{

//将上传到指定路径的文件夹

thisFileUpload1SaveAs(ServerMapPath("~/upload") + "\\" + dataName + "" + type);

//将路径保存到变量,将该变量的值保存到数据库相应字段即可

uppath = "~/upload/" + dataName + "" + type;

}

二、将以二进制数据流直接保存到数据库:

引用如下命名空间:

using SystemDrawing;

using SystemIO;

using SystemDataSqlClient;

设计数据库时,表中相应的字段类型为iamge

保存:

//路径

string strPath = thisFileUpload1PostedFileFileNameToString ();

//读取

FileStream fs = new SystemIOFileStream(strPath, FileModeOpen, FileAccessRead);

BinaryReader br = new BinaryReader(fs);

byte[] photo = brReadBytes((int)fsLength);

brClose();

fsClose();

//存入

SqlConnection myConn = new SqlConnection("Data Source=;Initial Catalog=stumanage;User ID=sa;Password=123");

string strComm = " INSERT INTO stuInfo(stuid,stuimage) VALUES(107,@photoBinary )";// *** 作数据库语句根据需要修改

SqlCommand myComm = new SqlCommand(strComm, myConn);

myCommParametersAdd("@photoBinary", SqlDbTypeBinary, photoLength);

myCommParameters["@photoBinary"]Value = photo;

myConnOpen();

if (myCommExecuteNonQuery() > 0)

{

thisLabel1Text = "ok";

}

myConnClose();

读取:

连接数据库字符串省略

myconOpen();

SqlCommand command = new

SqlCommand("select stuimage from stuInfo where stuid=107", mycon);//查询语句根据需要修改

byte[] image = (byte[])commandExecuteScalar ();

//指定从数据库读取出来的的保存路径及名字

string strPath = "~/Upload/zhangsanJPG";

string strPhotoPath = ServerMapPath(strPath);

//按上面的路径与名字保存文件

BinaryWriter bw = new BinaryWriter(FileOpen(strPhotoPath,FileModeOpenOrCreate));

bwWrite(image);

bwClose();

//显示

thisImage1ImageUrl = strPath;

采用俩种方式可以根据实际需求灵活选择。

以上就是关于请教 如何在数据库里面添加图片 如何吊出显示在网页上面全部的内容,包括:请教 如何在数据库里面添加图片 如何吊出显示在网页上面、sql server 2008数据库中怎么插入图片、图片如何存入数据库等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10193069.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存