SQL Server中使用表類型參數(shù)批量添加和修改的存儲過程
摘要:
最近做項目中遇到批量添加和修改的問題,在老大的指導下學會了使用表類型參數(shù)來做批量操作。為了鞏固強化,圍繞這個技術(shù)又做了個小demo。
開始正題:???
首先,我們在SQL Server 2008下創(chuàng)建一個示例數(shù)據(jù)庫名為TableTypeTest,再在該數(shù)據(jù)庫下創(chuàng)建一個名為Class和Student的表,結(jié)構(gòu)如下:
??????????
再在TableTypeTest數(shù)據(jù)庫下創(chuàng)建一個自定義表類型,取名StudentType,如下:
CREATE?TYPE?[dbo].[StudentType]?AS?TABLE( ????[SID]?[int]?NOT?NULL, ????[CID]?[int]?NOT?NULL, ????[SName]?[nvarchar](50)?NOT?NULL ) GO
然后,創(chuàng)建兩個存儲過程,批量添加和批量修改,分別為InserNewStudent和UpdateStudent,如下
InserNewStudent:
CREATE?PROCEDURE?[dbo].[InserNewStudent]? ????@Dt?dbo.StudentType?readonly AS BEGIN ????insert?into?dbo.Student(CID,SName)?select?t.CID,t.SName??from?@Dt?as?t? END GO
UpdateStudent:
啟動Visual Studio?2010,創(chuàng)建一個默認的窗體應用程序,窗體用于顯示所有班級列表,操作每個班級下的學生通過選中該班級,然后右鍵操作。如下:
創(chuàng)建顯示班級下的所有學生列表窗體,如下:
創(chuàng)建批量添加學生的窗體,如下:
關(guān)鍵代碼如下:
//////?批量添加 ?????????///public?static?bool?AddBantch(DataTable?dt)?{ ?????????????string?pName?=?"dbo.InserNewStudent"; ?????????????ListpList?=?new?List()?{ ?????????????DbHelper.CreateSqlParemeterStructured("@Dt",dt) ?????????????}; ?????????????try ?????????????{ ?????????????????DbHelper.RunProcedure(pName,?pList); ?????????????????return?true; ?????????????} ?????????????catch ?????????????{ ?????????????????return?false; ?????????????} ?????????}
private?void?button1_Click(object?sender,?EventArgs?e) ????????{ ????????????if?(string.IsNullOrEmpty(this.textBox1.Text)?||?string.IsNullOrEmpty(this.textBox4.Text)?||?string.IsNullOrEmpty(this.textBox6.Text)) ????????????{ ????????????????MessageBox.Show("不能有空文本框"); ????????????} ????????????else?{ ????????????????DataTable?dt?=?new?DataTable(); ????????????????dt.Columns.Add("SID"); ????????????????dt.Columns.Add("CID"); ????????????????dt.Columns.Add("SName"); ????????????????DataRow?dr; ????????????????dr?=?dt.NewRow(); ????????????????dr["SID"]?=?0;//此列雖然在添加的時候無用,但必須賦值,否則報錯 ????????????????dr["CID"]?=?CID; ????????????????dr["SName"]?=?this.textBox1.Text; ????????????????dt.Rows.Add(dr); ????????????????dr?=?dt.NewRow(); ????????????????dr["SID"]?=?0; ????????????????dr["CID"]?=?CID; ????????????????dr["SName"]?=?this.textBox4.Text; ????????????????dt.Rows.Add(dr); ????????????????dr?=?dt.NewRow(); ????????????????dr["SID"]?=?0; ????????????????dr["CID"]?=?CID; ????????????????dr["SName"]?=?this.textBox6.Text; ????????????????dt.Rows.Add(dr); ????????????????if?(Library.AddBantch(dt)) ????????????????{ ????????????????????MessageBox.Show("批量添加成功!"); ????????????????????this.Close(); ????????????????} ????????????????else ????????????????{ ????????????????????MessageBox.Show("批量添加失?。?); ????????????????????this.Close(); ????????????????} ????????????} ????????}
?創(chuàng)建批量更改班級學生姓名的窗體,如下:
關(guān)鍵代碼如下:
//////?批量修改 ?????????///public?static?bool?UpdateBantch(DataTable?dt)?{ ?????????????string?pName?=?"dbo.UpdateStudent"; ?????????????ListpList?=?new?List()?{? ?????????????DbHelper.CreateSqlParemeterStructured("@Dt",dt) ?????????????}; ?????????????try ?????????????{ ?????????????????DbHelper.RunProcedure(pName,?pList); ?????????????????return?true; ?????????????} ?????????????catch? ?????????????{ ?????????????????return?false; ?????????????} ?????????}
private?void?button1_Click(object?sender,?EventArgs?e) ????????{ ????????????int?rows?=?this.dataGridView1.Rows.Count; ????????????DataTable?dt?=?new?DataTable(); ????????????dt.Columns.Add("SID"); ????????????dt.Columns.Add("CID"); ????????????dt.Columns.Add("SName"); ????????????DataRow?dr; ????????????for?(int?i?=?0;?i?<?rows;i++?)?{ ????????????DataGridViewRow?dtr=this.dataGridView1.Rows[i]; ????????????dr?=?dt.NewRow(); ????????????dr["SID"]?=?dtr.Cells[0].Value; ????????????dr["CID"]?=?dtr.Cells[1].Value; ????????????dr["SName"]?=?dtr.Cells[2].Value; ????????????dt.Rows.Add(dr); ????????????} ????????????if?(Library.UpdateBantch(dt)) ????????????{ ????????????????MessageBox.Show("批量更改成功!"); ????????????} ????????????else?{ ????????????????MessageBox.Show("批量更改失?。?); ????????????} ????????}
源碼下載
PS:數(shù)據(jù)庫連接要在程序中的App.Config中配置