将数据从excel复制到SQL数据库VB.NET

时间:2017-07-20 11:10:06

标签: sql excel vb.net datagridview

我想在任何给定时间使用excel文件导入数据。

我已创建数据库,我需要将数据导入其中。

我能够将数据导入DataGridView,但现在我无法导入数据库。

我现在从DGV导入数据库的代码是

    Dim command As New Data.SqlClient.SqlCommand


    command.CommandText = "InsertDataIntoDevicesLDL"

    command.Parameters.Add("@SN", SqlDbType.VarChar)
    command.Parameters.Add("@IdClass", SqlDbType.VarChar)
    command.Parameters.Add("@IdManu", SqlDbType.VarChar)
    command.Parameters.Add("@IdModel", SqlDbType.VarChar)
    command.Parameters.Add("@PartNum", SqlDbType.VarChar)
    command.Parameters.Add("@IdClt", SqlDbType.VarChar)
    command.Parameters.Add("@IdWh", SqlDbType.VarChar)
    command.Parameters.Add("@Slot", SqlDbType.VarChar)
    command.Parameters.Add("@IdLoc", SqlDbType.VarChar)
    command.Parameters.Add("@IdSts", SqlDbType.VarChar)

    MyConnection.Open()
    command.Connection = MyConnection


    For i As Integer = 0 To DataGridView1.Rows.Count - 1
        command.Parameters(0).Value = DataGridView1.Rows(i).Cells(0).Value
        command.Parameters(1).Value = DataGridView1.Rows(i).Cells(1).Value
        command.Parameters(2).Value = DataGridView1.Rows(i).Cells(2).Value
        command.Parameters(3).Value = DataGridView1.Rows(i).Cells(3).Value
        command.Parameters(4).Value = DataGridView1.Rows(i).Cells(4).Value
        command.Parameters(5).Value = DataGridView1.Rows(i).Cells(5).Value
        command.Parameters(6).Value = DataGridView1.Rows(i).Cells(6).Value
        command.Parameters(7).Value = DataGridView1.Rows(i).Cells(7).Value
        command.Parameters(8).Value = DataGridView1.Rows(i).Cells(8).Value
        command.Parameters(9).Value = DataGridView1.Rows(i).Cells(9).Value

        command.ExecuteNonQuery()
    Next

End Sub

我将程序InsertDataIntoDevicesLDL作为:

CREATE PROCEDURE [dbo].[InsertDataIntoDevicesLDL]

@SN         NCHAR (10), 
@IdClass    INT,        
@IdManu     INT,       
@IdModel    INT,    
@PartNum    NCHAR (10),  
@OrdNum     NCHAR (10), 
@ATCode     NCHAR (10),  
@Ticket     NCHAR (10),
@CreDate    DATETIME , 
@ExpeDate   DATETIME,   
@OBS        NCHAR (200), 
@IdClt      INT,        
@IdWh       INT,       
@Slot       NCHAR (10),
@IdLoc      INT,     
@IdSts      INT,
@IdLdl      INT      

AS

BEGIN

Insert into Equipamento (SerialNumber, ID_CLASS, ID_Manufacturer, ID_Model,ProductNumber, OrderNumber, ATCode, Ticket, CreationDate, ExpeditionDate, Observations, ID_CLT,ID_WH, Slot, ID_loc, ID_STS, ID_LDL)
    Values (@SN, @IdClass, @IdManu, @IdModel, @PartNum, @OrdNum, @ATCode, @Ticket, @CreDate, @ExpeDate, @OBS, @IdClt, @IdWh, @Slot, @IdLoc, @IdSts, @IdLdl )

END

所以我的问题是。

如何导入它。

现在我收到了错误:

System.Data.SqlClient.SqlException: 'Procedure or function 'InsertDataIntoDevicesLDL' expects parameter '@SN', which was not supplied.'

2 个答案:

答案 0 :(得分:0)

看起来参数没有传递值。

尝试类似:

Using connection As New SqlConnection(connection)

 Dim command As New SqlCommand("[dbo].[InsertDataIntoDevicesLDL]", connection)
 command.CommandType = CommandType.StoredProcedure

 For i As Integer = 0 To DataGridView1.Rows.Count - 1
 command.Parameters.AddWithvalue("@sn",Cstr(DataGridView1.Rows(i).Cells(0).Value))
 command .ExecuteNonQuery()
 Next

End Using

答案 1 :(得分:0)

找到它。

DGV有可用的添加选项,所以我在excel文件上有181条记录,但DGV上有182条记录,最后一行是完全空的,它导致了这个错误。