我想在任何给定时间使用 excel 文件导入数据。
我已创建数据库,需要将数据导入其中。
我能够将数据导入到数据网格视图,但现在无法导入到数据库。
我现在要从 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
我有程序 将数据插入设备LDL 作为:
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.'
看起来参数没有传递值。
尝试类似操作:
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
找到了。
DGV 具有可用的添加选项,因此我在 excel 文件上有 181 条记录,但在 DGV 上有 182 行,最后一行完全为空,它导致了该错误。