-2

ネストされた SELECT ステートメントを使用して INSERT ステートメントを実行しようとしています。データベース フィールドは両方ともint型です。

私の声明:

energy_command = New SqlCommand("INSERT INTO energy ([ApplicationID], [usage], [policy], [audit], [lighting], [over_lighting], [controls], [turn_off], [energy_star], [weatherize],[thermostat], [natural_light], [air], [renewable_assessment], [on_site_renewable], [HVAC],[renewable_power], [efficient_enroll], [efficient_attain], [other]) " & 
"VALUES ('(SELECT ApplicationID FROM general where Business_Name = """ & txtName.Text & """)', '" & track_usage & "', '" & develop_strategy & "', '" & perform_audit & "', '" & replace_bulbs & "', '" & reduce_lighting & "', '" & lighting_controls & "', '" & not_in_use_policy & "', '" & energy_star & "', '" & weatherize & "', '" & program_thermo & "', '" & natural_light & "', '" & air_quality & "', '" & site_assessment & "', '" & renewable_power & "', '" & HVAC & "', '" & renewable_energy & "', '" & energy_programs & "', '" & energy_certification & "', '" & energy_other & "')", connection)`

私のエラー:

System.Data.SqlClient.SqlException: varchar 値 '(SELECT ApplicationID FROM general where Business_Name = "a")' をデータ型 int に変換するときに変換に失敗しました。

SELECT私の唯一の考えは、ステートメント全体をINTフィールドに挿入しようとしているということです。私が間違っている?どうすればこれを修正できますか?

4

3 に答える 3

2

クエリを引用するために単一引用符を入れるため、文字列と見なされます。これだけでクエリの一重引用符を削除する必要があります

VALUES ((SELECT ApplicationID FROM general where Business_Name = "'" & txtName.Text & "'"), ....
于 2012-05-14T06:36:37.007 に答える
1

クエリの前後のアポストロフィを削除するだけです。

変化する:

'(SELECT ApplicationID FROM general where Business_Name = """ & txtName.Text & """)'

に:

(SELECT ApplicationID FROM general where Business_Name = """ & txtName.Text & """)
于 2012-05-14T06:36:10.280 に答える
0

SELECTまた、別のテーブルのに基づいてデータをテーブルに挿入する場合は、キーワードを使用せずに、次のスタイルを使用する必要があります。VALUES

INSERT INTO dbo.YourTargetTable(Co1, Col2, ..., ColN)
   SELECT
      Src1, Src2, ...., SrcN
   FROM  
      dbo.YourSourceTableHere
   WHERE
      YourConditionHere

必要ありませんVALUES...

于 2012-05-14T06:45:37.927 に答える