public static void RawData(string raw_data, string IMEI, DateTime gps_date) { SqlConnection conn = new SqlConnection(GetConnectionString()); string sql = "INSERT INTO TBL_RAW_GPS (IMEI,RawData,GPS_Date) VALUES" + "(@IMEI,@RawData,@GPS_Date) SELECT SCOPE_IDENTITY()"; try { if (conn.State != ConnectionState.Open) { conn.Open(); } SqlCommand cmd = new SqlCommand(sql, conn); List <SqlParameter> sp = new List <SqlParameter>() { new SqlParameter() { ParameterName = "@IMEI", SqlDbType = SqlDbType.NVarChar, Value = IMEI }, new SqlParameter() { ParameterName = "@RawData", SqlDbType = SqlDbType.NVarChar, Value = raw_data }, new SqlParameter() { ParameterName = "@GPS_Date", SqlDbType = SqlDbType.DateTime, Value = gps_date }, }; cmd.Parameters.AddRange(sp.ToArray()); cmd.CommandType = CommandType.Text; var id = cmd.ExecuteScalar(); } catch (Exception ex) { AtrackMain.LogData(ex.Message); } finally { conn.Close(); } }
public static void DbInsert(GpsInfo data) { SqlConnection conn = new SqlConnection(GetConnectionString()); string sql = "INSERT INTO [dbo].[TBL_ATRACK_MASTER]" + " (IMEI,Length,Crc,SeqId,GPS_DateTime,Received_DateTime,Longitude,Latitude,Odometer,Heading,Speed) " + "VALUES " + "(@IMEI,@Length,@Crc,@SeqId,@GPS_DateTime,@Received_DateTime,@Longitude,@Latitude,@Odometer,@Heading,@Speed) SELECT SCOPE_IDENTITY()"; try { if (conn.State != ConnectionState.Open) { conn.Open(); } SqlCommand cmd = new SqlCommand(sql, conn); List <SqlParameter> sp = new List <SqlParameter>(); sp.Add(new SqlParameter() { ParameterName = "@IMEI", SqlDbType = SqlDbType.NVarChar, Value = data.IMEI }); sp.Add(new SqlParameter() { ParameterName = "@Length", SqlDbType = SqlDbType.Int, Value = data.Length }); sp.Add(new SqlParameter() { ParameterName = "@Crc", SqlDbType = SqlDbType.NVarChar, Value = data.Crc }); sp.Add(new SqlParameter() { ParameterName = "@SeqId", SqlDbType = SqlDbType.Int, Value = data.SeqId }); sp.Add(new SqlParameter() { ParameterName = "@GPS_DateTime", SqlDbType = SqlDbType.DateTime, Value = data.GPS_DateTime }); sp.Add(new SqlParameter() { ParameterName = "@Received_DateTime", SqlDbType = SqlDbType.DateTime, Value = data.Received_DateTime }); sp.Add(new SqlParameter() { ParameterName = "@Longitude", SqlDbType = SqlDbType.Float, Value = data.Longitude }); sp.Add(new SqlParameter() { ParameterName = "@Latitude", SqlDbType = SqlDbType.Float, Value = data.Latitude }); sp.Add(new SqlParameter() { ParameterName = "@Odometer", SqlDbType = SqlDbType.Float, Value = data.Odometer }); sp.Add(new SqlParameter() { ParameterName = "@Heading", SqlDbType = SqlDbType.Int, Value = data.Heading }); sp.Add(new SqlParameter() { ParameterName = "@Speed", SqlDbType = SqlDbType.Int, Value = data.Speed }); cmd.Parameters.AddRange(sp.ToArray()); cmd.CommandType = CommandType.Text; var id = cmd.ExecuteScalar(); } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert Error:"; msg += ex.Message; AtrackMain.LogData(msg); throw new Exception(msg); } finally { conn.Close(); } }