c#經典程式設計例項(ado.net基本操作)
阿新 • • 發佈:2019-02-14
一:連線資料庫之查詢學生個數
查詢學生個數程式碼如下:
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("儲存成功!");
}
執行圖: