如题,望高手帮忙~
解决方案 »
- 用SelectSingleNode()方法,如何获取XML指定值的节点
- 关键字:汉字拼音、内码的问题
- 关于c#数据查询问题
- sql:将数据类型 varchar 转换为 float 时出错
- C# SerialPort 类与读卡器的问题,请高手指教
- [简单问题]运算符“==”无法应用于“object”和“int”类型的操作数.
- 商城网站资金管理(包括下订单,用户充值,用户提成,用户提现)
- help me
- 这个类怎么写(特简单问题)??
- 一个很菜的问题,但我已想了二天了?? 关于类间传值!!在线等!!!
- Webclice的OpenRead() 和FtpWebResponse的GetResponseStream()有什么区别?
- socket中 每10秒获取一次数据
public bool Add_Spxx(c_spxx spxx)
{
try
{
bool flag = false;
string sql = "EXEC hyxs_spxx_ADD @spbh,@spmc,@tp";
SqlParameter[] para = new SqlParameter[]{
new SqlParameter("@spbh",spxx.spbh),
new SqlParameter("@spmc",spxx.spmc),
new SqlParameter("@tp",spxx.tp) //图片
};
if (DbHelperSQL.GetScalar(sql, para) == 0)
{
flag = true;
}
return flag;
}
catch (Exception ce)
{
throw ce;
}
finally
{
DbHelperSQL.ConnClose();
}
}
/// <summary>
/// 把指定图片转换为byte
/// </summary>
/// <param name="path">图形路径</param>
/// <returns></returns>
public static byte[] GetPhoto(string path)
{
string str = path;
byte[] photo = new byte[0];
if (File.Exists(path))
{
FileStream file = new FileStream(str, FileMode.Open, FileAccess.Read);
photo = new byte[file.Length];
file.Read(photo, 0, photo.Length); file.Close(); } return photo;
}
#endregion
byte[] picbyte;
OpenFileDialog ofdSelectPic = new OpenFileDialog();
if (ofdSelectPic.ShowDialog() == DialogResult.OK)
{
if ((ms = ofdSelectPic.OpenFile()) != null)
{
picbyte = new byte[ms.Length];
ms.Position = 0;
ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ""; sql = "Insert into Person(Photo) values(@Image)";
SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.Add("@Image", SqlDbType.VarBinary);
cmd.Parameters["@Image"].Value = picbyte; conn.Open();
cmd.ExecuteNonQuery();
conn.Close(); ms.Close();
}
} SqlConnection conn=new SqlConnection();
conn.ConnectionString="";
string strSql="";
SqlCommand cmd=new SqlCommand(strSql,conn);
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
MemoryStream ms=new MemoryStream((byte[])reader["Photo"]);
Image image=Image.FromStream(ms,true);
reader.Close();
conn.Close();
picturebox1.Image=image;
FileStream mystream=new FileStream("D:\\A.jpg",FileMode.Open,FileAccess.Read);
long len=mystream.Length;
mycmd.Parameters.Add("@image",SqlDbType.Image,(int)len,"picture");
mycmd.Parameters["@image"].Direction=System.Data.ParameterDirection.Input;
byte []box=new byte[len];
mystream.Read(box,0,(int)len);
mycmd.Parameters["@image"].Value=box;
本实例主要介绍如何将图片存入数据库。将图片存入数据库,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStream类、BinaryReader把图片读成字节的形式,赋给一个字节数组,然后用ADO.SqlCommand对象的ExecuteNonQuery()方法来把数据保存到数据库中。主要代码如下:
/// <summary>
/// 将图片以二进制读入数据库
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fullpath = openFileDialog1.FileName;//文件路径
FileStream fs = new FileStream(fullpath, FileMode.Open);
byte[] imagebytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
//打开数所
SqlConnection con = new SqlConnection("server=(local);database=zy_info;integrated security=true");
con.Open();
SqlCommand com = new SqlCommand("insert into tb_text values(@guatu)", con);
//"update yijing set guatu='"+
com.Parameters.Add("guatu", SqlDbType.Image);
com.Parameters["guatu"].Value = imagebytes;
com.ExecuteNonQuery();
con.Close();
MemoryStream ms = new MemoryStream(imagebytes);
Bitmap bmpt = new Bitmap(ms);
this.pictureBox1.Image = bmpt;
}
}
2、图片读出数据库
/// <summary>
/// 读出二进制图片数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
byte[] imagebytes = null;
//打开数据库
SqlConnection con = new SqlConnection("server=(local);database=zy_info;integrated security=true");
con.Open();
SqlCommand com = new SqlCommand("select top 1* from tb_text", con);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
imagebytes = (byte[])dr["guatu"];
}
dr.Close();
com.Clone();
con.Close();
MemoryStream ms = new MemoryStream(imagebytes);
Bitmap bmpt = new Bitmap(ms);
this.pictureBox1.Image = bmpt;
//dr.Close();
//com.Clone();
//con.Close();
}
这个可以成功的!