本例子通過8個按鈕實現了通過sqlcommand 和dataset來分別實現增、刪、改、查的功能,button1~button8 對應相應的代碼。尤其是dataset刪除時,表格必須要有主鍵,否則刪除不能同步到數據庫,
姓名對應的textbox2,生日對應的dateTimePicker1,電話對應的是textbox3.
//查詢數據庫
private void button1_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
string conString = @"Data Source=JSJ123-D0BB945A\SQLEXPRESS;Initial Catalog=ContactsDB;Integrated Security=True;Pooling=False";
SqlConnection con = new SqlConnection(conString);
con.Open();
//MessageBox.Show(con.State.ToString());
string sql = "select * from Contacts";
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = sql;
SqlDataReader sdr=com.ExecuteReader();
//textBox1.AppendText("序號"+"\t"+"姓名"+"\t"+"生日"+"\t\t"+"電話號碼");
//textBox1.AppendText("\r\n");
while(sdr.Read())
{
//textBox1.AppendText(sdr[0].ToString() + "\t" + sdr[1].ToString() + "\t" + sdr.GetDateTime(2).ToShortDateString() + "\t" + sdr[3].ToString());
//textBox1.AppendText ( "\r\n");
ListViewItem lvi = new ListViewItem(sdr[1].ToString());
lvi.SubItems.Add(sdr.GetDateTime(2).ToShortDateString());
lvi.SubItems.Add(sdr[3].ToString());
listView1.Items.Add(lvi);
}
con.Close();
}
{
listView1.Items.Clear();
string conString = @"Data Source=JSJ123-D0BB945A\SQLEXPRESS;Initial Catalog=ContactsDB;Integrated Security=True;Pooling=False";
SqlConnection con = new SqlConnection(conString);
con.Open();
//MessageBox.Show(con.State.ToString());
string sql = "select * from Contacts";
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = sql;
SqlDataReader sdr=com.ExecuteReader();
//textBox1.AppendText("序號"+"\t"+"姓名"+"\t"+"生日"+"\t\t"+"電話號碼");
//textBox1.AppendText("\r\n");
while(sdr.Read())
{
//textBox1.AppendText(sdr[0].ToString() + "\t" + sdr[1].ToString() + "\t" + sdr.GetDateTime(2).ToShortDateString() + "\t" + sdr[3].ToString());
//textBox1.AppendText ( "\r\n");
ListViewItem lvi = new ListViewItem(sdr[1].ToString());
lvi.SubItems.Add(sdr.GetDateTime(2).ToShortDateString());
lvi.SubItems.Add(sdr[3].ToString());
listView1.Items.Add(lvi);
}
con.Close();
}
//增加一條數據
private void button2_Click(object sender, EventArgs e)
{
string conString = @"Data Source=JSJ123-D0BB945A\SQLEXPRESS;Initial Catalog=ContactsDB;Integrated Security=True;Pooling=False";
SqlConnection con = new SqlConnection(conString);
con.Open();
string sql = "insert into contacts(Name,DayOfBirth,Phone) values('";
sql += textBox2.Text+"','";
sql += Convert.ToDateTime(dateTimePicker1.Text) + "','";
sql += textBox3.Text + "')";
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = sql;
int n=com.ExecuteNonQuery();
if (n == 1)
MessageBox.Show("添加成功");
con.Close();
}
private void button2_Click(object sender, EventArgs e)
{
string conString = @"Data Source=JSJ123-D0BB945A\SQLEXPRESS;Initial Catalog=ContactsDB;Integrated Security=True;Pooling=False";
SqlConnection con = new SqlConnection(conString);
con.Open();
string sql = "insert into contacts(Name,DayOfBirth,Phone) values('";
sql += textBox2.Text+"','";
sql += Convert.ToDateTime(dateTimePicker1.Text) + "','";
sql += textBox3.Text + "')";
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = sql;
int n=com.ExecuteNonQuery();
if (n == 1)
MessageBox.Show("添加成功");
con.Close();
}
//刪除一條數據
private void button3_Click(object sender, EventArgs e)
{
string conString = @"Data Source=JSJ123-D0BB945A\SQLEXPRESS;Initial Catalog=ContactsDB;Integrated Security=True;Pooling=False";
SqlConnection con = new SqlConnection(conString);
con.Open();
string sql = "delete from contacts where Name='";
sql += listView1.SelectedItems[0].Text;
sql += "'";
MessageBox.Show(sql);
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = sql;
int n = com.ExecuteNonQuery();
if (n == 1)
MessageBox.Show("刪除成功");
con.Close();
}
//修改一條數據
private void button4_Click(object sender, EventArgs e)
{
string conString = @"Data Source=JSJ123-D0BB945A\SQLEXPRESS;Initial Catalog=ContactsDB;Integrated Security=True;Pooling=False";
SqlConnection con = new SqlConnection(conString);
con.Open();
string sql = "update contacts set DayOfBirth='";
sql +=Convert.ToDateTime( dateTimePicker1.Text);
sql += "' , Phone='";
sql += textBox3.Text + "' where Name='";
sql += textBox2.Text + "'";
MessageBox.Show(sql);
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = sql;
int n = com.ExecuteNonQuery();
if (n == 1)
MessageBox.Show("更新成功");
con.Close();
}
private void button3_Click(object sender, EventArgs e)
{
string conString = @"Data Source=JSJ123-D0BB945A\SQLEXPRESS;Initial Catalog=ContactsDB;Integrated Security=True;Pooling=False";
SqlConnection con = new SqlConnection(conString);
con.Open();
string sql = "delete from contacts where Name='";
sql += listView1.SelectedItems[0].Text;
sql += "'";
MessageBox.Show(sql);
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = sql;
int n = com.ExecuteNonQuery();
if (n == 1)
MessageBox.Show("刪除成功");
con.Close();
}
//修改一條數據
private void button4_Click(object sender, EventArgs e)
{
string conString = @"Data Source=JSJ123-D0BB945A\SQLEXPRESS;Initial Catalog=ContactsDB;Integrated Security=True;Pooling=False";
SqlConnection con = new SqlConnection(conString);
con.Open();
string sql = "update contacts set DayOfBirth='";
sql +=Convert.ToDateTime( dateTimePicker1.Text);
sql += "' , Phone='";
sql += textBox3.Text + "' where Name='";
sql += textBox2.Text + "'";
MessageBox.Show(sql);
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = sql;
int n = com.ExecuteNonQuery();
if (n == 1)
MessageBox.Show("更新成功");
con.Close();
}
//數據查詢
private void button5_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
string conString = @"Data Source=JSJ123-D0BB945A\SQLEXPRESS;Initial Catalog=ContactsDB;Integrated Security=True;Pooling=False";
SqlConnection con=new SqlConnection(conString);
string sql = "select * from Contacts";
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
sda.Fill(ds, "Contacts");//獲取離線是數據不用打開數據庫,見p268頁
foreach (DataRow dr in ds.Tables["Contacts"].Rows)
{
ListViewItem lvi = new ListViewItem(dr["Name"].ToString());
lvi.SubItems.Add(dr["DayOfBirth"].ToString());
lvi.SubItems.Add(dr["Phone"].ToString());
listView1.Items.Add(lvi);
}
}
private void button5_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
string conString = @"Data Source=JSJ123-D0BB945A\SQLEXPRESS;Initial Catalog=ContactsDB;Integrated Security=True;Pooling=False";
SqlConnection con=new SqlConnection(conString);
string sql = "select * from Contacts";
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
sda.Fill(ds, "Contacts");//獲取離線是數據不用打開數據庫,見p268頁
foreach (DataRow dr in ds.Tables["Contacts"].Rows)
{
ListViewItem lvi = new ListViewItem(dr["Name"].ToString());
lvi.SubItems.Add(dr["DayOfBirth"].ToString());
lvi.SubItems.Add(dr["Phone"].ToString());
listView1.Items.Add(lvi);
}
}
//dataset增加一行數據
private void button6_Click(object sender, EventArgs e)
{
string conString = @"Data Source=JSJ123-D0BB945A\SQLEXPRESS;Initial Catalog=ContactsDB;Integrated Security=True;Pooling=False";
SqlConnection con = new SqlConnection(conString);
string sql = "select * from Contacts";
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
sda.Fill(ds, "Contacts");//獲取離線是數據不用打開數據庫,保持數據庫狀態,原來是打開的還是打開,原來是關閉的,讀出數據後,還關閉。
DataRow dr = ds.Tables["Contacts"].NewRow();
dr["Name"] = textBox2.Text;
dr["DayOfBirth"] = dateTimePicker1.Text;
dr["Phone"] = textBox3.Text;
ds.Tables["Contacts"].Rows.Add(dr);
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Update(ds, "Contacts");
MessageBox.Show("添加成功!");
}
private void button6_Click(object sender, EventArgs e)
{
string conString = @"Data Source=JSJ123-D0BB945A\SQLEXPRESS;Initial Catalog=ContactsDB;Integrated Security=True;Pooling=False";
SqlConnection con = new SqlConnection(conString);
string sql = "select * from Contacts";
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
sda.Fill(ds, "Contacts");//獲取離線是數據不用打開數據庫,保持數據庫狀態,原來是打開的還是打開,原來是關閉的,讀出數據後,還關閉。
DataRow dr = ds.Tables["Contacts"].NewRow();
dr["Name"] = textBox2.Text;
dr["DayOfBirth"] = dateTimePicker1.Text;
dr["Phone"] = textBox3.Text;
ds.Tables["Contacts"].Rows.Add(dr);
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Update(ds, "Contacts");
MessageBox.Show("添加成功!");
}
//dataset 刪除一條數據
private void button7_Click(object sender, EventArgs e)
{
string conString = @"Data Source=JSJ123-D0BB945A\SQLEXPRESS;Initial Catalog=ContactsDB;Integrated Security=True;Pooling=False";
SqlConnection con = new SqlConnection(conString);
string sql = "select * from Contacts";
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
sda.Fill(ds, "Contacts");
int index=-1;
foreach (DataRow dr in ds.Tables["Contacts"].Rows)
{
if (dr["Name"].ToString() == listView1.SelectedItems[0].SubItems[0].Text)
index=ds.Tables["Contacts"].Rows.IndexOf(dr);
}
ds.Tables["Contacts"].Rows[index].Delete();
//要想刪除有效,必須設置主鍵
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Update(ds.Tables["Contacts"]);
MessageBox.Show("刪除成功!");
}
private void button7_Click(object sender, EventArgs e)
{
string conString = @"Data Source=JSJ123-D0BB945A\SQLEXPRESS;Initial Catalog=ContactsDB;Integrated Security=True;Pooling=False";
SqlConnection con = new SqlConnection(conString);
string sql = "select * from Contacts";
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
sda.Fill(ds, "Contacts");
int index=-1;
foreach (DataRow dr in ds.Tables["Contacts"].Rows)
{
if (dr["Name"].ToString() == listView1.SelectedItems[0].SubItems[0].Text)
index=ds.Tables["Contacts"].Rows.IndexOf(dr);
}
ds.Tables["Contacts"].Rows[index].Delete();
//要想刪除有效,必須設置主鍵
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Update(ds.Tables["Contacts"]);
MessageBox.Show("刪除成功!");
}
//dataset修改一條數據。
private void button8_Click(object sender, EventArgs e)
{
string conString = @"Data Source=JSJ123-D0BB945A\SQLEXPRESS;Initial Catalog=ContactsDB;Integrated Security=True;Pooling=False";
SqlConnection con = new SqlConnection(conString);
string sql = "select * from Contacts";
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
sda.Fill(ds, "Contacts");//獲取離線是數據不用打開數據庫,見p268頁
foreach (DataRow dr in ds.Tables["Contacts"].Rows)
{
if (dr["Name"].ToString() == listView1.SelectedItems[0].SubItems[0].Text)
{
dr["DayOfBirth"] = dateTimePicker1.Text;
dr["Phone"] = textBox3.Text;
}
}
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Update(ds.Tables["Contacts"]);
MessageBox.Show("更新成功!");
}
private void button8_Click(object sender, EventArgs e)
{
string conString = @"Data Source=JSJ123-D0BB945A\SQLEXPRESS;Initial Catalog=ContactsDB;Integrated Security=True;Pooling=False";
SqlConnection con = new SqlConnection(conString);
string sql = "select * from Contacts";
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
sda.Fill(ds, "Contacts");//獲取離線是數據不用打開數據庫,見p268頁
foreach (DataRow dr in ds.Tables["Contacts"].Rows)
{
if (dr["Name"].ToString() == listView1.SelectedItems[0].SubItems[0].Text)
{
dr["DayOfBirth"] = dateTimePicker1.Text;
dr["Phone"] = textBox3.Text;
}
}
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Update(ds.Tables["Contacts"]);
MessageBox.Show("更新成功!");
}
//這個是讓listview1和上面的姓名等信息綁定。
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
textBox2.Text = listView1.SelectedItems[0].SubItems[0].Text;
dateTimePicker1.Text = listView1.SelectedItems[0].SubItems[1].Text;
textBox3.Text = listView1.SelectedItems[0].SubItems[2].Text;
}
catch
{
}
}