关于SQL Server 读取byte[]类型数组问题 搞了一天了,写入的和读取出来的数组总是对不上号,实在不知道怎么办了,求大婶们帮忙写个简单的范例。全分送上了~数据库为SQL server.储存类型为image。写入的数组为byte[] aaa =new byte[1024].读取的数组为byte[] bbb. 不胜感激 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=s"); conn.Open(); string strSql = "select image from table "; SqlCommand cmd = new SqlCommand(strSql, conn); SqlDataReader sdr = cmd.ExecuteReader(); sdr.Read(); MemoryStream ms = new MemoryStream((byte[])sdr[0]); Image image = Image.FromStream(ms); sdr.Close(); conn.Close(); pictureBox1.Image = image;参考http://www.cnblogs.com/zqn518/archive/2012/04/28/2475664.html 哥们,这都是转换图片的,我不想搞图片,就是把一个byte[]存入和读取,我现在重点是读取和写入不正确,所以我想要具体写入和读取这两快的代码 SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=s"); conn.Open(); string strSql = "select image from table "; SqlCommand cmd = new SqlCommand(strSql, conn); SqlDataReader sdr = cmd.ExecuteReader(); sdr.Read(); MemoryStream ms = new MemoryStream((byte[])sdr[0]); Image image = Image.FromStream(ms); sdr.Close(); conn.Close(); pictureBox1.Image = image; 我也曾经遇到过这个问题,插入和读取出来的二进制不一样,我想你很可能是因为你的插入代码写的有失误。这里给你个范例,写入和读出//写入:string stringcon = ConfigurationManager.ConnectionStrings["Connectionstring"].ConnectionString;SqlConnection Con = new SqlConnection(stringcon);String SqlCmd = "INSERT INTO indexsql(Image,, ImageSize)values(@Image,@ImageSize)";//indexsql为数据表,改成你的//出现写入和读出数据不一致的原因有可能你的SQL语句为insert into indexsql image='aaa';直接给赋值了SqlCommand CmdObj = new SqlCommand(SqlCmd, Con); Con.Open(); CmdObj.Parameters.Add("@Image", SqlDbType.Image); CmdObj.Parameters.Add("@ImageSize", SqlDbType.Int); CmdObj.Parameters["@Image"].Value = aaa;//这里为你的数组,即图片 CmdObj.Parameters["@ImageSize"].Value = UpFile.ContentLength; //图片的大小,upFile为上传控件 CmdObj.ExecuteNonQuery();//读出:int ImgID = Convert.ToInt32(Request.QueryString["id"]); string stringcon = ConfigurationManager.ConnectionStrings["Connectionstring"].ConnectionString;SqlConnection Con = new SqlConnection(stringcon );String SqlCmd = "select * from indexsql where id="+ImgID ;SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);Con.Open();SqlDataReader SqlReader = CmdObj.ExecuteReader();SqlReader.Read(); Response.OutputStream.Write ((byte[])SqlReader["Image"], 0, (int)SqlReader["ImageSize"]);//读出并显示 读取的数组 byte[] bbb= (byte[])SqlReader["Image"]; Response.End();Con.Close(); }这样应该就可以了,楼主给分吧! 关于多线程的问题 C#掉用Print2Flash设置大小和分页打印 c#字符串中\0如何操作?正则表达式如何捕获\0 C# Form为啥加载图片后,变得很卡 C# 如何实现记住用户名和密码? C#中如何接收指定某个进程的封包 关于textbox 页面滚动的问题 新手请教(为什么连不上远程数据库) 散分!关于项目的解决方案.考虑用WebService. mdi父窗体与子窗体的位置 C# dataset!!!! SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59...
conn.Open();
string strSql = "select image from table ";
SqlCommand cmd = new SqlCommand(strSql, conn);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
MemoryStream ms = new MemoryStream((byte[])sdr[0]);
Image image = Image.FromStream(ms);
sdr.Close();
conn.Close();
pictureBox1.Image = image;参考http://www.cnblogs.com/zqn518/archive/2012/04/28/2475664.html
conn.Open();
string strSql = "select image from table ";
SqlCommand cmd = new SqlCommand(strSql, conn);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
MemoryStream ms = new MemoryStream((byte[])sdr[0]);
Image image = Image.FromStream(ms);
sdr.Close();
conn.Close();
pictureBox1.Image = image;
//写入:
string stringcon = ConfigurationManager.ConnectionStrings["Connectionstring"].ConnectionString;
SqlConnection Con = new SqlConnection(stringcon);
String SqlCmd = "INSERT INTO indexsql(Image,, ImageSize)values(@Image,@ImageSize)";//indexsql为数据表,改成你的
//出现写入和读出数据不一致的原因有可能你的SQL语句为insert into indexsql image='aaa';直接给赋值了
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
Con.Open();
CmdObj.Parameters.Add("@Image", SqlDbType.Image);
CmdObj.Parameters.Add("@ImageSize", SqlDbType.Int);
CmdObj.Parameters["@Image"].Value = aaa;//这里为你的数组,即图片
CmdObj.Parameters["@ImageSize"].Value = UpFile.ContentLength; //图片的大小,upFile为上传控件
CmdObj.ExecuteNonQuery();//读出:
int ImgID = Convert.ToInt32(Request.QueryString["id"]);
string stringcon = ConfigurationManager.ConnectionStrings["Connectionstring"].ConnectionString;
SqlConnection Con = new SqlConnection(stringcon );
String SqlCmd = "select * from indexsql where id="+ImgID ;
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
Con.Open();
SqlDataReader SqlReader = CmdObj.ExecuteReader();
SqlReader.Read();
Response.OutputStream.Write ((byte[])SqlReader["Image"], 0, (int)SqlReader["ImageSize"]);//读出并显示 读取的数组 byte[] bbb= (byte[])SqlReader["Image"];
Response.End();
Con.Close(); }
这样应该就可以了,楼主给分吧!