コード例 #1
0
        //
        // Delete the Employee by ID.
        //   This method assumes that ConflictDetection is set to OverwriteValues.

        public int DeleteEmployee(NorthwindEmployee employee)
        {
            const string sqlCmd = "DELETE FROM Employees WHERE EmployeeID = @EmployeeID";

            SqlConnection conn = new SqlConnection(m_connectionString);
            SqlCommand    cmd  = new SqlCommand(sqlCmd, conn);

            cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = employee.EmployeeID;

            int result = 0;

            try
            {
                conn.Open();

                result = cmd.ExecuteNonQuery();
            }
            catch (SqlException)
            {
                // Handle exception.
            }
            finally
            {
                conn.Close();
            }

            return(result);
        }
コード例 #2
0
        private static NorthwindEmployee GetNorthwindEmployeeFromReader(IDataRecord reader)
        {
            NorthwindEmployee employee = new NorthwindEmployee();

            employee.EmployeeID = reader.GetInt32(0);
            employee.LastName   = reader.GetString(1);
            employee.FirstName  = reader.GetString(2);

            if (reader.GetValue(3) != DBNull.Value)
            {
                employee.Address = reader.GetString(3);
            }

            if (reader.GetValue(4) != DBNull.Value)
            {
                employee.City = reader.GetString(4);
            }

            if (reader.GetValue(5) != DBNull.Value)
            {
                employee.Region = reader.GetString(5);
            }

            if (reader.GetValue(6) != DBNull.Value)
            {
                employee.PostalCode = reader.GetString(6);
            }

            return(employee);
        }
コード例 #3
0
        // Insert an Employee.

        public int InsertEmployee(NorthwindEmployee employee)
        {
            if (String.IsNullOrEmpty(employee.FirstName))
            {
                throw new ArgumentException("FirstName cannot be null or an empty string.");
            }
            if (String.IsNullOrEmpty(employee.LastName))
            {
                throw new ArgumentException("LastName cannot be null or an empty string.");
            }

            if (employee.Address == null)
            {
                employee.Address = String.Empty;
            }
            if (employee.City == null)
            {
                employee.City = String.Empty;
            }
            if (employee.Region == null)
            {
                employee.Region = String.Empty;
            }
            if (employee.PostalCode == null)
            {
                employee.PostalCode = String.Empty;
            }

            SqlConnection conn = new SqlConnection(m_connectionString);
            SqlCommand    cmd  = new SqlCommand("INSERT INTO Employees " +
                                                "  (FirstName, LastName, Address, City, Region, PostalCode) " +
                                                "  Values(@FirstName, @LastName, @Address, @City, @Region, @PostalCode); " +
                                                "SELECT @EmployeeID = SCOPE_IDENTITY()", conn);

            cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 10).Value  = employee.FirstName;
            cmd.Parameters.Add("@LastName", SqlDbType.VarChar, 20).Value   = employee.LastName;
            cmd.Parameters.Add("@Address", SqlDbType.VarChar, 60).Value    = employee.Address;
            cmd.Parameters.Add("@City", SqlDbType.VarChar, 15).Value       = employee.City;
            cmd.Parameters.Add("@Region", SqlDbType.VarChar, 15).Value     = employee.Region;
            cmd.Parameters.Add("@PostalCode", SqlDbType.VarChar, 10).Value = employee.PostalCode;
            SqlParameter p = cmd.Parameters.Add("@EmployeeID", SqlDbType.Int);

            p.Direction = ParameterDirection.Output;

            int newEmployeeID = 0;

            try
            {
                conn.Open();

                cmd.ExecuteNonQuery();

                newEmployeeID = (int)p.Value;
            }
            catch (SqlException)
            {
                // Handle exception.
            }
            finally
            {
                conn.Close();
            }

            return(newEmployeeID);
        }
コード例 #4
0
        //
        // Update the Employee by ID.
        //   This method assumes that ConflictDetection is set to OverwriteValues.

        public int UpdateEmployee(NorthwindEmployee employee)
        {
            if (String.IsNullOrEmpty(employee.FirstName))
            {
                throw new ArgumentException("FirstName cannot be null or an empty string.");
            }
            if (String.IsNullOrEmpty(employee.LastName))
            {
                throw new ArgumentException("LastName cannot be null or an empty string.");
            }

            if (employee.Address == null)
            {
                employee.Address = String.Empty;
            }
            if (employee.City == null)
            {
                employee.City = String.Empty;
            }
            if (employee.Region == null)
            {
                employee.Region = String.Empty;
            }
            if (employee.PostalCode == null)
            {
                employee.PostalCode = String.Empty;
            }

            SqlConnection conn = new SqlConnection(m_connectionString);
            SqlCommand    cmd  = new SqlCommand("UPDATE Employees " +
                                                "  SET FirstName=@FirstName, LastName=@LastName, " +
                                                "  Address=@Address, City=@City, Region=@Region, " +
                                                "  PostalCode=@PostalCode " +
                                                "  WHERE EmployeeID=@EmployeeID", conn);

            cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 10).Value  = employee.FirstName;
            cmd.Parameters.Add("@LastName", SqlDbType.VarChar, 20).Value   = employee.LastName;
            cmd.Parameters.Add("@Address", SqlDbType.VarChar, 60).Value    = employee.Address;
            cmd.Parameters.Add("@City", SqlDbType.VarChar, 15).Value       = employee.City;
            cmd.Parameters.Add("@Region", SqlDbType.VarChar, 15).Value     = employee.Region;
            cmd.Parameters.Add("@PostalCode", SqlDbType.VarChar, 10).Value = employee.PostalCode;
            cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value         = employee.EmployeeID;

            int result = 0;

            try
            {
                conn.Open();

                result = cmd.ExecuteNonQuery();
            }
            catch (SqlException)
            {
                // Handle exception.
            }
            finally
            {
                conn.Close();
            }

            return(result);
        }
コード例 #5
0
ファイル: Employee.cs プロジェクト: webgrid/WebGrid
        private static NorthwindEmployee GetNorthwindEmployeeFromReader(IDataRecord reader)
        {
            NorthwindEmployee employee = new NorthwindEmployee();

            employee.EmployeeID = reader.GetInt32(0);
            employee.LastName = reader.GetString(1);
            employee.FirstName = reader.GetString(2);

            if (reader.GetValue(3) != DBNull.Value)
                employee.Address = reader.GetString(3);

            if (reader.GetValue(4) != DBNull.Value)
                employee.City = reader.GetString(4);

            if (reader.GetValue(5) != DBNull.Value)
                employee.Region = reader.GetString(5);

            if (reader.GetValue(6) != DBNull.Value)
                employee.PostalCode = reader.GetString(6);

            return employee;
        }
コード例 #6
0
ファイル: Employee.cs プロジェクト: webgrid/WebGrid
        //
        // Update the Employee by ID.
        //   This method assumes that ConflictDetection is set to OverwriteValues.
        public int UpdateEmployee(NorthwindEmployee employee)
        {
            if (String.IsNullOrEmpty(employee.FirstName))
                throw new ArgumentException("FirstName cannot be null or an empty string.");
            if (String.IsNullOrEmpty(employee.LastName))
                throw new ArgumentException("LastName cannot be null or an empty string.");

            if (employee.Address == null)
            {
                employee.Address = String.Empty;
            }
            if (employee.City == null)
            {
                employee.City = String.Empty;
            }
            if (employee.Region == null)
            {
                employee.Region = String.Empty;
            }
            if (employee.PostalCode == null)
            {
                employee.PostalCode = String.Empty;
            }

            SqlConnection conn = new SqlConnection(m_connectionString);
            SqlCommand cmd = new SqlCommand("UPDATE Employees " +
                                            "  SET FirstName=@FirstName, LastName=@LastName, " +
                                            "  Address=@Address, City=@City, Region=@Region, " +
                                            "  PostalCode=@PostalCode " +
                                            "  WHERE EmployeeID=@EmployeeID", conn);

            cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 10).Value = employee.FirstName;
            cmd.Parameters.Add("@LastName", SqlDbType.VarChar, 20).Value = employee.LastName;
            cmd.Parameters.Add("@Address", SqlDbType.VarChar, 60).Value = employee.Address;
            cmd.Parameters.Add("@City", SqlDbType.VarChar, 15).Value = employee.City;
            cmd.Parameters.Add("@Region", SqlDbType.VarChar, 15).Value = employee.Region;
            cmd.Parameters.Add("@PostalCode", SqlDbType.VarChar, 10).Value = employee.PostalCode;
            cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = employee.EmployeeID;

            int result = 0;

            try
            {
                conn.Open();

                result = cmd.ExecuteNonQuery();
            }
            catch (SqlException)
            {
                // Handle exception.
            }
            finally
            {
                conn.Close();
            }

            return result;
        }
コード例 #7
0
ファイル: Employee.cs プロジェクト: webgrid/WebGrid
        // Insert an Employee.
        public int InsertEmployee(NorthwindEmployee employee)
        {
            if (String.IsNullOrEmpty(employee.FirstName))
                throw new ArgumentException("FirstName cannot be null or an empty string.");
            if (String.IsNullOrEmpty(employee.LastName))
                throw new ArgumentException("LastName cannot be null or an empty string.");

            if (employee.Address == null)
            {
                employee.Address = String.Empty;
            }
            if (employee.City == null)
            {
                employee.City = String.Empty;
            }
            if (employee.Region == null)
            {
                employee.Region = String.Empty;
            }
            if (employee.PostalCode == null)
            {
                employee.PostalCode = String.Empty;
            }

            SqlConnection conn = new SqlConnection(m_connectionString);
            SqlCommand cmd = new SqlCommand("INSERT INTO Employees " +
                                            "  (FirstName, LastName, Address, City, Region, PostalCode) " +
                                            "  Values(@FirstName, @LastName, @Address, @City, @Region, @PostalCode); " +
                                            "SELECT @EmployeeID = SCOPE_IDENTITY()", conn);

            cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 10).Value = employee.FirstName;
            cmd.Parameters.Add("@LastName", SqlDbType.VarChar, 20).Value = employee.LastName;
            cmd.Parameters.Add("@Address", SqlDbType.VarChar, 60).Value = employee.Address;
            cmd.Parameters.Add("@City", SqlDbType.VarChar, 15).Value = employee.City;
            cmd.Parameters.Add("@Region", SqlDbType.VarChar, 15).Value = employee.Region;
            cmd.Parameters.Add("@PostalCode", SqlDbType.VarChar, 10).Value = employee.PostalCode;
            SqlParameter p = cmd.Parameters.Add("@EmployeeID", SqlDbType.Int);
            p.Direction = ParameterDirection.Output;

            int newEmployeeID = 0;

            try
            {
                conn.Open();

                cmd.ExecuteNonQuery();

                newEmployeeID = (int)p.Value;
            }
            catch (SqlException)
            {
                // Handle exception.
            }
            finally
            {
                conn.Close();
            }

            return newEmployeeID;
        }
コード例 #8
0
ファイル: Employee.cs プロジェクト: webgrid/WebGrid
        //
        // Delete the Employee by ID.
        //   This method assumes that ConflictDetection is set to OverwriteValues.
        public int DeleteEmployee(NorthwindEmployee employee)
        {
            const string sqlCmd = "DELETE FROM Employees WHERE EmployeeID = @EmployeeID";

            SqlConnection conn = new SqlConnection(m_connectionString);
            SqlCommand cmd = new SqlCommand(sqlCmd, conn);
            cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = employee.EmployeeID;

            int result = 0;

            try
            {
                conn.Open();

                result = cmd.ExecuteNonQuery();
            }
            catch (SqlException)
            {
                // Handle exception.
            }
            finally
            {
                conn.Close();
            }

            return result;
        }