谁能做一个用C#将图片保存到2000的数据库中并读取出来,弄好了发给将全部压缩发给我QQ826312723我做了好多
谁能做一个用C#将图片保存到2000的数据库中并读取出来,弄好了发给,最好将全部压缩发给及数据库,这样我更好弄的懂一些,我QQ826312723我做了好多次没有做出来,谢...
谁能做一个用C#将图片保存到2000的数据库中并读取出来,弄好了发给,最好将全部压缩发给及数据库,这样我更好弄的懂一些,我QQ826312723我做了好多次没有做出来,谢谢啦
展开
3个回答
展开全部
我给你一个小例子,你自己看看吧,可能需要修改一下。
(这是我以前学习 SQL Sever 2000 的练习)
存储方法:把图片以二进制格式读入到数据库。
下面是我自己做的一个类,实例化后即可使用。
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;
}
}
}
希望对你有帮助!
(这是我以前学习 SQL Sever 2000 的练习)
存储方法:把图片以二进制格式读入到数据库。
下面是我自己做的一个类,实例化后即可使用。
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;
}
}
}
希望对你有帮助!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询