コード例 #1
0
        public bool SaveCustomer(MyCustomer customer)
        {
            SqlCommand cmd = new SqlCommand("InsertCustomer", _cn);

            cmd.CommandType = CommandType.StoredProcedure;

            if (customer.Id != 0)
            {
                cmd.Parameters.Add("@Id", SqlDbType.Int).Value = customer.Id;
            }
            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value
                = customer.Name;
            cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value
                = customer.FirstName;
            cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value
                = customer.LastName;
            cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value
                = customer.Email;
            cmd.Parameters.Add("@DateBirth", SqlDbType.DateTime).Value
                = customer.DateBirth;

            int id = Convert.ToInt32(cmd.ExecuteScalar());

            customer.Id = id;
            return(id > 0);
        }
コード例 #2
0
        private static MyCustomer GetCustomerByReader(SqlDataReader reader)
        {
            MyCustomer customer = new MyCustomer();

            customer.Id        = reader.GetInt32(0);
            customer.Name      = reader.GetString(1);
            customer.FirstName = reader.GetString(2);
            customer.LastName  = reader.GetString(3);
            customer.Email     = reader.GetString(4);
            customer.DateBirth = reader.GetDateTime(5);
            return(customer);
        }
コード例 #3
0
        public MyCustomer GetCustomerById(int id)
        {
            SqlCommand cmd = new SqlCommand("GetCustomers", _cn);

            cmd.CommandType = CommandType.StoredProcedure;

            SqlParameter param = new SqlParameter();

            param.ParameterName = "@Id";
            param.SqlDbType     = SqlDbType.Int;
            param.Value         = id;

            cmd.Parameters.Add(param);

            SqlDataReader reader   = cmd.ExecuteReader();
            MyCustomer    customer = null;

            while (reader.Read())
            {
                customer = GetCustomerByReader(reader);
            }
            return(customer);
        }