public List<turtype> GetAllTurTypes() { SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand("SELECT id, name, comment, pathfoto FROM turtype", con); cmd.CommandType = CommandType.Text; // Создать коллекцию для всех записей List<turtype> list = new List<turtype>(); try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { turtype emp = new turtype( (int)reader["id"], (string)reader["name"], (string)reader["comment"], (string)reader["pathfoto"]); list.Add(emp); } reader.Close(); return list; } catch { throw new ApplicationException("Ошибка данныx."); } finally { con.Close(); } }
public int AddTurType(turtype emp1) { SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand("Insert into turtype(name,comment,pathfoto) values (@name,@comment,@pathfoto) SET @id = @@IDENTITY ", con); cmd.CommandType = CommandType.Text; cmd.Parameters.Add(new SqlParameter("@name", SqlDbType.NVarChar, 150)); cmd.Parameters["@name"].Value = emp1.Name; cmd.Parameters.Add(new SqlParameter("@comment", SqlDbType.NVarChar, 200)); cmd.Parameters["@comment"].Value = emp1.Comment; cmd.Parameters.Add(new SqlParameter("@pathfoto", SqlDbType.NVarChar, 200)); cmd.Parameters["@pathfoto"].Value = emp1.Pathfoto; cmd.Parameters.Add(new SqlParameter("@id", SqlDbType.Int, 6)); cmd.Parameters["@id"].Direction = ParameterDirection.Output; try { con.Open(); cmd.ExecuteNonQuery(); return (int)cmd.Parameters["@id"].Value; } catch { return 0; throw new ApplicationException("Ошибка данныx. добавление типа тура"); } finally { con.Close(); } }