ログ ファイル セクションを SQL に変換する際に問題が発生しDateTime
ましたが、より良い解決策は、コードではなくデータベースによって日付フィールドを生成することであることがわかりました ( を使用GETDATE()
)。
挿入から日付フィールドを削除しましたが、次のエラーが発生しました。
エラー: パラメータ化されたクエリ '(@station int, @indoor_temp float, @outdoor_temp float, @dewpoint f' は、指定されていないパラメータ '@wind_direction_text' を予期しています。
問題のある値が提供されますが、奇妙なことに、挿入は引き続き成功し、データベース レコードが挿入されます。
なぜこれらの例外が発生するのかを知りたいのですが!
これが私のコードです:
var dataCommand = new SqlCommand();
dataCommand.Connection = dataConnection;
dataCommand.Parameters.AddWithValue("@station", _stationId);
dataCommand.Parameters.AddWithValue("@indoor_temp", _indoorTemp);
dataCommand.Parameters.AddWithValue("@outdoor_temp", _outdoorTemp);
dataCommand.Parameters.AddWithValue("@dewpoint", _dewPoint);
dataCommand.Parameters.AddWithValue("@rel_humidity_indoor", _relHumidityIndoor);
dataCommand.Parameters.AddWithValue("@rel_humidity_outdoor", _relHumidityOutdoor);
dataCommand.Parameters.AddWithValue("@wind_speed", _windSpeed);
dataCommand.Parameters.AddWithValue("@wind_direction_degrees", _windDirectionDegrees);
dataCommand.Parameters.AddWithValue("@wind_direction_text", _windDirectionText);
dataCommand.Parameters.AddWithValue("@wind_chill", _windChill);
dataCommand.Parameters.AddWithValue("@rain1h", _rain1H);
dataCommand.Parameters.AddWithValue("@rain24h", _rain24H);
dataCommand.Parameters.AddWithValue("@rain_total", _totalRain);
dataCommand.Parameters.AddWithValue("@relative_pressure", _relativePressure);
dataCommand.Parameters.AddWithValue("@tendency", _tendency);
dataCommand.Parameters.AddWithValue("@forecast", _forecast);
dataCommand.CommandType = CommandType.Text;
dataCommand.CommandText =
"INSERT INTO"
+ " dbo.WeatherData(station_id, indoor_temp, outdoor_temp, dewpoint," +
"rel_humidity_indoor, rel_humidity_outdoor, wind_speed, wind_direction_degrees," +
"wind_direction_text, wind_chill, rain1h, rain24h, rain_total, relative_pressure," +
"tendency, forecast)"
+ "VALUES " +
"(@station, @indoor_temp, @outdoor_temp, @dewpoint, @rel_humidity_indoor," +
"@rel_humidity_outdoor, @wind_speed, @wind_direction_degrees," +
"@wind_direction_text, @wind_chill, @rain1h, @rain24h, @rain_total," +
"@relative_pressure, @tendency, @forecast)";
// execute the insert command
dataCommand.ExecuteNonQuery();
ご協力いただきありがとうございます!