richTextBox如何读取数据库中的image字段的问题?保存我已经写好了,winform中,有些不是很熟悉,谢谢;
我这样写总是不行,
byte[] bWrite = model.PlanExplain; //返回的byte[]
System.IO.MemoryStream mstream = new System.IO.MemoryStream(bWrite, false);
richTextBox.LoadFile(mstream, RichTextBoxStreamType.RichText);
我这样写总是不行,
byte[] bWrite = model.PlanExplain; //返回的byte[]
System.IO.MemoryStream mstream = new System.IO.MemoryStream(bWrite, false);
richTextBox.LoadFile(mstream, RichTextBoxStreamType.RichText);
richTextBox.LoadFile(ms, RichTextBoxStreamType.PlainText);orrichTextBox.LoadFile(ms, RichTextBoxStreamType.UnicodePlainText);
就这么几个重载方法哦 是不是本身读取得数据有问题?
richTextBox1.SaveFile("temp.rtf");
stream = new FileStream("temp.rtf", FileMode.Open, FileAccess.Read);
int size = Convert.ToInt32(stream.Length);
Byte[] rtf = new Byte[size];
stream.Read(rtf, 0, size);
==========读取图片========SqlConnection conn = new SqlConnection("Server=.;Database=Northwind;Integrated Security=SSPI");
string strSql = "select a from c";
SqlCommand cmd = new SqlCommand(strSql, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
byte[] bytes = (byte[])reader["a"];
MemoryStream ms = new MemoryStream(bytes);
Image image = Image.FromStream(ms); pictureBox1.Image = image; reader.Close();
conn.Close();
===============================写入图片============= OpenFileDialog openfileDialog = new OpenFileDialog();
openfileDialog.Filter = "Picture Files(*.jpg)|*.jpg|Bmp(*.bmp)|*.bmp|All Files(*.*)|*.*";
FileStream fileStream;
openfileDialog.ShowDialog();
string filepath = openfileDialog.FileName; //获得图象并把图象转换为byte[]
fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
byte[] photoArray = new byte[(int)fileStream.Length];
fileStream.Read(photoArray, 0, photoArray.Length);
fileStream.Close();
try
{
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
pictureBox1.Image = System.Drawing.Image.FromFile(filepath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
SqlConnection conn = new SqlConnection("Server=.;Database=Northwind;Integrated Security=SSPI"); //插入语句
SqlCommand cmd = new SqlCommand("INSERT INTO c(a)VALUES (@a)", conn); cmd.Parameters.Add("@a",SqlDbType.Image);
cmd.Parameters["@a"].Value=photoArray;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
=========================================
reader = cmd.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
if (!reader.IsDBNull(0))
{
Byte[] rtf = new Byte[Convert.ToInt32((reader.GetBytes(0, 0, null, 0, Int32.MaxValue)))];
long bytesReceived = reader.GetBytes(0, 0, rtf, 0, rtf.Length); ASCIIEncoding encoding = new ASCIIEncoding();
richTextBox1.Rtf = encoding.GetString(rtf, 0, Convert.ToInt32(bytesReceived));
}
}
只不过我用的是dataset,不是dataread,不知道怎么改一下?
======================== 有区别 吗 ? 如果别人面试的时候 问他们之间的区别 你又是怎么去 回答 :
===
SqlConnection conn = new SqlConnection("Server=.;Database=Northwind;Integrated Security=SSPI");
string strSql = "select a from c";
SqlCommand cmd = new SqlCommand(strSql, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
byte[] bytes = (byte[])reader["a"];
MemoryStream ms = new MemoryStream(bytes);
Image image = Image.FromStream(ms); pictureBox1.Image = image; reader.Close();
conn.Close();
===========
SqlConnection conn = new SqlConnection("Server=.;Database=Northwind;Integrated Security=SSPI");
string strSql = "select a from c"; //SQL 语句自己改动,
SqlDataAdapter da=New SqlDataAdapter(strSql, conn); DataSet ds=New DataSet();
da.Fill(ds);
byte[] bytes = (byte[])ds.Tables[0].rows[0]["列名"];
MemoryStream ms = new MemoryStream(bytes);
Image image = Image.FromStream(ms); pictureBox1.Image = image; reader.Close();
conn.Close();
看关键代码:
Byte[] rtf = new Byte[Convert.ToInt32((reader.GetBytes(0, 0, null, 0, Int32.MaxValue)))];
long bytesReceived = reader.GetBytes(0, 0, rtf, 0, rtf.Length); ASCIIEncoding encoding = new ASCIIEncoding();
richTextBox1.Rtf = encoding.GetString(rtf, 0, Convert.ToInt32(bytesReceived));如果是dataset,怎么改一下,不能用pictureBox显示