2013-05-20 62 views
0
private void Profile_Load(object sender, EventArgs e) 
{ 
    OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\Users\jay.desai\Documents\Visual Studio 2008\Projects\Employee Profile\Employee.mdb"); 
    OleDbCommand cmd = new OleDbCommand("select * from Profile where Emp_No=" + txtEmployeeNo.Text + "", con); 
    cmd.CommandType = CommandType.Text; 
    OleDbDataAdapter da = new OleDbDataAdapter(cmd); 
    DataSet ds = new DataSet(); 
    da.Fill(ds, "Profile"); 
    txtEmployeeNo.Text = ds.Tables[0].Rows[0][0].ToString(); 
    txtName.Text = ds.Tables[0].Rows[0][1].ToString(); 
    txtAddress.Text = ds.Tables[0].Rows[0][2].ToString(); 
    txtSex.Text = ds.Tables[0].Rows[0][3].ToString(); 
    txtMobNo.Text = ds.Tables[0].Rows[0][4].ToString(); 
    dtp.Text = ds.Tables[0].Rows[0][5].ToString(); 
    textBox1.Text = ds.Tables[0].Rows[0][6].ToString(); 
    pictureBox1.Image = ds.Tables[0].Rows[0][7]; 
} 

我已經創建了MS訪問一個數據庫,其中有一個表中調用檔案包含一個場照片具有OLE對象數據類型我在場上已經插入碼的手動.BMP一個形象現在訪問數據庫的照片我想要在picturebox中檢索該圖像,但在運行時我得到了這個錯誤「不能隱式地將類型'object'轉換爲'System.Drawing.Image'。存在明確的轉換(你是否缺少一個轉換?)」如何從

+0

看看這裏http://stackoverflow.com/a/3440372/125740。同樣的問題被問了很多次。你應該做一些研究。 – Yahya

+0

在你的SQL語句結尾處沒有任何需要你的+「」,除非Emp_No是一個varchar值,在這種情況下你需要更多的單引號。當然,將一個文本框的值放入SQL語句中會讓您對注入攻擊持開放態度 –

+0

我已經完成了該操作,但仍然無法正常工作 –

回答

0

你需要有Image類型的對象來設置Picture Box Image,所以你可以使用內存流和Image.FromStream方法來獲取圖片

byte[] bimg= (byte[])ds.Tables[0].Rows[0][7]; 
MemoryStream mstream = new MemoryStream(bimg); 
pictureBox1.Image = Image.FromStream(mstream); 
+0

感謝您的回覆,但仍然收到一個錯誤「mstream參數無效」。 –

+0

我得到了同樣的錯誤.. ?? –