public static int Update(mtRegion r) { string SQL_INSERT = @"INSERT INTO dbo.mt_Region (RegionDescription) VALUES (@RegionDescription); SELECT @@IDENTITY;"; string SQL_UPDATE = @"UPDATE dbo.mt_Region SET RegionDescription = @RegionDescription WHERE regioncode = @regioncode"; using (SqlConnection cn = new SqlConnection(Config.ConnectionString)) { cn.Open(); SqlCommand cmd; if (r.regionCode > 0) { cmd = new SqlCommand(SQL_UPDATE, cn); cmd.Parameters.Add("@regioncode", SqlDbType.Int).Value = r.regionCode; cmd.Parameters.Add("@regiondescription", SqlDbType.VarChar, 50).Value = r.regionDescription; cmd.ExecuteNonQuery(); } else { cmd = new SqlCommand(SQL_INSERT, cn); cmd.Parameters.Add("@regiondescription", SqlDbType.VarChar, 50).Value = r.regionDescription; r.regionCode = Convert.ToInt32(cmd.ExecuteScalar()); } } return(r.regionCode); }
public static mtRegion GetRegion(int regionCode) { string sSQL = "SELECT * FROM dbo.mt_Region WHERE regionCode=@regionCode"; using (SqlConnection cn = new SqlConnection(Config.ConnectionString)) { cn.Open(); SqlCommand cmd = new SqlCommand(sSQL, cn); cmd.Parameters.Add("@regioncode", SqlDbType.Int).Value = regionCode; SqlDataReader rs = cmd.ExecuteReader(CommandBehavior.CloseConnection); if (!rs.Read()) { return(null); } mtRegion r = new mtRegion(); r.regionCode = (int)rs["regioncode"]; r.regionDescription = rs["regiondescription"] + ""; return(r); } }