C# 怎么实现上传图片到数据库 代码
有一个PIctureBox控件,有一个“浏览”控钮,一个“上传”按钮,要怎么实现点浏览的时候可以把从本地图片显示到pictrueBox控件上,点上传的时候把图片写入库中,...
有一个PIctureBox控件,有一个“浏览”控钮,一个“上传”按钮,要怎么实现点浏览的时候可以把从本地图片显示到pictrueBox控件上,点上传的时候把图片写入库中,怎么实现啊,求代码。(不要WEB版的。)
展开
展开全部
我给你一个小例子,你自己看看吧,可能会由于我们数据库软件不同,可能需要修改一下。(我用的是 SQL Server)
下面是我自己做的一个类,实例化后即可使用。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace SQL_Query.MyClass
{
class ConnectionSQLClass
{
private string _server = string.Empty;
private string _database = string.Empty;
private string _uid = string.Empty;
private string _pwd = string.Empty;
private string _sqlConnection = string.Empty;
public ConnectionSQLClass(string server, string database, string uid, string pwd)
{
_server = server;
_database = database;
_uid = uid;
_pwd = pwd;
_sqlConnection = "server=" + _server + ";database=" + _database + ";uid=" + _uid + ";pwd=" + _pwd;
}
//插入图片(table 表名、fieldName 存储图片字段名、imagePath 图片完整路径)
public bool Insert_Image(string table, string fieldName, string imagePath)
{
try
{
FileStream fs = new FileStream(imagePath, FileMode.Open);
byte[] imagebytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
//打开数所
SqlConnection con = new SqlConnection(_sqlConnection);
con.Open();
SqlCommand com = new SqlCommand("insert into " + table + "(" + fieldName + ")" + " values(@ImageList)", con);
com.Parameters.Add("ImageList", SqlDbType.Image);
com.Parameters["ImageList"].Value = imagebytes;
com.ExecuteNonQuery();
con.Close();
}
catch { return false; }
return true;
}
//读取 fieldName 该字段中符合条件的所有图片(sql SQL查询语句、fieldName 存储图片字段名)
public List<Image> Get_Image(string sql, string fieldName)
{
List<Image> InformatoinCollection = new List<Image>();
SqlConnection cn = new SqlConnection(_sqlConnection);
try
{
cn.Open();
SqlCommand cm = new SqlCommand(sql, cn);
SqlDataReader dr = cm.ExecuteReader();
//MessageBox.Show(dr.HasRows.ToString());
if (!dr.HasRows)
{
return null;
}
while (dr.Read())
{
MemoryStream ms1 = new MemoryStream((byte[])dr[fieldName]);
Image image = Image.FromStream(ms1, true);
InformatoinCollection.Add(image);
}
dr.Close();
cn.Close();
}
catch
{
InformatoinCollection = null;
}
return InformatoinCollection;
}
}
}
希望对你有帮助!
下面是我自己做的一个类,实例化后即可使用。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace SQL_Query.MyClass
{
class ConnectionSQLClass
{
private string _server = string.Empty;
private string _database = string.Empty;
private string _uid = string.Empty;
private string _pwd = string.Empty;
private string _sqlConnection = string.Empty;
public ConnectionSQLClass(string server, string database, string uid, string pwd)
{
_server = server;
_database = database;
_uid = uid;
_pwd = pwd;
_sqlConnection = "server=" + _server + ";database=" + _database + ";uid=" + _uid + ";pwd=" + _pwd;
}
//插入图片(table 表名、fieldName 存储图片字段名、imagePath 图片完整路径)
public bool Insert_Image(string table, string fieldName, string imagePath)
{
try
{
FileStream fs = new FileStream(imagePath, FileMode.Open);
byte[] imagebytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
//打开数所
SqlConnection con = new SqlConnection(_sqlConnection);
con.Open();
SqlCommand com = new SqlCommand("insert into " + table + "(" + fieldName + ")" + " values(@ImageList)", con);
com.Parameters.Add("ImageList", SqlDbType.Image);
com.Parameters["ImageList"].Value = imagebytes;
com.ExecuteNonQuery();
con.Close();
}
catch { return false; }
return true;
}
//读取 fieldName 该字段中符合条件的所有图片(sql SQL查询语句、fieldName 存储图片字段名)
public List<Image> Get_Image(string sql, string fieldName)
{
List<Image> InformatoinCollection = new List<Image>();
SqlConnection cn = new SqlConnection(_sqlConnection);
try
{
cn.Open();
SqlCommand cm = new SqlCommand(sql, cn);
SqlDataReader dr = cm.ExecuteReader();
//MessageBox.Show(dr.HasRows.ToString());
if (!dr.HasRows)
{
return null;
}
while (dr.Read())
{
MemoryStream ms1 = new MemoryStream((byte[])dr[fieldName]);
Image image = Image.FromStream(ms1, true);
InformatoinCollection.Add(image);
}
dr.Close();
cn.Close();
}
catch
{
InformatoinCollection = null;
}
return InformatoinCollection;
}
}
}
希望对你有帮助!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询