ado.net五大對象

來源:互聯網
上載者:User

標籤:localhost   man   get   data   集合   using   password   http   多個   

五大對象分別是:

1、 Connection:與資料來源建立串連。

2、 Command:對資料來源執行SQL命令並返回結果。

3、 DataReader:讀取資料來源的資料,只允許對將資料來源以唯讀、順向的方式查看其中所儲存的資料。其常用於檢索大量資料,DataReader對象還是一種非常節省資源的資料對象。

4、 DataAdapter:對資料來源執行操作並返回結果,在DataSet與資料來源之間建立通訊,將資料來源中的資料寫入DataSet中,或根據DataSet中的資料繫結資料來源。

5、 DataSet:記憶體中的資料庫,是資料表的集合,它可以包含任意多個資料表。

class UserBLL {        public const string constring = "User Id=root;Host=localhost;Database=dbdemo;password=wdf123;charset=‘utf8‘";        //添加資料        public int Add(User entity) {            string sql = "insert into User(ID,UserName)Value(?ID,?UserName)";            using (MySqlConnection conn = new MySqlConnection(constring)) {                conn.Open();                MySqlCommand command = new MySqlCommand(sql,conn);                command.Parameters.AddWithValue("?ID",entity.ID);                command.Parameters.AddWithValue("?UserName", entity.UserName);                return command.ExecuteNonQuery();            }        }        //修改資料        public int Update(User entity) {            string sql = "update User set UserName=?UserName where ID=?ID";            using (MySqlConnection conn = new MySqlConnection(constring)) {                conn.Open();                MySqlCommand command = new MySqlCommand(sql, conn);                command.Parameters.AddWithValue("?ID", entity.ID);                command.Parameters.AddWithValue("?UserName", entity.UserName);                return command.ExecuteNonQuery();            }        }        //刪除資料        public int Delete(int ID) {            string sql = "delete from User where ID=?ID";            using (MySqlConnection conn = new MySqlConnection(constring)) {                conn.Open();                MySqlCommand command = new MySqlCommand(sql, conn);                command.Parameters.AddWithValue("?ID", ID);                return command.ExecuteNonQuery();            }        }        //根據主鍵查詢        public User Get(int ID) {            string sql = "select ID,UserName from User where ID=?ID";            using (MySqlConnection conn = new MySqlConnection(constring)) {                conn.Open();                MySqlCommand command = new MySqlCommand(sql, conn);                command.Parameters.AddWithValue("?ID", ID);                MySqlDataReader reader = command.ExecuteReader();                User user = null;                if (reader.Read()) {                    user = new User();                    user.ID = Convert.ToInt32(reader["ID"]);                    user.UserName = reader["UserName"].ToString();                }                return user;            }        }        //查詢集合        public IList<User> GetList() {            string sql = "select* from User";            using (MySqlConnection conn = new MySqlConnection(constring)) {                conn.Open();                MySqlCommand command = new MySqlCommand(sql, conn);                MySqlDataReader reader = command.ExecuteReader();                IList<User> list = new List<User>();                while(reader.Read()){                    User user = new User();                    user.ID = Convert.ToInt32(reader["ID"]);                    user.UserName = reader["UserName"].ToString();                    list.Add(user);                }                return list;            }        }    }

  

 

ado.net五大對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.