1用於登錄的sql語句:
string sql = string.Format
("select count(*) from student where id='{0}'and name='{1}'", textBox1.Text, textBox2.Text);
int n = (int)cmd.ExecuteScalar();
2.datagrideview顯示數據
string s = "server=.;database=SampleDb;integrated security=true;";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = s;
SqlCommand cmd = new SqlCommand();
string sql = string.Format("select * from student");
DataSet ds=new DataSet ();
SqlDataAdapter adapter=new SqlDataAdapter (sql,conn);
adapter.Fill(ds);
grid.DataSource=ds.Tables[0];
3.添加學生
string gender = radioButton1.Checked ? "男" : "女";
string sql = string.Format("insert into student values('{0}','{1}','{2}','{3}',{4},{5})",
textBox1.Text, textBox2.Text, gender,textBox3.Text, textBox4.Text, textBox5.Text);
4.刪除:
string s1 = grid.CurrentRow.Cells[0].Value.ToString();
string s = "server=.;database=SampleDb;integrated security=true;";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = s;
SqlCommand cmd = new SqlCommand();
string sql = string.Format("delete from student where id='{0}'",s1);
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
adapter.Fill(ds);
grid.DataSource = ds.Tables[0];
或 string sql = string.Format("delete from Course where Cno='{0}'", textBox1.Text);
5查詢:
string sql = string.Format("select * from student where grade={0} and major like '%{1}%' and gender like '%{2}%'", textBox1.Text, textBox2.Text, textBox3.Text);
grid.DataSource = "";
string s = "server=.;database=SampleDb;integrated security=true;";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = s;
string sql = string.Format("select * from student where grade={0} and major like '%{1}%' and gender like '%{2}%'", textBox1.Text, textBox2.Text, textBox3.Text);
DataSet ds = new DataSet();
SqlDataAdapter adaper = new SqlDataAdapter(sql, conn);
adaper.Fill(ds);
6.查詢學生個數
string sql = "select count(*) from student";
string sql = "select count(*) from student where gender='男'";
int n =cmd.ExecuteScalar(sql);
MessageBox.Show("共有" + n + "個男生");
grid.DataSource = ds.Tables[0];
7.編輯
(1)string id = grid.CurrentRow.Cells[0].Value.ToString();
EditStudentForm f = new EditStudentForm();
f.id = id;
DialogResult r = f.ShowDialog();
if (r == DialogResult.OK)
refereshData();
(2)
public string id; //學號,接收從列表窗體傳遞來的參數
private void button1_Click(object sender, EventArgs e)
{
string g = r1.Checked ? "男" : "女";
string sql = string.Format("update student set name='{0}',gender='{1}',major='{2}',grade={3},class={4} where id='{5}'",
textBox2.Text,g, textBox4.Text, textBox5.Text, textBox6.Text, textBox1.Text);
DbHelper.myExecute(sql);
this.DialogResult = DialogResult.OK;
this.Close();
}
private void EditStudentForm_Load(object sender, EventArgs e)
{
readStudent();
}
private void readStudent()
{
SqlConnection conn = DbHelper.createConnection();
SqlCommand command = new SqlCommand();
string sql = string.Format("select * from student where id ='{0}'", id);
command.CommandText = sql;
command.Connection = conn;
conn.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
textBox1.Text = reader["id"].ToString();
textBox2.Text = (string)reader["name"];
textBox4.Text = (string)reader["major"];
if (reader["gender"].ToString().Trim() == "男")
r1.Checked = true;
else
r2.Checked = true;
textBox5.Text = reader["grade"].ToString();
textBox6.Text = reader["class"].ToString();
}
reader.Close();
conn.Close();
}
ado.net考前複習
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.