日期:2014-05-17  浏览次数:20739 次

关于richtextbox如何循环读出数据库中的图片
不知道刚才那位大哥还在不在,代码如下,我该如何去改?
C# code

        //发送表情
        private void btnOK_Click(object sender, EventArgs e)
        {
            //建立内存流 
            MemoryStream ms = new MemoryStream();
            ms.Position = 0;
            //把当前的richtextbox内容包括图片和文本保存到流中 
            rtbOut.SaveFile(ms, RichTextBoxStreamType.RichText);
            string sql = "insert rrr(Chat) values (@biStr)";
            SqlCommand comm = new SqlCommand(sql, DBHelper.conn);
            comm.Parameters.AddWithValue("@biStr", ms.GetBuffer());
            DBHelper.conn.Open();
            int num = comm.ExecuteNonQuery();
            DBHelper.conn.Close();
            if (num == 1)
            {
                trmIn.Start();
            } 
}

        //接受数据库的信息
        private void trmIn_Tick(object sender, EventArgs e)
        {
            rtbOut.Clear();
            string sql = "select Chat from rrr where Id <> 0";
            SqlCommand comm = new SqlCommand(sql, DBHelper.conn);
            DBHelper.conn.Open();
            //下面是读取 
            byte[] b = new byte[] { };
            b = (byte[])(comm.ExecuteScalar());
            MemoryStream ms = new MemoryStream();
            ms.Position = 0;
            ms.Write(b, 0, b.Length);
            ms.Position = 0;
            rtbIn.LoadFile(ms, RichTextBoxStreamType.RichText);
            ms.Close();

            DBHelper.conn.Close();
            trmIn.Stop();
}

顺便能帮我把每句代码写下注释,明天起来立马结贴



------解决方案--------------------
那么,这样好了:
C# code

       //接受数据库的信息
        private void trmIn_Tick(object sender, EventArgs e)
        {
            rtbOut.Clear();
            string sql = "select Chat from rrr where Id <> 0";
            SqlCommand comm = new SqlCommand(sql, DBHelper.conn);
            DBHelper.conn.Open();
            //下面是读取 
            byte[] b = null;
            SqlDataReader reader=comm.ExecuteReader();
            MemoryStream ms = new MemoryStream();
            ms.Position = 0;//这一句实在是多余的,流的起始位置本来就在0。
            while(reader.Read())
            {
            b = (byte[])(reader[0]);
            ms.Write(b, ms.Position, b.Length);
            ms.Seek(Position,b.Length);//加这一句看看
            ms.Position += b.Length;
            }
            ms.Position=0;//这一句也不知道要不要,可以试试去掉看看
            rtbIn.LoadFile(ms, RichTextBoxStreamType.RichText);
            ms.Close();

            DBHelper.conn.Close();
            trmIn.Stop();
}

------解决方案--------------------
//接受数据库的信息
private void trmIn_Tick(object sender, EventArgs e)
{
rtbOut.Clear();
string sql = "select Chat from rrr where Id <> 0";
SqlCommand comm = new SqlCommand(sql, DBHelper.conn);
DBHelper.conn.Open();
//下面是读取 
byte[] b = null;
SqlDataReader reader=comm.ExecuteReader();
MemoryStream ms = new MemoryStream();
ms.Position = 0;//这一句实在是多余的,流的起始位置本来就在0。
while(reader.Read())
{
b = (byte[])(reader[0]);
ms.Write(b, 0, b.Length);
ms.Flush()
}
rtbIn.LoadFile(ms, RichTextBoxStreamType.RichText);
ms.Close();

DBHelper.conn.Close();
trmIn.Stop();
}