如直接在数据库查询时:0xFFD8FFE000104A46494600010101004800480000FFDB...后面还很长。我只贴一部分
而在C#中,查询得到的是System.Byte[]
我想把值转换成和直接在数据库查询时一样的值。该怎么做呢?由于要取一个表里的图片然后插入到另一个表里去,所以是直接赋值(
请注意:这里由于特殊原因,不能使用参数SqlParameter
之类的)
可是又不能直接byte值给它,所以请大家帮下忙
而在C#中,查询得到的是System.Byte[]
我想把值转换成和直接在数据库查询时一样的值。该怎么做呢?由于要取一个表里的图片然后插入到另一个表里去,所以是直接赋值(
请注意:这里由于特殊原因,不能使用参数SqlParameter
之类的)
可是又不能直接byte值给它,所以请大家帮下忙
insert byte[]字段 into B
select byte[]字段 from A
byte[] Img = null;
FileStream fs = new FileStream(strImg, FileMode.Open, FileAccess.Read);//strImg是选择图片的路径
BinaryReader br = new BinaryReader(fs);
Img = br.ReadBytes((int)fs.Length);//Img直接存入数据库就是LZ在数据库中查询得到的那个很长的查询图片
byte[]Img=null;
SqlConnection sqlCon = "server=.;user id=sa;password=sa;database=数据库名;"
SqlCommand sqlCom = new SqlCommand("select spic from 表 where id=" + ID, sqlCon);
try
{
sqlCon.Open();
SqlDataReader dr = sqlCom.ExecuteReader();
while (dr.Read())
{
Img = (byte[])dr.GetValue(0);
}
}
catch (Exception exErr)
{
strErr = exErr.ToString();
}
finally
{
sqlCon.Close();
sqlCom.Dispose();
}
可以直接将Img传到其他数据库,结果如何我没试过 - -!
如要显示图片:
MemoryStream ms = new MemoryStream(Img);
Bitmap bmpt = new Bitmap(ms);
picgbox.Image = bmpt;
不知道能不能从数据库直接将数据传到另一个数据库,我也是初学者哇~~~
如从weservice传来了一张photo的byte[] byte[] photo=(byte[])DS.Tables[0].Rows[0]["photo"];
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand("insert into test(ID,photo) values(@ID,@photo)", connection))
{
try
{
connection.Open();
cmd.Parameters.Add(new SqlParameter("@ID",SqlDbType.NVarChar,50));
cmd.Parameters.Add(new SqlParameter("@photo",SqlDbType.Image));
cmd.Parameters["@ID"] = "ssssss";
cmd.Parameters["@photo"] = photo;
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
throw e;
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
这样可以很容易解决,问题是,不使用参数。该怎么做:
byte[] photo=(byte[])DS.Tables[0].Rows[0]["photo"];
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand("insert into test(ID,photo) values('"+ID+"',?这里要输入和数据库直接查询时一样的数据,问题是怎么转换过来)", connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
throw e;
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
你那个位置直接就用photo不行?
你那个位置直接就用photo不行?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Data.SqlClient;namespace WindowsFormsApplication14
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} void button1_Click(object sender, EventArgs e)
{
MemoryStream MS = new MemoryStream();
pictureBox1.Image.Save(MS, ImageFormat.Jpeg); // pictureBox1 原图
Byte[] ImageData = new Byte[MS.Length];
MS.Position = 0;
MS.Read(ImageData, 0, Convert.ToInt32(MS.Length));
String S = Convert.ToBase64String(ImageData); using (SqlConnection Connection = new SqlConnection("Data Source=(local);Initial Catalog=db;Integrated Security=True"))
{
Connection.Open();
// 前提 sql字段:image 不是 "image" 类型的,而是 "text"类型
SqlCommand Command = new SqlCommand("insert into z (image) values ('" + S + "')", Connection);
Command.ExecuteNonQuery(); SqlDataAdapter DataAdapter = new SqlDataAdapter("select top 1 image from z", Connection);
DataTable DT = new DataTable();
DataAdapter.Fill(DT);
byte[] B = Convert.FromBase64String(DT.Rows[0][0].ToString());
MS = new MemoryStream(B);
Image LoadedImage = Image.FromStream(MS, true);
pictureBox2.Image = LoadedImage; // 从数据库里读出放在pictureBox2里
}
}
}
}
他是2进制的 当然于你在SQL查询的结果不一样
如果 你硬要把转过来 请把 byte[] photo=(byte[])DS.Tables[0].Rows[0]["photo"];
转换成16进制
11楼的不好意思,你写了那么多,根本就没什么作用,我不是要把图片显示出来,而是要更新到webservice所的在服务器,没必要更新图片,不过还是谢谢你的代码。
StringBuilder sb = new StringBuilder();
foreach(byte b in bs)
{
sb.Append("{0:xx}", b);
}
string s = sb.ToString(); //s=0xFFD8FFE000104A46494600010101004800480000FF.......
形如:
insert into tb_test (binarycolumn, othercolumn) values (0xFFD8FFE000104A46494600010101004800480000FF, 'kdkdk');. 一个二进制流, 如何得到 hexstring: string convertBinaryToHexString(byte[] bytes)
{
System.Text.StringBuilder sb = new StringBuilder();
string ss = "0123456789ABCDEF";
char[] cc = ss.ToCharArray();
foreach (byte b in bytes)
{
sb.Append(b.ToString("X2"));
}
return sb.Insert(0, "0x").ToString();
}
我:
使用那个构造的 hexstring , 并使用显式 insert语句 (非绑定模式), 从一个图片文件读取字节流,
更新 sql server 的image 字段,
再获取出来图片用于显示, 成功.
如果提供一个功能时, 要想想完整不,
如果提供了开, 则要提供关,转换也是这样咬.不过从 hexstring 转换回 byte[] 的操作你好像用不到.
也谢谢Jimmy,不过你提供的有些错误,你本人又不在,这个功能对我很重要,所以是按帮到我程度的来给分的,不好意思了。