如何在数据库中添加图片

如何在数据库中添加图片,第1张

我使用过,我一般是这么处理的,如下(我用的vb,你可以转变成access里的代码):

是可以放到数据库里面的,以ACCESS为例子,要使用OLE数据类型,

然后把转化为长二进制数据存入,读取的时候可以生成系统隐藏的图

片然后在控件中显示

Dim cc() As Byte

Dim i

Dim t

Private Sub Command1_Click()

CommonDialog1Filter = "JPG(BMP)|BMP"

CommonDialog1ShowOpen

If CommonDialog1FileName <> "" Then

Picture1Picture = LoadPicture(CommonDialog1FileName)

strname = CommonDialog1FileName

Open strname For Binary As #1

ReDim cc(LOF(1) - 1)

t = cc(LOF(1) - 1) '在读取二进制时,如果是用文件 *** 作那么涉及到到底从文件中取多少字节的问题,所以字节数组要指定大小

Get #1, , cc

Close

Else

MsgBox "没有选中"

End If

End Sub

Private Sub Command2_Click()

Adodc1Refresh

Adodc1RecordsetAddNew

Adodc1RecordsetFields("id") = i

Adodc1RecordsetFields("qq") = cc

i = i + 1

Adodc1RecordsetUpdate

Adodc1Refresh

End Sub

Private Sub Command3_Click()

Adodc1Refresh

Dim P() As Byte

Adodc1RecordSource = "select qq from 表一 where id='" & Text1Text & "'"

P = Adodc1RecordsetFields("qq") '当把一组二进制数据赋值给一个二进制数组时就不用指定数组的大小了,因为是全部赋值

Open AppPath & "\oobmp" For Binary As #1

Put #1, , P

Close

End Sub

Private Sub Command4_Click()

Open AppPath & "\oobmp" For Binary As #1

Put #1, , cc

Close

End Sub

Private Sub Form_Load()

i = 1

End Sub

第一步://获取当前选择的thispictureBox1Image = ImageFromStream(thisopenFileDialog1OpenFile());//获取当前的路径string path = openFileDialog1FileNameToString();//将制定路径的添加到FileStream类中FileStream fs = new FileStream(path, FileModeOpen, FileAccessRead);//通过FileStream对象实例化BinaryReader对象BinaryReader br = new BinaryReader(fs);//通过BinaryReader类对象的ReadBytes()方法将FileStream类对象转化为二进制数组byte[] imgBytesIn = brReadBytes(ConvertToInt32(fsLength));第二步://将添加到数据库中string sql="insert into pic values(@pic)";SqlParameter[] param = new SqlParameter[] { new SqlParameter("@pic", imgBytesIn) };DBHelperGetExecuteQuery(sql, param);第三步://将从数据库中取出string sql="select from pic where id=0";SqlDataReader reader = DBHelperGetExecuteReader(sql, null);MemoryStream mss = null;

作者:小风寒呐

链接:>

建议存路径

下面是上传并保存路径到数据库

显示有很多中方法,如果在gridview里面显示的就如截图这样设置

 protected void Button4_Click(object sender, EventArgs e)

    {

        string FilePath = "";

        if (FileUpload1FileName != "")

        {

            if (FileUpload1PostedFileContentLength <= 0)

            {

                // PublicFunPublicFunctionshowMsg(this, "上传文件为空,请重新选择");

                labmsgText = "上传文件为空,请重新选择";

                return;

            }

            else

            {

            }

            if (FileUpload1HasFile)

            {

                if (FileUpload1PostedFileContentLength > 4196304)

                {

                    // PublicFunPublicFunctionshowMsg(this, "上传文件过大");

                    // return;

                }

                else

                {

                    // FilePath = ServerMapPath("~/Download/Dload1");//服务器文件路径

                }

                FilePath = ServerMapPath("~/DownLoad/SignImg");//服务器文件路径

                FileLoadFunUpLoad(FilePath, FileUpload1, DropDownList2SelectedValue);

                UploadURL = "~/DownLoad/SignImg/" + DropDownList2SelectedValue + "_" + FileUpload1FileName;

                UploadURL2 = UploadURL;

                sql = " update yp_insproom_base_t set SignURL='" + UploadURL2 + "' " +

                      " where UserID='" + DropDownList2SelectedValue + "' ";

                MySqlHelperExecuteNonQuery(PublicFunPublicFunctionGetDBconstr("ce_manage_db"), sql);

                labmsgText = "上传成功";

                databind();

            }

        }

        else

        {

            // PublicFunPublicFunctionshowMsg(this, "上传文件路径错误");

            labmsgText = "上传文件路径错误";

            return;

        }

    }

以上就是关于如何在数据库中添加图片全部的内容,包括:如何在数据库中添加图片、如何将图片插入到数据库中、python 如何把图片存入数据库等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址: http://outofmemory.cn/sjk/9385008.html

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

发表评论

登录后才能评论

评论列表(0条)

保存