我正在尝试此代码,但出现错误:
Dim cmd3 As OleDbCommand = New OleDbCommand("INSERT INTO Criteria (Calculation, [Interval], Weight) VALUES (?, ?, ?)", Nordeen_Investing_3.con)
cmd3.Parameters.AddWithValue("@Calculation", Calculation)
cmd3.Parameters.AddWithValue("@[Interval]", Interval)
cmd3.Parameters.AddWithValue("@Weight", "(SELECT (Avg(C." & Calculation & " * G.Growth) - Avg(C." & Calculation & ") * Avg(G.Growth)) / (StDevP(C." & Calculation & ") * StDevP(G.Growth)) AS Weight FROM " & Calculation & " AS C INNER JOIN " & Growth_Rate & " AS G " & On_Statement & ")")
cmd3.ExecuteNonQuery()
这是错误消息:
Data type mismatch in criteria expression.
问题在于这行代码:
cmd3.Parameters.AddWithValue("@Weight", "(SELECT (Avg(C." & Calculation & " * G.Growth) - Avg(C." & Calculation & ") * Avg(G.Growth)) / (StDevP(C." & Calculation & ") * StDevP(G.Growth)) AS Weight _
FROM " & Calculation & " AS C _
INNER JOIN " & Growth_Rate & " AS G _
" & On_Statement & ")")
如果我用产生Select Statement
的值替换 ,Select Statement
那么代码运行良好。
我怎样才能让它工作?
更新 根据以下建议,我将代码更改为:
Dim cmd3 As OleDbCommand = New OleDbCommand("INSERT INTO Criteria (Calculation, [Interval], Weight) VALUES (?, ?, (SELECT (Avg(C." & Calculation & " * G.Growth) - Avg(C." & Calculation & ") * Avg(G.Growth)) / (StDevP(C." & Calculation & ") * StDevP(G.Growth)) AS Weight FROM " & Calculation & " AS C INNER JOIN " & Growth_Rate & " AS G " & On_Statement & "))", Nordeen_Investing_3.con)
cmd3.Parameters.AddWithValue("@Calculation", Calculation)
cmd3.Parameters.AddWithValue("@[Interval]", Interval)
cmd3.ExecuteNonQuery()
运行新代码时收到此错误:
Unspecified error