簡體   English   中英

使用c#將數據庫訪問中的長二進制數據轉換為圖像

[英]convert long binary data in database access to image using c#

這是我的代碼:

try
{
    connection.Open();
    OleDbCommand command = new OleDbCommand();
    command.Connection = connection;
    string query = "select * from StudentInformation where [StudentID] = " + txtStudentID.Text + "";
    command.CommandText = query;
    OleDbDataReader read = command.ExecuteReader();

    while (read.Read())
    {
       txtStudentID.Text = (read["StudentID"].ToString());
       txtFirstname.Text = (read["Firstname"].ToString());
       txtLastname.Text = (read["Lastname"].ToString());

        byte[] imgbyte = (byte[])read["Image"];    //when i add this a got error with this code
        MemoryStream ms = new MemoryStream(imgbyte);
        StudentPicture.Image = Image.FromStream(ms);
    }

    connection.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

我收到錯誤“參數無效”有人可以幫助我嗎? 我很混亂! 我嘗試了所有搜索但仍然錯誤的代碼:'(抱歉我的英文不好你的回復將不勝感激....

嘗試獲取這樣的字節數組:

var binLength = read.Item[3].Length;
byte[] imgByte = new byte[binLength - 1];
var bytes = read.GetBytes(0,0,imgByte,0,imgByte.Length);
MemoryStream ms = new MemoryStream(imgByte);
StudentPicture.Image = Image.FromStream(ms);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM