数据库中存的 是System.Byte[]类型的啊。该怎么处理呢?
15 个解决方案
#1
直接写myds.Tables[0].Rows[0][1].ToString(),如果需要用byte[]的话再转换一下。
#2
Encoding encoding;
encoding = System.Text.Encoding.GetEncoding( "GB2312 ");
// string > bytes
byte[] bytes = encoding.GetBytes("");
encoding = System.Text.Encoding.GetEncoding( "GB2312 ");
// string > bytes
byte[] bytes = encoding.GetBytes("");
#3
string s;
byte[] bytes= Encoding.UTF8.GetBytes(s);
用encoding类的getbytes方法转换
byte[] bytes= Encoding.UTF8.GetBytes(s);
用encoding类的getbytes方法转换
#4
不行啊这样。
#5
<img src="show.aspx?id=x" />
show.aspx
private void Page_Load(object sender, System.EventArgs e)
{
string ID = Request.QueryString["id"];
string sqlstr = "select photo from Tb where id=@id";
SqlConnection myConnection = new SqlConnection(connstr);
SqlCommand myCommand = new SqlCommand(sqlstr, myConnection);
myCommand.AddWithValue("@id",ID )
try
{
myConnection.Open();
SqlDataReader dr= myCommand.ExecuteReader(CommandBehavior.CloseConnection);
if(dr.Read())
{
Response.Clear();
Response.ContentType = "image/gif";
Response.BinaryWrite((byte[])dr["photo"]);
}
myConnection.Close();
}
catch (SqlException ex)
{
}
Response.End();
}
show.aspx
private void Page_Load(object sender, System.EventArgs e)
{
string ID = Request.QueryString["id"];
string sqlstr = "select photo from Tb where id=@id";
SqlConnection myConnection = new SqlConnection(connstr);
SqlCommand myCommand = new SqlCommand(sqlstr, myConnection);
myCommand.AddWithValue("@id",ID )
try
{
myConnection.Open();
SqlDataReader dr= myCommand.ExecuteReader(CommandBehavior.CloseConnection);
if(dr.Read())
{
Response.Clear();
Response.ContentType = "image/gif";
Response.BinaryWrite((byte[])dr["photo"]);
}
myConnection.Close();
}
catch (SqlException ex)
{
}
Response.End();
}
#6
用convert转一下拜
#7
System.Text.UTF8Encoding.UTF8.GetBytes("yourstring");
#8
#9
using system text;
byte[] byteData = Encoding.UTF8.GetBytes(myds.Tables[0].Rows[0][1]);
byte[] byteData = Encoding.UTF8.GetBytes(myds.Tables[0].Rows[0][1]);
#10
晕 它现在说参数无效。。。
Encoding encoding;
encoding = System.Text.Encoding.GetEncoding("GB2312");
// string > bytes
byte[] bytes = encoding.GetBytes(myds.Tables[0].Rows[0][1].ToString());
MemoryStream MStream = new MemoryStream(bytes);
pictureBox1.Image = Image.FromStream(MStream);
#11
pictureBox1.Image = Image.FromStream(MStream);
这句。
这句。
#12
直接用 ASCII 编码试试
System.Text.Encoding.ASCII.GetBytes
System.Text.Encoding.ASCII.GetBytes
#13
不过楼主你说“数据库中存的 是System.Byte[]类型”是什么意思?一般数据库没这种类型吧?
#14
你出错误的原因应该是 myds.Tables[0].Rows[0][1] 返回一个string类型的对象吧,你要把一个string类型的对象强制转换成byte[]所以不对了。
正确的做法应该是
byte[] byteData = Encoding.UTF8.GetBytes(myds.Tables[0].Rows[0][1]);
MemoryStream MStream = new MemoryStream(byteData);
这样吧?
正确的做法应该是
byte[] byteData = Encoding.UTF8.GetBytes(myds.Tables[0].Rows[0][1]);
MemoryStream MStream = new MemoryStream(byteData);
这样吧?
#15
问题解决了吗?
#1
直接写myds.Tables[0].Rows[0][1].ToString(),如果需要用byte[]的话再转换一下。
#2
Encoding encoding;
encoding = System.Text.Encoding.GetEncoding( "GB2312 ");
// string > bytes
byte[] bytes = encoding.GetBytes("");
encoding = System.Text.Encoding.GetEncoding( "GB2312 ");
// string > bytes
byte[] bytes = encoding.GetBytes("");
#3
string s;
byte[] bytes= Encoding.UTF8.GetBytes(s);
用encoding类的getbytes方法转换
byte[] bytes= Encoding.UTF8.GetBytes(s);
用encoding类的getbytes方法转换
#4
不行啊这样。
#5
<img src="show.aspx?id=x" />
show.aspx
private void Page_Load(object sender, System.EventArgs e)
{
string ID = Request.QueryString["id"];
string sqlstr = "select photo from Tb where id=@id";
SqlConnection myConnection = new SqlConnection(connstr);
SqlCommand myCommand = new SqlCommand(sqlstr, myConnection);
myCommand.AddWithValue("@id",ID )
try
{
myConnection.Open();
SqlDataReader dr= myCommand.ExecuteReader(CommandBehavior.CloseConnection);
if(dr.Read())
{
Response.Clear();
Response.ContentType = "image/gif";
Response.BinaryWrite((byte[])dr["photo"]);
}
myConnection.Close();
}
catch (SqlException ex)
{
}
Response.End();
}
show.aspx
private void Page_Load(object sender, System.EventArgs e)
{
string ID = Request.QueryString["id"];
string sqlstr = "select photo from Tb where id=@id";
SqlConnection myConnection = new SqlConnection(connstr);
SqlCommand myCommand = new SqlCommand(sqlstr, myConnection);
myCommand.AddWithValue("@id",ID )
try
{
myConnection.Open();
SqlDataReader dr= myCommand.ExecuteReader(CommandBehavior.CloseConnection);
if(dr.Read())
{
Response.Clear();
Response.ContentType = "image/gif";
Response.BinaryWrite((byte[])dr["photo"]);
}
myConnection.Close();
}
catch (SqlException ex)
{
}
Response.End();
}
#6
用convert转一下拜
#7
System.Text.UTF8Encoding.UTF8.GetBytes("yourstring");
#8
#9
using system text;
byte[] byteData = Encoding.UTF8.GetBytes(myds.Tables[0].Rows[0][1]);
byte[] byteData = Encoding.UTF8.GetBytes(myds.Tables[0].Rows[0][1]);
#10
晕 它现在说参数无效。。。
Encoding encoding;
encoding = System.Text.Encoding.GetEncoding("GB2312");
// string > bytes
byte[] bytes = encoding.GetBytes(myds.Tables[0].Rows[0][1].ToString());
MemoryStream MStream = new MemoryStream(bytes);
pictureBox1.Image = Image.FromStream(MStream);
#11
pictureBox1.Image = Image.FromStream(MStream);
这句。
这句。
#12
直接用 ASCII 编码试试
System.Text.Encoding.ASCII.GetBytes
System.Text.Encoding.ASCII.GetBytes
#13
不过楼主你说“数据库中存的 是System.Byte[]类型”是什么意思?一般数据库没这种类型吧?
#14
你出错误的原因应该是 myds.Tables[0].Rows[0][1] 返回一个string类型的对象吧,你要把一个string类型的对象强制转换成byte[]所以不对了。
正确的做法应该是
byte[] byteData = Encoding.UTF8.GetBytes(myds.Tables[0].Rows[0][1]);
MemoryStream MStream = new MemoryStream(byteData);
这样吧?
正确的做法应该是
byte[] byteData = Encoding.UTF8.GetBytes(myds.Tables[0].Rows[0][1]);
MemoryStream MStream = new MemoryStream(byteData);
这样吧?
#15
问题解决了吗?