예제 #1
0
        public static PersonInfo1 GetSingle(int id, out string errorMessage)
        {
            errorMessage = null;
            PersonInfo1 perdonInfo = null;

            try
            {
                string sqlQuery = "SELECT FirstName," +
                                  "LastName," +
                                  "Gender," +
                                  "DateOfBirth," +
                                  "Phone," +
                                  "[Address]," +
                                  "Town," +
                                  "PostCode," +
                                  "Country," +
                                  "IsCloseFriend " +
                                  "FROM PersonInfo1 WHERE Id = " + id.ToString();

                SqlConnection conn = new SqlConnection(connectionString);
                SqlCommand    com  = new SqlCommand(sqlQuery, conn);
                conn.Open();
                SqlDataReader reader = com.ExecuteReader();
                reader.Read();
                perdonInfo = new PersonInfo1(id,
                                             reader["FirstName"].ToString(),
                                             reader["LastName"].ToString(),
                                             reader["Gender"].ToString(),
                                             reader["DateOfBirth"].ToString(),
                                             reader["Phone"].ToString(),
                                             reader["Address"].ToString(),
                                             reader["Town"].ToString(),
                                             reader["PostCode"].ToString(),
                                             reader["Country"].ToString(),
                                             bool.Parse(reader["IsCloseFriend"].ToString()));
                conn.Close();
            }
            catch (Exception e)
            {
                errorMessage = string.Format("Error! An exception happened: {0}", e.Message);
                return(null);
            }

            return(perdonInfo);
        }
예제 #2
0
        public static void Update(PersonInfo1 person, out string errorMessage)
        {
            errorMessage = null;

            try
            {
                string sqlQuery = string.Format("UPDATE PersonInfo1 " +
                                                "SET FirstName = '{0}'," +
                                                "LastName = '{1}'," +
                                                "Gender = '{2}'," +
                                                "DateOfBirth = '{3}'," +
                                                "Phone = '{4}'," +
                                                "Address = '{5}'," +
                                                "Town = '{6}'," +
                                                "PostCode = '{7}'," +
                                                "Country = '{8}'," +
                                                "IsCloseFriend = {9} WHERE Id = {10}",
                                                person.FirstName,
                                                person.LastName,
                                                person.Gender,
                                                person.DateOfBirth,
                                                string.IsNullOrEmpty(person.Phone) ? "" : person.Phone,
                                                string.IsNullOrEmpty(person.Address) ? "" : person.Address,
                                                string.IsNullOrEmpty(person.Town) ? "" : person.Town,
                                                string.IsNullOrEmpty(person.PostCode) ? "" : person.PostCode,
                                                person.Country,
                                                person.IsCloseFriend ? 1 : 0,
                                                person.Id);



                SqlConnection conn = new SqlConnection(connectionString);
                SqlCommand    com  = new SqlCommand(sqlQuery, conn);
                conn.Open();
                com.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception e)
            {
                errorMessage = string.Format("Error! An exception happened: {0}", e.Message);
                return;
            }
        }
예제 #3
0
        public static int Add(PersonInfo1 person, out string errorMessage)
        {
            errorMessage = null;
            int id;

            try
            {
                string sqlQuery = string.Format("INSERT INTO PersonInfo1 (FirstName, LastName, Gender, DateOfBirth, Phone, [Address], Town, PostCode, Country, IsCloseFriend) " +
                                                "VALUES('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', {9}) SELECT TOP 1 Id FROM PersonInfo1 ORDER BY Id desc",
                                                person.FirstName,
                                                person.LastName,
                                                person.Gender,
                                                person.DateOfBirth,
                                                string.IsNullOrEmpty(person.Phone) ? "" : person.Phone,
                                                string.IsNullOrEmpty(person.Address) ? "" : person.Address,
                                                string.IsNullOrEmpty(person.Town) ? "" : person.Town,
                                                string.IsNullOrEmpty(person.PostCode) ? "" : person.PostCode,
                                                person.Country,
                                                person.IsCloseFriend ? 1 : 0);



                SqlConnection conn = new SqlConnection(connectionString);
                SqlCommand    com  = new SqlCommand(sqlQuery, conn);
                conn.Open();
                id = (int)com.ExecuteScalar();
                conn.Close();
            }
            catch (Exception e)
            {
                errorMessage = string.Format("Error! An exception happened: {0}", e.Message);
                return(-1);
            }

            return(id);
        }