1. 程式人生 > 資訊 >聯手英偉達,網魚網咖計劃年內開出 30 家電競酒店

聯手英偉達,網魚網咖計劃年內開出 30 家電競酒店

dapper的使用

安裝

控制檯中安裝

Install-Package Dapper

方法 :Execute

返回: 受影響的行數

執行: 刪 改 查

引數:

Execute(sql,	//查詢語句
param,          // default = null 引數
transaction,    //default = null 事務
commandTimeout, //default = null 最大連線時間
commandType     //default = null 命令型別?
)

程式碼:

  /// <summary>
        /// Execute插入一條
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            using (IDbConnection conn = SqlConnection.MySqlConnection())
            {
                Person person = new Person("zhangsan","[email protected]","3212151343","男");
                string sqlstr = "INSERT INTO person(name,email,gender,phone) VALUES(@name,@email,@gender,@phone) ";
                int affectedRows = conn.Execute(sqlstr,
                    person,
                    null,
                    null);

                textBox1.Text = affectedRows.ToString();
            }
        }

        /// <summary>
        /// Execute插入多條
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            using (IDbConnection conn = SqlConnection.MySqlConnection())
            {
                List<Person> personList = new List<Person>();
                personList.Add(new Person("zhangsan", "[email protected]", "3212151343", "男"));
                personList.Add(new Person("zhsffa", "[email protected]", "31511511543", "男"));
                string sqlstr = "INSERT INTO person(name,email,gender,phone) VALUES(@name,@email,@gender,@phone) ";
                int affectedRows = conn.Execute(sqlstr,
                    personList,
                    null,
                    null);

                textBox1.Text = affectedRows.ToString();
            }
        }

        /// <summary>
        /// Execute 更新一條
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            using (IDbConnection conn = SqlConnection.MySqlConnection())
            {
                Person person = new Person();
                person.Id = 15;
                person.Gender = "改改看";
                string sqlstr = "UPDATE person SET gender = @gender WHERE id = @id ";
                
                int affectedRows = conn.Execute(sqlstr,
                    person,
                    null,
                    null);

                textBox1.Text = affectedRows.ToString();
            }
        }

        /// <summary>
        /// Execute 更新多條
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            using (IDbConnection conn = SqlConnection.MySqlConnection())
            {
                List<Person> personList = new List<Person>();
                personList.Add(new Person(3,"", "", "3212151343", "^-^"));
                personList.Add(new Person(5,"zhsffa", "[email protected]", "31511511543", "^0^"));
                string sqlstr = "UPDATE person SET gender = @gender WHERE id = @id ";

                int affectedRows = conn.Execute(sqlstr,
                    personList,
                    null,
                    null);

                textBox1.Text = affectedRows.ToString();
            }
        }

        /// <summary>
        /// Execute 刪除一條
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            using (IDbConnection conn = SqlConnection.MySqlConnection())
            {
                string sqlstr = "DELETE FROM person  WHERE id = @id ";
                int affectedRows = conn.Execute(sqlstr,
                    new { id = 9},
                    null,
                    null);

                textBox1.Text = affectedRows.ToString();
            }
        }

方法 :Query

Query QueryFirst QueryFirstOrDefault

返回:對映結果

執行: 匿名 強型別? 一對一 一對多 多型別?

引數:

Query(sql,	//查詢語句
param,          // default = null 引數
transaction,    //default = null 事務
buffered,       //default = null 查詢的緩衝結果?
commandTimeout, //default = null 最大連線時間
commandType     //default = null 命令型別?
)

程式碼

/// <summary>
        /// Query 查詢一條
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_Click(object sender, EventArgs e)
        {
            using (IDbConnection conn = SqlConnection.MySqlConnection())
            {
                string sqlstr = "SELECT * FROM person ";
                Person person = conn.Query<Person>(sqlstr).FirstOrDefault();

                textBox1.Text = person.ToString();
            }
        }

        /// <summary>
        /// 查詢多條
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button8_Click(object sender, EventArgs e)
        {
            using (IDbConnection conn = SqlConnection.MySqlConnection())
            {
                List<Person> personList = new List<Person>();
                string sqlstr = "SELECT * FROM person ";
                personList = conn.Query<Person>(sqlstr).ToList();

                string str = null;
                foreach (Person person in personList)
                {
                    str += person.ToString();
                }
                textBox1.Text = str;
            }
        }

        /// <summary>
        /// Query 一對一查詢
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button9_Click(object sender, EventArgs e)
        {
            using (IDbConnection conn = SqlConnection.MySqlConnection())
            {
                List<Person> personList = new List<Person>();
                string sqlstr = "SELECT p.*,r.role_name FROM person p INNER JOIN role r ON p.role_id = r.role_id";
                var list = conn.Query<Person, Role, Person>(sqlstr,
                    (person, role) => { person.Role = role; return person; },
                    null,
                    null,
                    true,
                    splitOn:"role_id"
                  ).ToList();


                this.dataGridView1.DataSource = list;
            }
        }

        /// <summary>
        /// Query 一對多查詢  
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button10_Click(object sender, EventArgs e)
        {
            // 姑且這麼用,但是很多東西還不明白 比如賦值的過程
            using (IDbConnection conn = SqlConnection.MySqlConnection())
            {
                List<Person> personList = new List<Person>();
                var lookUp = new Dictionary<int, Person>();
                string sqlstr = "SELECT p.* ,r.*,s.* " +
                    "from person_role s " +
                    "left join person p  on p.id = s.person_id " +
                    "left join role r on s.role_id = r.role_id";
                var list = conn.Query<Person, Role, Person>(sqlstr,
                    (person, role) => {
                        Person p;
                        if (!lookUp.TryGetValue(person.Id,out p))
                        {
                            lookUp.Add(person.Id, p = person);
                        }
                        person.RoleList.Add(role);
                       
                        
                        return person; },
                    null,
                    null,
                    true,
                    splitOn:"role_id,id"
                  ).ToList();


                this.dataGridView1.DataSource = list;
            }
        }

事務

 /// <summary>
        /// 事務
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button11_Click(object sender, EventArgs e)
        {
            using (IDbConnection conn = SqlConnection.MySqlConnection())
            {
                IDbTransaction trans = conn.BeginTransaction();
                try
                {
                    
                    Person person1 = new Person("zhangsan", "[email protected]", "3212151343", "男");
                    Person person2 = new Person("asdfban", "[email protected]", "87654", "男");
                    string sqlstr = "INSERT INTO person(name,email,gender,phone) VALUES(@name,@email,@gender,@phone) ";
                    
                    int affectedRows1 = conn.Execute(sqlstr,
                        person1,
                        trans,
                        null);

                    int affectedRows2 = conn.Execute(sqlstr,
                        person2,
                        trans,
                        null);
                    trans.Commit();

                    textBox1.Text = affectedRows1.ToString() + "---"+ affectedRows2.ToString();
                }
                catch (Exception)
                {

                    trans.Rollback();
                }
                
            }
        }

ps:直接執行可能會有些錯誤,僅提供思路 Gitee