<pre lang="c#"> IDbConnection conn = null; try { conn = this.GetConnection(); conn.Open(); IDbCommand cmd = conn.CreateCommand(); //string insertSQL = @"insert into Routes(FromCityId,ToCityId,DistanceInKms,Status) values(@fromcityID,@toCityID,@dis,@st);"; //cmd.CommandText = insertSQL; //cmd.CommandType = CommandType.Text; cmd.CommandText = "StoredProcedureName"; cmd.CommandType = CommandType.StoredProcedure; IDataParameter p1 = cmd.CreateParameter(); p1.ParameterName = "@FromCity"; p1.Value = RouteInfo.FromCity.CityId; cmd.Parameters.Add(p1); //........................ return cmd.ExecuteNonQuery(); } } catch (Common.ConnectToDatabaseException) { throw new RouteDAOException("Unable to add route"); } catch (Exception) { throw new RouteDAOException("Unable to add route"); } finally { conn.Close(); }
IF EXISTS(SELECT * FROM table WHERE field = value) BEGIN -- do an update since it exists UPDATE table SET field1 = value. field2 = value2 WHERE field = value END ELSE BEGIN -- Since it does not exist, do an insert INSERT INTO ... END
IF NOT EXISTS(SELECT * FROM TABLE NAME WHERE <duplicate condition="">) BEING --INSERT INTO TABLENAME (columnName1,..) VALUES (...) END </duplicate>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)