对于text、ntext 和 image 等数据类型,建议用流的方式存取。下面是图片的存取代码:
以MS自带的数据库Northwnd为例,其中有个表是Categories,有四个四段,其中有一个是Image类型的Picture字段.我们首先添加一张bmp图片到最后一行的Picture中,然后在读出来显示到Image控件中.添加一个SqlDataAdapter1,用向导设置联接数据库为Northwnd,SQL语句为SELECT [Category ID], [Category Name], Description, Picture FROM Categories.生成一个数据集为dataset1. 然后添加两个按钮分别表示写图片到数据库和读数据库,还有一个Image控件用于显示图片.添加以下代码Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SqlDataAdapter1.Fill(DataSet11)
End Sub'从数据库读取图片暂时存储为monkey.bmp,然后加载到image控件里面.    
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadPicFromDb.Click
        Try
            Dim data As Byte() = DataSet11.Tables(0).Rows(7).Item(3)
            Dim myfilestream As New System.IO.FileStream(Application.StartupPath & "\monkey.bmp", IO.FileMode.Create)
            myfilestream.Write(data, 0, data.Length)
            myfilestream.Close()
            PictureBox1.Image = New Bitmap(Application.StartupPath & "\monkey.bmp")
        Catch
        End Try
End Sub
    '把C:\6.bmp写入库中,你可以改为自己的图片.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertPicToDb.Click
        Dim myfilestream As New System.IO.FileStream("c:\6.bmp", IO.FileMode.Open)
        Dim data() As Byte
        ReDim data(myfilestream.Length - 1)
        myfilestream.Read(data, 0, myfilestream.Length)
        myfilestream.Close()
        DataSet11.Tables(0).Rows(7).Item(3) = data
        SqlDataAdapter1.Update(DataSet11.GetChanges())
End Sub

解决方案 »

  1.   

    问题在查询:她想要的是用dataGrid控件的那种效果。符合条件的记录都能显示出来,可是dataGrid也能显示图片么?
      

  2.   

    将图片编号,在指定目录下查找就可以了
    datalist能显示图片
      

  3.   

    datagrid可以显示存在数据库里的图片。可以在datagrid中绑定一个image,imageurl为一个显示图片的网页。
      

  4.   

    http://aspalliance.com/das/datagridimages.aspx
      

  5.   

    DG里用模板列,在模板列里插入IMG,IMG的SRC=显示从数据库中读出IMAGE的ASPX文件
      

  6.   

    你作过ASP 吗?
    你可以参考一下!做一个表格,他和数据库中的记录绑定,这样的效果和DATAGRID差不多!
    骗一骗了,善意的啊
      

  7.   

    : LongBow007(明朝散发弄扁舟) 呵呵,这位仁兄说的好啊
    就告诉她难度太大,起码需要半年时间研究“算法”哈哈,然后你三个月给她
    她一定以为你好伟大的
    三个月时间,自己慢慢研究,一定没有问题,嘻嘻,够黑啊
      

  8.   

    给mm看的?
    不用下功夫研究数据库了,可能等不及。
    同意图片放一个目录编号
    动态生成Image控件显示
    算法自己写
    快点啊,呵呵,别让mm跑了
      

  9.   

    我找到解决方法了,只是不知道怎么用下面的代码。我试了几天了就是不行。没有逻辑错误。应该是怎么在一个类中用下面的protected override paint()方法。兄弟姐妹们,帮忙呀!
    5.10 How do I color a individual cell depending upon its value or some external method?     
    We give three different methods for doing this.
    The first one overrides Paint in a derived columnstyle and sets the backcolor there. 
    The second uses a delegate to set the color in the Paint override. 
    The third method adds an event to the derived column style to allow you to set the color in an event handler.
    Method 1
    You can do this by deriving from DataGridTextBoxColumn and overriding the Paint method to conditionally set the backColor and foreColor. The sample code below colors any cell that starts 
    with a letter higher than 'F'. You can download a project (C#, VB) using this class. 
     
    [C#] 
     
         public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn 
     
         { 
     
              protected override void Paint(System.Drawing.Graphics g,System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight) 
     { // the idea is to conditionally set the foreBrush and/or backbrush 
     // depending upon some crireria on the cell value 
     // Here, we color anything that begins with a letter higher than 'F' 
     
                   try{ 
     
                        object o = this.GetColumnValueAtRow(source, rowNum); 
     
                        if( o!= null) 
     
                        { 
     
                             char c = ((string)o)[0]; 
     
                             if( c > 'F') 
          { 
     
                             // could be as simple as 
     
                             // backBrush = new SolidBrush(Color.Pink); 
     
                             // or something fancier... 
      backBrush = new LinearGradientBrush(bounds, 
     
                                       Color.FromArgb(255, 200, 200), 
     
                                       Color.FromArgb(128, 20, 20), 
     
                                       LinearGradientMode.BackwardDiagonal); 
     
                                  foreBrush = new SolidBrush(Color.White); 
     
                             } 
     
                        } 
     
                   } 
     
                    catch(Exception ex){ /* empty catch */ } 
     
                   finally{ 
        // make sure the base class gets called to do the drawing with 
     
                        // the possibly changed brushes 
     
                        base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight); 
     
                   } 
     
              } 
     
         }