我需要监视目录中的多个文件夹,并在每个文件夹中插入多个.csv文件的记录。我使用filesystemwatcher进行监控。现在我需要检查并将每个更新/新创建的.csv文件中的记录插入到SQL Server数据库中。我不能拥有唯一字段,因为可能存在可以复制的字段值。因此,我使用三个字段的组合来检查重复并仅在数据库中不存在时插入.csv文件记录。以下代码在行cmdInsertSQL.ExecuteNonQuery()上抛出一个错误:
参数化查询'(@ ColumnA int,@ ColumnB datetime,@ ColumnC time(7),@ ColumnD varcha'需要参数'@ColumnA',这是未提供的。
以下是代码:
Private Sub ReadAndInsertCSVData()
Dim table As New DataTable()
table.Columns.Add("ColumnA")
table.Columns.Add("ColumnB")
table.Columns.Add("ColumnC")
table.Columns.Add("ColumnD")
table.Columns.Add("ColumnE")
table.Columns.Add("ColumnF")
table.Columns.Add("ColumnG")
table.Columns.Add("ColumnH")
table.Columns.Add("ColumnI")
table.Columns.Add("ColumnJ")
table.Columns.Add("ColumnK")
table.Columns.Add("ColumnL")
table.Columns.Add("ColumnM")
table.Columns.Add("ColumnN")
table.Columns.Add("ColumnO")
table.Columns.Add("ColumnP")
table.Columns.Add("ColumnQ")
table.Columns.Add("ColumnR")
table.Columns.Add("ColumnS")
table.Columns.Add("ColumnT")
table.Columns.Add("ColumnU")
table.Columns.Add("ColumnV")
table.Columns.Add("ColumnW")
table.Columns.Add("MetNo")
Dim parser As New FileIO.TextFieldParser("C:\Folder1\SD08.CSV")
parser.Delimiters = New String() {","}
parser.HasFieldsEnclosedInQuotes = True
parser.TrimWhiteSpace = True
Do Until parser.EndOfData = True
table.Rows.Add(parser.ReadFields())
Loop
For Each row As DataRow In table.Rows
row("MetNo") = lblMetNo.Text
Next
Dim strSQLConnectionString As String = "Data Source=ServerName;Initial Catalog=DBName;Integrated Security=SSPI;"
Dim connTemp As New SqlClient.SqlConnection(strSQLConnectionString)
Dim connInsertSQL As New SqlClient.SqlConnection(strSQLConnectionString)
connTemp.Open()
connInsertSQL.Open()
Dim intReturn As Integer
Dim I As Integer
Dim strSQL As String = ""
Dim strSQLInsert As String = ""
Dim cmdInsertSQL As SqlClient.SqlCommand
For Each row As DataRow In table.Rows
strSQL = "SELECT COUNT(*) AS NoOfRecords FROM MetDB WHERE ColumnA = @ColumnA AND " _
& "ColumnB = @ColumnB AND ColumnC = @ColumnC AND MetNo = @MetNo"
Dim cmdTemp As New SqlClient.SqlCommand(strSql, connTemp)
With cmdTemp.Parameters
.AddWithValue("@ColumnA", row("ColumnA"))
.AddWithValue("@ColumnB", row("ColumnB"))
.AddWithValue("@ColumnC", row("ColumnC"))
.AddWithValue("@MetNo", lblMetNo.Text)
End With
intReturn = Convert.ToInt16(cmdTemp.ExecuteScalar)
If (intReturn = 0) Then
strSQLInsert = "INSERT INTO dbo.MetDB " _
& "(ColumnA, ColumnB, ColumnC, ColumnD, ColumnE, ColumnF, ColumnG, ColumnH, ColumnI, ColumnJ, ColumnK, ColumnL, ColumnM, ColumnN, ColumnO, " _
& "ColumnP, ColumnQ, ColumnR, ColumnS, ColumnT, ColumnU, ColumnV, ColumnW, MetNo) VALUES (@ColumnA, @ColumnB, @ColumnC, @ColumnD, " _
& "@ColumnE, @ColumnF, @ColumnG, @ColumnH, @ColumnI, @ColumnJ, @ColumnK, @ColumnL, @ColumnM, @ColumnN, @ColumnO, @ColumnP, @ColumnQ, " _
& "@ColumnR, @ColumnS, @ColumnT, @ColumnU, @ColumnV, @ColumnW, @MetNo)"
cmdInsertSQL = New SqlClient.SqlCommand(strSQLInsert, connInsertSQL)
With cmdInsertSQL.Parameters
cmdInsertSQL.Parameters.Add("@ColumnA", SqlDbType.Int, 4, "ColumnA")
cmdInsertSQL.Parameters.Add("@ColumnB", SqlDbType.DateTime, 10, "ColumnB")
cmdInsertSQL.Parameters.Add("@ColumnC", SqlDbType.Time, 8, "ColumnC")
cmdInsertSQL.Parameters.Add("@ColumnD", SqlDbType.VarChar, 50, "ColumnD")
cmdInsertSQL.Parameters.Add("@ColumnE", SqlDbType.VarChar, 50, "ColumnE")
cmdInsertSQL.Parameters.Add("@ColumnF", SqlDbType.Decimal, 28, "ColumnF")
cmdInsertSQL.Parameters("@ColumnF").Precision = 28
cmdInsertSQL.Parameters("@ColumnF").Scale = 10
cmdInsertSQL.Parameters.Add("@ColumnG", SqlDbType.VarChar, 50, "ColumnG")
cmdInsertSQL.Parameters.Add("@ColumnH", SqlDbType.VarChar, 50, "ColumnH")
cmdInsertSQL.Parameters.Add("@ColumnI", SqlDbType.Decimal, 28, "ColumnI")
cmdInsertSQL.Parameters("@ColumnI").Precision = 28
cmdInsertSQL.Parameters("@ColumnI").Scale = 10
cmdInsertSQL.Parameters.Add("@ColumnJ", SqlDbType.VarChar, 50, "ColumnJ")
cmdInsertSQL.Parameters.Add("@ColumnK", SqlDbType.Decimal, 28, "ColumnK")
cmdInsertSQL.Parameters("@ColumnK").Precision = 28
cmdInsertSQL.Parameters("@ColumnK").Scale = 10
cmdInsertSQL.Parameters.Add("@ColumnL", SqlDbType.VarChar, 50, "ColumnL")
cmdInsertSQL.Parameters.Add("@ColumnM", SqlDbType.Int, 10, "ColumnM")
cmdInsertSQL.Parameters.Add("@ColumnN", SqlDbType.VarChar, 50, "ColumnN")
cmdInsertSQL.Parameters.Add("@ColumnO", SqlDbType.Int, 4, "ColumnO")
cmdInsertSQL.Parameters.Add("@ColumnP", SqlDbType.VarChar, 50, "ColumnP")
cmdInsertSQL.Parameters.Add("@ColumnQ", SqlDbType.Int, 4, "ColumnQ")
cmdInsertSQL.Parameters.Add("@ColumnR", SqlDbType.VarChar, 50, "ColumnR")
cmdInsertSQL.Parameters.Add("@ColumnS", SqlDbType.Int, 4, "ColumnS")
cmdInsertSQL.Parameters.Add("@ColumnT", SqlDbType.VarChar, 50, "ColumnT")
cmdInsertSQL.Parameters.Add("@ColumnU", SqlDbType.Int, 4, "ColumnU")
cmdInsertSQL.Parameters.Add("@ColumnV", SqlDbType.VarChar, 50, "ColumnV")
cmdInsertSQL.Parameters.Add("@ColumnW", SqlDbType.Int, 4, "ColumnW")
cmdInsertSQL.Parameters.Add("@MetNo", SqlDbType.VarChar, 6, "MetNo")
End With
cmdInsertSQL.ExecuteNonQuery()
Else
MsgBox("Record Exists", MessageBoxIcon.Error, "Error")
End If
Next
End Sub
答案 0 :(得分:1)
您没有说出代码抛出错误的位置,但是如何添加cmdInsertSQL
的参数对我来说似乎不对。您正在使用Parameters.Add
的4参数重载,其中第4个参数是sourceColumn
,我认为(我自己从未使用过!)用于从DataTable
进行更新。
因此,我相信您当前的代码是添加参数,但没有指定值,因此错误。相反,您应该设置row
中匹配列的值:
cmdInsertSQL.Parameters.Add("@ColumnA", SqlDbType.Int, 4).Value = row("ColumnA")