public static int Update(mtDeparturePoint d) { string SQL_INSERT = @"INSERT INTO dbo.mt_DeparturePoint (DeparturePoint) VALUES (@DeparturePoint); SELECT @@IDENTITY;"; string SQL_UPDATE = @"UPDATE dbo.mt_DeparturePoint SET DeparturePoint = @DeparturePoint WHERE departurecode = @departurecode"; using (SqlConnection cn = new SqlConnection(Config.ConnectionString)) { cn.Open(); SqlCommand cmd; if (d.departureCode > 0) { cmd = new SqlCommand(SQL_UPDATE, cn); cmd.Parameters.Add("@departurecode", SqlDbType.Int).Value = d.departureCode; cmd.Parameters.Add("@departurepoint", SqlDbType.VarChar, 50).Value = d.departurePoint; cmd.ExecuteNonQuery(); } else { cmd = new SqlCommand(SQL_INSERT, cn); cmd.Parameters.Add("@departurepoint", SqlDbType.VarChar, 50).Value = d.departurePoint; d.departureCode = Convert.ToInt32(cmd.ExecuteScalar()); } } return(d.departureCode); }
public static mtDeparturePoint GetDeparturePoint(int departureCode) { string sSQL = "SELECT * FROM dbo.mt_DeparturePoint WHERE departurecode=@departurecode"; using (SqlConnection cn = new SqlConnection(Config.ConnectionString)) { cn.Open(); SqlCommand cmd = new SqlCommand(sSQL, cn); cmd.Parameters.Add("@departurecode", SqlDbType.Int).Value = departureCode; SqlDataReader rs = cmd.ExecuteReader(CommandBehavior.CloseConnection); if (!rs.Read()) { return(null); } mtDeparturePoint d = new mtDeparturePoint(); d.departureCode = (int)rs["departurecode"]; d.departurePoint = rs["departurepoint"] + ""; return(d); } }