简体   繁体   English

如何将图像与C#上的文本框和组合框一起从SQL Server绑定到图片框?

[英]How to bind an image into picture box from SQL Server along with a textbox and combo box on C#?

So I have a table called Bicycle inside BikeStore database on sql server, consists of the following columns: BID, BName, Description, BType, Brand, Origin, Price, Stock, BImage 因此,我在SQL Server上的BikeStore数据库中有一个名为Bicycle的表,该表包含以下几列:BID,BName,描述,BType,品牌,产地,价格,库存,BImage

Successfully bind all textboxes and one combo box with my data from sql server just by clicking cells on datagridview. 只需单击datagridview上的单元格,即可将所有文本框和一个组合框与sql server中的数据成功绑定。 Below is the code: 下面是代码:

private void dg1_CellClick(object sender, DataGridViewCellEventArgs e)
{
        bidTxt.Text = dg1.SelectedRows[0].Cells[0].Value.ToString();
        bnameTxt.Text = dg1.SelectedRows[0].Cells[1].Value.ToString();
        bdescrichTxt.Text = dg1.SelectedRows[0].Cells[2].Value.ToString();
        typeCB.Text = dg1.SelectedRows[0].Cells[3].Value.ToString();
        brandTxt.Text = dg1.SelectedRows[0].Cells[4].Value.ToString();
        originTxt.Text = dg1.SelectedRows[0].Cells[5].Value.ToString();
        priceTxt.Text = dg1.SelectedRows[0].Cells[6].Value.ToString();
        stockTxt.Text = dg1.SelectedRows[0].Cells[7].Value.ToString();

But it still generates an error everytime I tried to load the image too. 但是每次我也尝试加载图像时,它仍然会产生错误。 Here's the code: 这是代码:

        sqlconn.Open();

        SqlCommand cmd = new SqlCommand("SELECT BImage FROM Bicycle WHERE BID = " + dg1.SelectedRows[0].Cells[0].Value + "", sqlconn);
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();
        byte[] mydata = new byte[0];
        da.Fill(ds, "Bicycle");
        DataRow myrow;
        myrow = ds.Tables["Bicycle"].Rows[8];
        mydata = (byte[])myrow["BImage"];
        MemoryStream stream = new MemoryStream(mydata);
        BikePic.Image = Image.FromStream(stream);

        sqlconn.Close();
}

What did I do wrong? 我做错了什么?

I'm new at this. 我是新来的。

If it just about only retrieving the BLOB on that query then you could do something more simple : 如果仅在该查询上检索BLOB,则可以执行一些更简单的操作:

// SqlConnection creation and opening omitted
using(SqlCommand cmd = new SqlCommand("SELECT BImage FROM Bicycle WHERE BID = " + dg1.SelectedRows[0].Cells[0].Value + "", sqlconn))
{
  byte[] mydata = (byte[])cmd.ExecuteScalar();
  MemoryStream stream = new MemoryStream(mydata);
  BikePic.Image = Image.FromStream(stream);
}

Also you might want to dispose of stream after creating the Image control but I'm not completely sure. 另外,您可能要在创建Image控件后处置stream ,但是我不确定。

Problem solved! 问题解决了! Add single quotation mark on the SqlCommand line between the blockquote as it'll search up the ID you looking for, like this: blockquote之间的SqlCommand行上添加单引号 ,因为它将搜索您要查找的ID,如下所示:

SqlCommand cmd = new SqlCommand("SELECT BImage FROM Bicycle WHERE BID = '" + dg1.SelectedRows[0].Cells[0].Value + "'", sqlconn);

Below is the code before modified . 下面是修改前的代码。 See the difference? 看到不同?

SqlCommand cmd = new SqlCommand("SELECT BImage FROM Bicycle WHERE BID = " + dg1.SelectedRows[0].Cells[0].Value + "", sqlconn);

Haha silly me. 哈哈愚蠢的我。 Thanks anyway guys! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM