system.byte []到datagridview [英] system.byte[] to datagridview

查看:53
本文介绍了system.byte []到datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Guys



我将一些DataGridViewImage列保存到sql后,将它们与一些datagridtextcolumn数据一起转换为二进制数据。我需要将它们写回我的datagridview控件。这是代码的一部分以及我的数据表的样子。当我运行此代码时,我得到错误无效参数任何想法?谢谢。



Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理Button1.Click

Dim cmdselect作为SqlCommand = New SqlCommand(SELECT * FROM tblImgData,_ Connect)

_Connection.Open()

尝试

使用dataAdapter作为新的SqlDataAdapter (cmdselect)

使用commandBuilder作为新的SqlCommandBuilder(dataAdapter)

Dim dataTable As New DataTable()

dataTable.Locale = System.Globalization。 CultureInfo.InvariantCulture

dataAdapter.Fill(dataTable)

对于x = 0 To(dataTable.DefaultView.Count - 1)

Dim barrImg As Byte()= DirectCast(dataTable.Rows(x).Item(1),Byte())

''''在这一行我得到了错误

dataTable .Rows(x).Item(1)= Image.FromStre am(New MemoryStream(barrImg))

下一页

Dim bindingSource1 As New BindingSource()

bindingSource1.DataSource = dataTable

DataGridView1.DataSource = bindingSource1

结束使用

结束使用

Catch ex As Exception

MessageBox。显示(错误:+ ex.Message,连接错误,MessageBoxButtons.OK,MessageBoxIcon。[错误])

'完成程序

应用程序。[退出]()

结束尝试

结束子



数据表:



name ---- image ------ message

彼得--- system.byte [] ----你好

dan ---- system.byte [] ---- hello

Hi Guys

I have saved some DataGridViewImage column to sql after converting them to Binary data along with some datagridtextcolumn data. I need to write them back to my datagridview control. here is part of the code and what my datatable look like. when I run this code I got error "invalid Parameter" any idea ? Thanks.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cmdselect As SqlCommand = New SqlCommand("SELECT * FROM tblImgData", _Connection)
_Connection.Open()
Try
Using dataAdapter As New SqlDataAdapter(cmdselect)
Using commandBuilder As New SqlCommandBuilder(dataAdapter)
Dim dataTable As New DataTable()
dataTable.Locale = System.Globalization.CultureInfo.InvariantCulture
dataAdapter.Fill(dataTable)
For x = 0 To (dataTable.DefaultView.Count - 1)
Dim barrImg As Byte() = DirectCast(dataTable.Rows(x).Item(1), Byte())
''''In this line I got the error
dataTable.Rows(x).Item(1) = Image.FromStream(New MemoryStream(barrImg))
Next
Dim bindingSource1 As New BindingSource()
bindingSource1.DataSource = dataTable
DataGridView1.DataSource = bindingSource1
End Using
End Using
Catch ex As Exception
MessageBox.Show("Error: " + ex.Message, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
' finish the program
Application.[Exit]()
End Try
End Sub

DataTable:

name---- image------ message
Peter--- system.byte[]---- hello
dan---- system.byte[] ---- hello

推荐答案

如果收到数据无效的错误,那么错误可能在存储图像的代码。为什么要在数据库中存储图像而不是文件系统?
If you get an error that your data is invalid, then the error is probably in the code storing the image. Why are you storing images in your database, instead of the file system ?


嗯,在我的应用程序中,我有一个带有4列的datagridview ctrl。 2个datagridviewtext列和2个datagridviewImage列。使用下面的代码以及存储过程,我能够将所有这些列存储到sql表中。存储在sql中的原因是因为用户将对此列进行更改,我需要能够保存以防万一我们需要重新启动PC。图像是嵌入的资源图标。

在sql表中,我看到所有的名称,消息和图像都在< binary data =>格式。

但是使用上面的代码我无法检索此表以在datagridview上显示它

Well, In my application I have a datagridview ctrl with 4 columns. 2 datagridviewtext columns and 2 datagridviewImage columns. with the code below along with the store procedure I was able to store all this columns into sql table. the reason behind storing in sql because the user will make changes to this columns I need to be able to save is just in case we need to reboot the PC. the images are embedded resource icons.
In sql table, I see all the names , messages and the images are in <binary data=""> format.
But with code above I can't retrieve this table to show it back on datagridview
For X As Integer = 0 To DataGridView1.RowCount - 1
Dim strFn As String = DataGridView1.Rows(X).Cells"CompanyStatusImage".Value.ToString
               Dim fiImage As New FileInfo(strFn)
               Me.m_lImageFileLength = fiImage.Length
               m_barrImg = New Byte(Convert.ToInt32(Me.m_lImageFileLength) - 1) {}







Private Sub frmMainForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim _DelimitedString As String = String.Empty
        For X As Integer = 0 To DataGridView1.RowCount - 1

            _DelimitedString &= DataGridView1.Rows(X).Cells("Coworker_name").Value.ToString() & "," _
            & DataGridView1.Rows(X).Cells("StatusImage").Value.ToString() & "," _
            & DataGridView1.Rows(X).Cells("StatusImage1").Value.ToString() & "," _
            & DataGridView1.Rows(X).Cells("Message").Value.ToString() & "," _




Next

       _Connection.Open()
       _Command = New System.Data.SqlClient.SqlCommand()
       _Command.CommandType = CommandType.StoredProcedure
       _Command.CommandText = "sp_InputWork"
       _Command.Connection = _Connection
       _Command.Parameters.AddWithValue("@_DelimitedString", System.Data.SqlDbType.NVarChar).Value = _DelimitedString
       _Command.Dispose()
       _Command.ExecuteNonQuery()

   End Sub





商店程序





Store Procedure

USE [master]
GO
/****** Object:  StoredProcedure [dbo].[sp_InputWork]    Script Date: 06/17/2013 12:51:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON

go

ALTER PROCEDURE [dbo].[sp_SQLQuery3]
    @_DelimitedString nvarchar(MAX)
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @_DataRow nvarchar(MAX)
    DECLARE @_coworker_name nvarchar(MAX)
        DECLARE @_TemporaryStorage nvarchar(MAX)
    DECLARE @_m_barrImg varchar(MAX)
DECLARE @_m_barrImg1 varchar(MAX)
DECLARE @_message nvarchar(MAX)
    DECLARE @_CurrentField int

    WHILE CHARINDEX(';', @_DelimitedString) > 0
    BEGIN
        SET @_DataRow = CAST(SUBSTRING(@_DelimitedString, 0, CHARINDEX(';', @_DelimitedString)) AS nvarchar(MAX))
        SET @_CurrentField = 1
        WHILE CHARINDEX(',', @_DataRow) > 0
            BEGIN
            SET @_TemporaryStorage = CAST(SUBSTRING(@_DataRow, 0, CHARINDEX(',', @_DataRow)) AS nvarchar(MAX))
IF @_CurrentField = 2
                SET @_m_barrImg = @_TemporaryStorage
IF @_CurrentField = 3
                SET @_m_barrImg1 = @_TemporaryStorage
 IF @_CurrentField = 1
            SET @_coworker_name = @_TemporaryStorage
 IF @_CurrentField = 4
                    SET @_company_name = @_TemporaryStorage

            SET @_DataRow = SUBSTRING(@_DataRow, CHARINDEX(',', @_DataRow) + 1, LEN(@_DataRow))
            SET @_CurrentField = @_CurrentField + 1
        END
       INSERT INTO tblImgData (coworker_name,CompanyStatusImage,CompanyStatusImage1,message) VALUES (@_coworker_name,@_m_barrImg,@_m_barrImg1,@_message)
	    SET @_DelimitedString = SUBSTRING(@_DelimitedString, CHARINDEX(';', @_DelimitedString) + 1, LEN(@_DelimitedString))
        END
END

sql table has 4 columns
nvarchar(max), image,image, nvarchar(max)


如何将带有图像的数据网格视图保存到系统?

谢谢

dean
How would you save a datagridview with images to system ?
Thanks
dean


这篇关于system.byte []到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆