我的SQL准备语句不起作用

时间:2015-09-24 13:08:59

标签: c# mysql prepared-statement

我正在尝试将一些数据插入到我最近安装的本地MYSQL数据库中。这是我第一次尝试使用MYSQL,因为我通常使用MSSQL。我从未遇到过使用MSSQL准备好的语句的问题。

下面的准备好的声明不起作用,MySqlCommand comm的输出总是与初始命令字符串完全一致。我试过更换所有的@?那不起作用。我已经删除了字符串前面的@符号和其他几个组合。我已经用几年时间搜索了这个问题而没有真正的答案。通常有人在他们的字符串中有某种拼写错误。我的插入字符串是直接从HeidiSQL的insert语句中复制的,我将所有@variables替换为C#代码和常规插入语句。

 string cs = @"server=localhost;userid=root;database=busdata;";

         MySqlConnection conn = null;
         conn = new MySqlConnection(cs);
         conn.Open();

         try
         {

             MySqlCommand comm = conn.CreateCommand();
             comm.CommandText = @"INSERT INTO `busdata`.`buses` (`RecordedAtTime`, `DirectionRef`, `JourneyPatternRef`, `PublishedLineName`, `OperatorRef`, `OriginRef`, `DestinationRef`, `DestinationName`, `Monitored`, `Bearing`, `ProgressRate`, `BlockRef`, `VehicleRef`, `OriginAimedDepartureTime`, `ProgressStatus`, `Longitude`, `Latitude`) VALUES (@RecordedAtTime, @DirectionRef, @JourneyPatternRef, @PublishedLineName, @OperatorRef, @OriginRef, @DestinationRef, @DestinationName, @Monitored, @Bearing, @ProgressRate, @BlockRef, @VehicleRef, @OriginAimedDepartureTime, @ProgressStatus, @Longitude, @Latitude`); ";



             foreach (Vehicleactivity bus in buses)
             {              

                 comm.Parameters.AddWithValue("@RecordedAtTime", bus.RecordedAtTime.ToString());
                 comm.Parameters.AddWithValue("@DirectionRef", bus.MonitoredVehicleJourney.DirectionRef);
                 comm.Parameters.AddWithValue("@JourneyPatternRef", bus.MonitoredVehicleJourney.JourneyPatternRef);
                 comm.Parameters.AddWithValue("@PublishedLineName", bus.MonitoredVehicleJourney.PublishedLineName);
                 comm.Parameters.AddWithValue("@OperatorRef", bus.MonitoredVehicleJourney.OperatorRef);
                 comm.Parameters.AddWithValue("@OriginRef", bus.MonitoredVehicleJourney.OriginRef);
                 comm.Parameters.AddWithValue("@DestinationRef", bus.MonitoredVehicleJourney.DestinationRef);
                 comm.Parameters.AddWithValue("@DestinationName", bus.MonitoredVehicleJourney.DestinationName);
                 comm.Parameters.AddWithValue("@Monitored", bus.MonitoredVehicleJourney.Monitored.ToString());
                 comm.Parameters.AddWithValue("@Bearing", bus.MonitoredVehicleJourney.Bearing.ToString());
                 comm.Parameters.AddWithValue("@ProgressRate", bus.MonitoredVehicleJourney.ProgressRate);
                 comm.Parameters.AddWithValue("@BlockRef", bus.MonitoredVehicleJourney.BlockRef);
                 comm.Parameters.AddWithValue("@VehicleRef", bus.MonitoredVehicleJourney.VehicleRef);
                 comm.Parameters.AddWithValue("@OriginAimedDepartureTime", bus.MonitoredVehicleJourney.OriginAimedDepartureTime);
                 comm.Parameters.AddWithValue("@ProgressStatus", bus.MonitoredVehicleJourney.ProgressStatus);
                 comm.Parameters.AddWithValue("@Longitude", bus.MonitoredVehicleJourney.VehicleLocation.Longitude.ToString());
                 comm.Parameters.AddWithValue("@Latitude", bus.MonitoredVehicleJourney.VehicleLocation.Latitude.ToString());
                 comm.Prepare();
                 Console.WriteLine(comm.CommandText.ToString());
                 comm.ExecuteNonQuery();
                 comm.Parameters.Clear();


             }

         }
         catch (MySqlException ex)
         {
             Console.WriteLine("Error: {0}", ex.ToString());

1 个答案:

答案 0 :(得分:1)

您在@location附近的插入查询中有一个反引号。删除反引号..它应该可以正常工作