public static int Update(mtShip s) { string SQL_INSERT = @"INSERT INTO dbo.mt_Ship (ShipName, VendorCode) VALUES (@ShipName, @VendorCode); SELECT @@IDENTITY;"; string SQL_UPDATE = @"UPDATE dbo.mt_Ship SET ShipName = @ShipName, vendorCode = @VendorCode WHERE shipcode = @shipcode"; using (SqlConnection cn = new SqlConnection(Config.ConnectionString)) { cn.Open(); SqlCommand cmd; if (s.shipCode > 0) { cmd = new SqlCommand(SQL_UPDATE, cn); cmd.Parameters.Add("@shipcode", SqlDbType.Int).Value = s.shipCode; FillCmd(cmd, s); cmd.ExecuteNonQuery(); } else { cmd = new SqlCommand(SQL_INSERT, cn); FillCmd(cmd, s); s.shipCode = Convert.ToInt32(cmd.ExecuteScalar()); } } return(s.shipCode); }
private static void FillCmd(SqlCommand cmd, mtShip s) { cmd.Parameters.Add("@shipname", SqlDbType.VarChar, 50).Value = s.shipName; cmd.Parameters.Add("@vendorcode", SqlDbType.VarChar, 10).Value = DBNull.Value; if (s.vendorCode != "") { cmd.Parameters["@vendorcode"].Value = s.vendorCode; } }
public static mtShip GetShip(int shipCode) { string sSQL = "SELECT * FROM dbo.mt_Ship WHERE shipCode=@shipCode"; using (SqlConnection cn = new SqlConnection(Config.ConnectionString)) { cn.Open(); SqlCommand cmd = new SqlCommand(sSQL, cn); cmd.Parameters.Add("@shipcode", SqlDbType.Int).Value = shipCode; SqlDataReader rs = cmd.ExecuteReader(CommandBehavior.CloseConnection); if (!rs.Read()) { return(null); } mtShip s = new mtShip(); s.shipCode = (int)rs["shipcode"]; s.shipName = rs["shipname"] + ""; s.vendorCode = rs["vendorcode"] + ""; return(s); } }