qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          ADO連接數據庫的增刪改查方法

           1:添加命名空間System.Data.SqlClient中的SQL Server訪問類;
            2:與SQL Server數據庫建立連接,ADO.NET提供Connection對象用于建立與SQL Server數據庫的連接
            string connectionStr = "Data source=服務器名;Initial Catalog=數據庫名稱; uid=用戶名;pwd=密碼()";        //  定義連接字符串
            // Integrated Security=True    集成身份驗證
            //uid=xxx;Pwd=xxx    用戶名密碼登陸
            SqlConnection connection1 = new SqlConnection(connectionStr);           ///實例化Connection對象用于連接數據源
            connection1.Open();                                                                          ///打開數據庫連接
            ……………
            connection1.Close();                                                                         ///關閉數據庫連接
            3:與SQL Server數據庫建立連接后,使用命令對象SqlCommand類直接對數據庫進行操作
            (1)增加、刪除、修改操作
            SqlConnection connection1 = new SqlConnection(connectionStr);            //建立連接
            connection1.Open();                                                                            //打開數據庫連接
            string sqlStr = "(SQL執行語句,例如  insert into A  values('abc',1))";       //定義相關的執行語句,相當于寫好命令
            SqlCommand command1 = new SqlCommand(sqlStr, connection1);          //構造函數指定命令對象所使用的連接對象connection1以及命令文本sqlStr ,相當于讓系統接受命令。
            command1.ExecuteNonQuery();                                                             //ExecuteNonQuery()方法返回值為一整數,代表操作所影響到的行數,注意ExecuteNonQuery()方法一般用于執行                                                                                                                           // UPDATE、INSERT、DELETE等非查詢語句,可以理解為讓系統執行命令
            connection1.Close();     ///關閉數據庫連接
            示例1:刪除的Course表中課程編號為003的記錄:
          string connectionStr = "Data source=.;Initial Catalog=Student; Integrated Security=True";
          SqlConnection connections = new SqlConnection(connectionStr);
          string sqlstr = "delete from Course where Cno='006' ";
          SqlCommand command1 = new SqlCommand(sqlstr, connectionss);
          conn.Open();
          if (command1.ExecuteNonQuery() > 0)
          {
          MessageBox.Show("刪除課程成功!");
          };
          connections .Close();
            示例2:向Course表中增加一門課程,課程信息由前臺輸入
          string connectionStr = "Data source=.;Initial Catalog=Student;Integrated Security=True";
          SqlConnection connection = new SqlConnection(connectionStr);
          int Credit = Convert.ToInt32(txtCredit.Text); /TextBox.text是string類型,需要用到強制轉換方法“Convert.ToInt32”將string類型轉化為int類型
          string sqlStr = "insert into Course values('" + txtCno.Text + "','" + txtCname.Text + "'," + Credit + ")";//因為字符串的組成部分為需要從前臺讀取的變量,所以在這里需要用到字符串拼接,
          //拼接字符:‘ “+字符串變量+” ’,拼接數字:“+數字變量+”
          SqlCommand command1= new SqlCommand(sqlStr, connection);
          connection.Open();
          if (command1.ExecuteNonQuery() > 0)
          {
          MessageBox.Show("課程添加成功!");
          };
          connection.Close();
          示例3:把課程“線性代數”的學分修改為5分
          string connectionStr = "Data source=.;Initial Catalog=Student; Integrated Security=True";
          SqlConnection connection = new SqlConnection(connectionStr);
          string sqlStr = "update Course set Ccredit=5 where Cname='線性代數'";
          SqlCommand command1= new SqlCommand(sqlStr, connection);
          connection .Open();
          if (command1.ExecuteNonQuery() > 0)
          {
          MessageBox.Show("學分修改成功!");
          };
          connection .Close();
            (2)查詢數據庫,用ExecuteScalar()方法,返回單個值(Object)(查詢結果第一行第一列的值)
            示例4:從Student表中查詢學號為201244111學生的姓名:
          string connectionStr = "Data source=.;Initial Catalog=Student;
          Integrated Security=True";
          SqlConnection connection = new SqlConnection(connectionStr);
          string sqlstr = "select Sname from student where Sno='201244111' ";
          SqlCommand command1 = new SqlCommand(sqlstr, connection);
          connection.Open();
          string studentName = command1.ExecuteScalar().ToString();
          MessageBox.Show(studentName);
          connection.Close();
            使用DataReader讀取多行數據,逐行讀取,每次讀一行
            示例5:運用DataReader逐行讀出student表中的第一列數據
          string connectionStr = "Data source=.;Initial Catalog=Student;
          Integrated Security=True"; SqlConnection connection = new SqlConnection(connectionStr);
          string sqlstr = "select *from student";
          SqlCommand command1 = new SqlCommand(sqlstr, connection);
          connection.Open();
          SqlDataReader dataReader1 = command1.ExecuteReader();
          // DataReader類沒有構造函數,不能實例化,需要通過調用Command對象的command1的ExecuteReader()方法
          while (dataReader1.Read())  ///DataReader的Read()方法用于讀取數據,每執行一次該語句,DataReader就向前讀取一行數據;如果遇到末尾,就返回False,否則為True
          {
          MessageBox.Show(dataReader1[0].ToString());
          }
          connection.Close();
            4.使用SqlDataAdapter數據適配器類訪問數據庫 ,注意:它既可以將數據庫中數據傳給數據集中的表,又可將數據集中的表傳到數據庫中。簡言之,數據適配器類用于數據源與數據集間交換數據
            (鏈接語句略)
            connection1.Open();      ///打開數據庫連接
            string sqlStr = "SELECT * FROM A";                                                                ///從A表中選擇所有數據的SQL語句
            SqlDataAdapter dataAdapter1 = new dataAdapter(sqlStr, connection1);            ///構造名為dataAdapter1的數據適配器對象,            并指定連接對象connection1以及SELECT語句
            DataSet dataSet1 = new DataSet();                                                            ///構造名為dataSet1的數據集對象 dataAdapter1.Fill(dataSet1);
            ………………………………
            ///使用SqlDataAdapter類中的Fill()方法將數據填充到數據集中,注意:SqlDataAdapter類中的Fill()方法和Update()方法可用于將數據填充到單個數據表或數據集中
            connection1.Close();
            示例6:將Student表中的數據全部查詢出來
          string connectionStr = "Data source=.;Initial Catalog=Student;
          Integrated Security=True";
          SqlConnection connection = new SqlConnection(connectionStr);
          string sqlstr = "select *from student";
          connection.Open();
          SqlDataAdapter dataAdapter1 = new SqlDataAdapter(sqlstr, connection);
          DataSet dataSet1 = new DataSet();
          dataAdapter1.Fill(dataSet1); ///使用SqlDataAdapter類中的Fill()方法將數據填充到數據集中,相當于程序的臨時數據庫
          DataTable dt1 = dataSet1.Tables[0];
          ///獲取數據集的第一張表
          this.dataGridView1.DataSource = dt1;
          connection.Close();

          posted on 2014-09-29 10:08 順其自然EVO 閱讀(204) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

          <2014年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 社旗县| 滦平县| 会宁县| 疏勒县| 兴化市| 辛集市| 沙田区| 河曲县| 北碚区| 黄龙县| 长岭县| 宣武区| 扎兰屯市| 濮阳市| 阿合奇县| 石屏县| 东源县| 洛隆县| 黄平县| 新乡县| 苏州市| 承德市| 新营市| 大埔县| 大冶市| 保德县| 崇州市| 承德市| 新丰县| 珲春市| 长阳| 宜城市| 黄大仙区| 安庆市| 车险| 额尔古纳市| 贺兰县| 丰城市| 资中县| 镇远县| 普格县|