将datagrid图像转换为数据二进制

时间:2013-06-17 19:48:15

标签: vb.net

我需要使用内存流将datagridview image列转换为数据二进制文件并将它们存储在sql表中。我的datagrid包含30行。这就是我已经完成的工作。但是这只将图像存储为nvchar(max)

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


    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("CompanyStatusImage").Value.ToString() & "," _
        & DataGridView1.Rows(X).Cells("CompanyStatusImage1").Value.ToString() & "," _
        & DataGridView1.Rows(X).Cells("company_name").Value.ToString() & ",;"
Next
    _Connection = New System.Data.SqlClient.SqlConnection("Data Source=server\sqlexpress;Initial Catalog=master;User ID=test;Password=test")
    _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

存储过程

    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_InputWork] 
    @_DelimitedString nvarchar(MAX) 
AS
BEGIN
    SET NOCOUNT ON;

DECLARE @_DataRow nvarchar(MAX)
DECLARE @_coworker_name nvarchar(MAX)
DECLARE @_TemporaryStorage nvarchar(MAX)
DECLARE @_CompanyStatusImage nvarchar(MAX)
DECLARE @_company_name nvarchar(MAX)
DECLARE @_CurrentField int
truncate table master.dbo.tblImgData
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 @_CompanyStatusImage = @_TemporaryStorage
                IF @_CurrentField = 1
        SET @_coworker_name = @_TemporaryStorage
                IF @_CurrentField = 3
                SET @_company_name = @_TemporaryStorage         
        SET @_DataRow = SUBSTRING(@_DataRow, CHARINDEX(',', @_DataRow) + 1, LEN(@_DataRow))
        SET @_CurrentField = @_CurrentField + 1
    END
    INSERT INTO tblImgData (coworker_name,CompanyStatusImage,company_name) VALUES (@_coworker_name,@_CompanyStatusImage, @_company_name)
    SET @_DelimitedString = SUBSTRING(@_DelimitedString, CHARINDEX(';', @_DelimitedString) + 1, LEN(@_DelimitedString))
    END

END

感谢..............

0 个答案:

没有答案