如何在Datatbase中插入图片? [英] How Can I Insert Picture In Datatbase?

查看:108
本文介绍了如何在Datatbase中插入图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我的朋友。

i我的程序中有图片框(vb.net)。

i想在我的数据库中插入picturebox图片。

我该怎么做?

解决方案

您可以将图像保存在数据库中的文件夹和图像路径中这个



  bool  CheckFileType( string  fileName)
{
string ext = Path.GetExtension(fileName);
switch (ext.ToLower())
{
case 。gif
return true ;
case 。jpg
return true ;
case .jpeg
return true ;
case 。png
return true ;
case 。bmp
return true ;
默认
返回 false ;

}
}


受保护 void btnUpload_Click( object sender,EventArgs e)
{
if (fileupload.HasFile)
{
if (fileupload.PostedFile.ContentLength < 50000
{

if (CheckFileType( fileupload.FileName))
{

String filepath = FolderName / + fileupload.FileName;
fileupload.SaveAs(MapPath(filepath));

qry = update emp_detail set img =' + filepath.ToString ()+ 'where emp_id =' + Session [ emp_id]。ToString()+ ';
conn.Open();
SqlCommand cmd = new SqlCommand(qry,conn);
cmd.ExecuteNonQuery();
conn.Close();
}
else
{
lblmsg.Text = 仅允许jpg,jpeg,png,gif和bmp图像;
}
}
else
{
lblmsg.Text = 文件大小超过限制100KB;
}
}
}


参考 - 从图像加载图像并将图像保存到数据库 [< a href =http://www.codeguru.com/vb/gen/vb_database/sqlserver/article.php/c7427/Load-Images-from-and-Save-Images-to-a-Database.htmtarget = _blanktitle =新窗口> ^ ]。

引用:

有时你需要将图像存储在数据库中而不是物理文件中。此示例应用程序将向您展示如何构建允许您执行以下操作的Windows窗体界面:

  • 在硬盘上浏览图像
  • 将所选图像加载到PictureBox控件中以供查看
  • 将PictureBox控件中显示的图像保存到数据库
  • 从ListBox控件中选择图像,然后从数据库中加载

hi my friends.
i have picturebox in my program (vb.net).
i want insert picture of picturebox in my database.
how can i do that?

解决方案

You can save image in folder and image path in database for this

 bool CheckFileType(string fileName)
    {
        string ext = Path.GetExtension(fileName);
        switch (ext.ToLower())
        {
            case ".gif":
                return true;
            case ".jpg":
                return true;
            case ".jpeg":
                return true;
            case ".png":
                return true;
            case ".bmp":
                return true;
            default:
                return false;

        }
    }


protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (fileupload.HasFile)
        {
            if (fileupload.PostedFile.ContentLength < 50000)
            {
                
                if (CheckFileType(fileupload.FileName))
                {

                    String filepath = "FolderName/" + fileupload.FileName;
                    fileupload.SaveAs(MapPath(filepath));

                    qry = "update emp_detail set img='" + filepath.ToString() + "' where emp_id='" + Session["emp_id"].ToString() + "' ";
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(qry, conn);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                else
                {
                    lblmsg.Text = "Only jpg, jpeg, png, gif & bmp Images are Allowed";
                }
            }
            else
            {
                lblmsg.Text = "File size Exceed limit 100KB";
            }
        }
    }


Refer - Load Images from and Save Images to a Database[^].

Quote:

Sometimes you need to store images in a database instead of as physical files. This sample application will show you how to build a Windows Forms interface that allows you to do the following:

  • Browse for an image on your hard disk
  • Load the selected image into a PictureBox control for viewing
  • Save an image displayed in the PictureBox control to the database
  • Select an image from a ListBox control, and load it from the database


这篇关于如何在Datatbase中插入图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆