使用枚举做为类的字段时与数据库的交互

   public  enum Sex
    {
       Male = 0,
       Female
    }

   public class Customer
    {
        int? nID;


        string firstName;

        string lastName;

        Sex sex;
       string interest;
       public Customer(int? nID, string firstName, string lastName, Sex sex, string interest)
        {
            this.nID = nID;
            this.firstName = firstName;
            this.lastName = lastName;
            this.sex = sex;
            this.interest = interest;
        }
        public Sex Sex
        {
            get { return sex; }
            set { sex = value; }
        }

        
        public int? NID
        {
            get { return nID; }
            set { nID = value; }
        }
        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }
        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }
        public string Interest
        {
            get { return interest; }
            set { interest = value; }
        }
    }
   public  class CustomerData
    {
     const  string MSSQLConnectionString = "Data Source=.;Initial Catalog=test;User ID=sa;Password=123456";
     const string SqlInsertString = "Insert into tbCustomer (firstName,lastName,sex,interest) values (@firstName,@lastName,@sex,@interest)";
     const string SqlSelectAllStrig = "select *from tbCustomer";
       public void Insert(Customer customer)
       {
           SqlConnection con = new SqlConnection(MSSQLConnectionString);

           con.Open();
           SqlCommand cmd = new SqlCommand(SqlInsertString, con);
              cmd.Parameters.AddWithValue("@firstName",customer.FirstName);
              cmd.Parameters.AddWithValue("@lastName", customer.LastName);
              cmd.Parameters.AddWithValue("@sex", customer.Sex);
              cmd.Parameters.AddWithValue("@interest", customer.Interest);
              cmd.ExecuteNonQuery();
              con.Close();
       }
       public IList<Customer> GetAll()
       {
           SqlConnection con = new SqlConnection(MSSQLConnectionString);
           SqlCommand command = new SqlCommand(SqlSelectAllStrig, con);
           DataSet ds = new DataSet();
           SqlDataAdapter da = new SqlDataAdapter(command);
           da.Fill(ds);
           DataTable dt = new DataTable();
           dt = ds.Tables[0];
           IList<Customer> customers = new List<Customer>();
           foreach (DataRow row in dt.Rows)
           {
               Customer customer = new Customer(Convert.ToInt32(row["nID"]), row["firstName"].ToString(), row["lastName"].ToString(),(Sex)Convert.ToInt32(row["sex"]), row["interest"].ToString());
               customers.Add(customer);
           }
           return customers;

       }
    }

 
    class Program
    {
        static void Main(string[] args)
        {
            //Customer customer = new Customer(null, "viper", "Tang", Sex.Male, "Playing BasketBall");
            CustomerData cd = new CustomerData();
            //cd.Insert(customer);
            foreach (var item in cd.GetAll())
            {
                
                Console.WriteLine(item.NID + " " + item.FirstName + item.LastName + " " + item.Sex + " " + item.Interest);
            }
            Console.ReadKey();
        }
    }

插入枚举值:Sex.Male到数据库时,插入的是值0,初始化Customer时赋0值给枚举字段属性Sex,读出来时就对应上Male.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值