ADO.NET復習總結(6)-斷開式數據操作
阿新 • • 發佈:2017-12-28
div list() ide -a toolstrip pty ogre 提示 names
一、基礎知識
主要類及成員(和數據庫無關的)
(1)類DataSet:數據集,對應著庫,屬性Tables表示所有的表
(2)類DataTable:數據表,對應著表,屬性Rows表示所有的行
(3)類DataRow:行數據,一個行數組,就對應著一個實體對象
-》使用DataAdapter的Fill方法,可以將數據填充到DataSet或DataTable中
二、練習:完成學生表的crud
(1)dataGridView的填充:
using System; using System.Collections.Generic; using System.ComponentModel;View Codeusing System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; using t2_StudentInfo; namespace studentInfo { public partial class Form1 : Form {public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string sql = "select * from studentinfo"; using (SqlConnection conn = new SqlConnection("server=.;database=dbtest;uid=sa;pwd=123")) { SqlDataAdapter sda= new SqlDataAdapter(sql,conn); DataTable table = new DataTable(); sda.Fill(table); dataGridView1.DataSource = table; } } } }
(2)重新封裝SQLhelper
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace t2_StudentInfo { public static partial class SqlHelper { private static string connStr = ConfigurationManager.ConnectionStrings["dbtest"].ConnectionString; public static int ExecuteNonQuery(string sql,params SqlParameter[] ps) { using (SqlConnection conn=new SqlConnection(connStr)) { SqlCommand cmd=new SqlCommand(sql,conn); cmd.Parameters.AddRange(ps); conn.Open(); return cmd.ExecuteNonQuery(); } } public static object ExecuteScalar(string sql, params SqlParameter[] ps) { using (SqlConnection conn=new SqlConnection(connStr)) { SqlCommand cmd=new SqlCommand(sql,conn); cmd.Parameters.AddRange(ps); conn.Open(); return cmd.ExecuteScalar(); } } public static DataTable ExecuteTable(string sql,params SqlParameter[] ps) { using (SqlConnection conn=new SqlConnection(connStr)) { SqlDataAdapter adapter=new SqlDataAdapter(sql,conn); //用於進行select操作,可以通過SelectCommand屬性獲取此操作的SqlCommand對象 adapter.SelectCommand.Parameters.AddRange(ps); DataTable dt=new DataTable(); adapter.Fill(dt); return dt; } } public static SqlDataReader ExecuteReader(string sql,params SqlParameter[] ps) { SqlConnection conn=new SqlConnection(connStr); SqlCommand cmd=new SqlCommand(sql,conn); cmd.Parameters.AddRange(ps); conn.Open(); return cmd.ExecuteReader(CommandBehavior.CloseConnection); } } }View Code
(3)查詢
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; using t2_StudentInfo; namespace studentInfo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //連接查詢 string sql = "select * from StudentInfo inner join ClassInfo on StudentInfo.cid=ClassInfo.cId "; DataTable dt = SqlHelper.ExecuteTable(sql); dataGridView1.AutoGenerateColumns = false; dataGridView1.DataSource = dt; } private void dataGridView1_CellFormatting_1(object sender, DataGridViewCellFormattingEventArgs e) { if (e.ColumnIndex == 2) { if (Convert.ToBoolean(e.Value)) { e.Value = "男"; } else { e.Value = "女"; } } } } }View Code
(4)
A:列表:
B:列表代碼:
private void FormAdd_Load(object sender, EventArgs e) { string sql = "select * from classinfo"; DataTable dt = SqlHelper.ExecuteTable(sql); cdoClassInfo.DisplayMember = "CTitle"; cdoClassInfo.ValueMember = "CId"; cdoClassInfo.DataSource = dt; }View Code
C:添加、
private void button1_Click(object sender, EventArgs e) { string sql =""; if (string.IsNullOrEmpty(label1.Text)) { //添加 sql = "insert into studentinfo(sname,sgender,sbirthday,cid) values(@name,@gender,@birthday,@cid)"; } else { //修改 sql = "update studentinfo set sname=@name,sgender=@gender,sbirthday=@birthday,cid=@cid where sid=" +label1.Text; } SqlParameter[] ps = { new SqlParameter("@name",textBox1.Text), new SqlParameter("@gender",radioButton1.Checked), new SqlParameter("@birthday",dtpBirthday.Value), new SqlParameter("@cid",cboClassInfo.SelectedValue), }; int result=SqlHelper.ExecuteNonQuery(sql, ps); if (result > 0) { FreshForm(); this.Close(); } else { MessageBox.Show("保存失敗"); } }View Code
D:刪除:
private void removeToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("確定要刪除嗎?", "提示", MessageBoxButtons.OKCancel); if (result == DialogResult.Cancel) { return; } int sid = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value); string sql = "update studentinfo set isdelete=1 where sid=" + sid; if (SqlHelper.ExecuteNonQuery(sql) > 0) { LoadList(); } else { MessageBox.Show("刪除失敗"); } }View Code
E:修改:
private void editToolStripMenuItem_Click(object sender, EventArgs e) { int id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value); FormAdd formAdd=new FormAdd(); formAdd.FreshForm += LoadList; ShowStudent += formAdd.ShowInfo; formAdd.Show(); ShowStudent(id);//發布顯示內容的消息 }View Code
-》事件(廣播、消息)的代碼實現:
《1》定義委托
《2》在發布消息的類型中定義事件
《3》在接收消息的類型中為事件添加方法
ADO.NET復習總結(6)-斷開式數據操作