c#經典編程實例(ado.net基本操作)

一:連接數據庫之查詢學生個數

查詢學生個數代碼如下:

   private void button1_Click(object sender, EventArgs e)
        {
            string s = "server=.;database=SampleDb;integrated security=true;";
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = s;
            SqlCommand cmd = new SqlCommand();
            string sql = "select  count(*) from  student";
            cmd.CommandText = sql;
            cmd.Connection = conn;
            conn.Open();
            object o = cmd.ExecuteScalar();
            int n = (int)o;
            conn.Close();
            MessageBox.Show("學生共有" + n + "個學生");

        }

功能如下:


舉個例子 :cmd.ExecuteNonQuery() 用於執行 update ,delete 等操作。可以返回更新了多少行,或者刪除了多少行。
 cmd.ExecuteScalar(); 可以用來返回像   
select count(StudentName)from Students where Sex="男"
這樣的語句。返回一個結果值(這裏是男生的人數)。

二:實現一個簡單的登錄頁面

代碼如下:

 private void button4_Click(object sender, EventArgs e)
        {
            string s = "server=.;database=SampleDb;integrated security=true;";
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = s;
            SqlCommand cmd = new SqlCommand();
            string sql = string.Format("select  count(*) from  student where Id='{0}'and name='{1}'",
             textBox1.Text,textBox2.Text);
            cmd.CommandText = sql;
            cmd.Connection = conn;
            conn.Open();
            object o = cmd.ExecuteScalar();
            int n = (int)o;
            conn.Close();
            if(n>0)
            MessageBox.Show("登陸成功");


        }

運行如下:


三:用sqldatareader實現讀取學生功能


代碼如下:

 private void button1_Click(object sender, EventArgs e)
        {
            string s = "server=.;database=SampleDb;integrated security=true;";
            SqlConnection conn = new SqlConnection(s);
            SqlCommand cmd = new SqlCommand();
            string sql = "select  * from  student";
            cmd.CommandText = sql;
            cmd.Connection = conn;
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            string id, name, gender, major;
            int grade, cls;
            object o;
            while (reader.Read())
            {
                o = reader["id"];
                id=(string) o;
                name = (string)reader["name"];
                gender = (string)reader["gender"];
                major = (string)reader["major"];
                grade = (int)reader["grade"];
                cls = (int)reader["class"];
                string temp=string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\n", 
                   id, name, gender, major, grade, cls);
                textBox1.AppendText(temp);
            }
            reader.Close();
            conn.Close();


        }


運行如下:


四:實現條件查詢

用性別和年級

代碼如下:

 private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            string s = "server=.;database=SampleDb;integrated security=true;";
            SqlConnection conn = new SqlConnection(s);
            SqlCommand cmd = new SqlCommand();
            string sql = "select  * from  student ";
            string where = " where 1=1 ";
            if (textBox2.Text != "") 
            where = where + " and grade=" + textBox2.Text;
            if (textBox3.Text  != "")
                where = where + " and gender='" + textBox3.Text + "'";//字符串需加引號
            sql = sql + where;
            cmd.CommandText = sql;
            cmd.Connection = conn;
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            string id, name, gender, major;
            int grade, cls;
            object o;
            while (reader.Read())
            {
                o = reader["id"];
                id = (string)o;
                name = (string)reader["name"];
                gender = (string)reader["gender"];
                major = (string)reader["major"];
                grade = (int)reader["grade"];
                cls = (int)reader["class"];
              
                string temp = string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\n",
                   id, name, gender, major, grade, cls);
                textBox1.AppendText(temp);
            }
            reader.Close();
            conn.Close();


        }

運行如下:


五:用datagirdview實現數據的查詢和更刪改查


代碼如下;

顯示的:

 private void button1_Click(object sender, EventArgs e)
        {
            string s = "server=.;database=SampleDb;integrated security=true;";
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = s;

             ds = new DataSet();
             adapter = new SqlDataAdapter("select * from student",conn);
            adapter.Fill(ds);

            grid.DataSource = ds.Tables[0];

        }
運行圖:

及時保存的;

  private void button2_Click(object sender, EventArgs e)
        {
            SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
            adapter.Update(ds);
            MessageBox.Show("保存成功!");
        }

運行圖:






發佈了146 篇原創文章 · 獲贊 149 · 訪問量 73萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章