ImageVerifierCode 换一换
格式:DOCX , 页数:15 ,大小:19.05KB ,
资源ID:27264739      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/27264739.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C# 读取新增修改删除 数据库资料方法.docx)为本站会员(b****3)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C# 读取新增修改删除 数据库资料方法.docx

1、C# 读取新增修改删除 数据库资料方法/ / 讀取SQL Server數據,使用DataSet / private void DataSerConnection() try / 1、 /* * 使用SqlConnection?接?据?之前,首先需要建立?据?接。 * ?了建立?据?接,需要?建SqlConnection?例,并通?SqlConnection?构造器或SqlConnection?例的?性初始化?据?接字符串。 * SqlConnection 與 SqlDataAdapter 或 SqlCommand一起使用 */ /SqlConnection 表示與SQL Server ?据?的

2、一?打?的?接。作用:控制到SQL Server的連接 /(如果有可用的開啟連接,則 SqlConnection 會從連接集區取出開啟的連接。否則,它會建立與 SQL Server 之執行個體的新連接。) SqlConnection dataConnection = new SqlConnection(); /ConnectionString 取得或設定用來開啟 SQL Server 資料庫的字串。 dataConnection.ConnectionString = Data Source=CCM02SQLEXPRESS;Initial Catalog=Northwind;Persist Se

3、curity Info=True;User ID=sa;Password=sh2_123; /Open 開啟資料庫連接。 dataConnection.Open(); /SqlDataAdapter 表示一組資料命令集和資料庫連接 /* * 初始化 SqlDataAdapter 類別的新執行個體 * 名稱 說明 SqlDataAdapter () 初始化 SqlDataAdapter 類別的新執行個體。 SqlDataAdapter (SqlCommand) 使用指定 SqlCommand 做為 SelectCommand 屬性,初始化 SqlDataAdapter 類別的新執行個體。 Sql

4、DataAdapter (String, SqlConnection) 使用 SelectCommand 和 SqlConnection 物件,初始化 SqlDataAdapter 類別的新執行個體。 SqlDataAdapter (String, String) 使用 SelectCommand 和連接字串,初始化 SqlDataAdapter 類別的新執行個體。 */ / 2、 SqlDataAdapter suppliersTableAdapter = new SqlDataAdapter(select * from products, dataConnection); /* * 成員名

5、稱 說明 DataReader 從基礎資料儲存區中擷取資料做為 IDataReader DataSet 從基礎資料儲存區中擷取資料至 DataSet 結構中。 */ / 3、 DataSet dt = new DataSet(); dt.DataSetName = Northwind; /* * 名稱 說明 DataAdapter.FillSchema (DataSet, SchemaType) 將 DataTable 加入至指定的 DataSet,並且根據指定的 SchemaType 設定結構描述,以符合資料來源中的資料表。 DataAdapter.FillSchema (DataTable

6、, SchemaType, IDataReader) 將 DataTable 加入指定的 DataSet。 DataAdapter.FillSchema (DataSet, SchemaType, String, IDataReader) 將 DataTable 加入指定的 DataSet。 * * SchemaType :指定如何在執行 FillSchema 作業時處理現有的結構描述 (Schema) 對應。 * 成員名稱 說明 Mapped 套用所有的現有資料表對應至內送的結構描述 (Schema)。使用變形的結構描述來設定 DataSet。 Source 忽略在 DataAdapter

7、上的任何資料表對應。使用內送的結構描述來設定 DataSet,而不套用任何的變形。 */ / 4、 suppliersTableAdapter.Fill(dt, products); /* * BindingSource * 封裝表單的資料來源。 * 名稱 說明 BindingSource () 初始化 BindingSource 類別的新執行個體成為預設屬性值。 BindingSource (IContainer) 初始化 BindingSource 類別的新執行個體,並將 BindingSource 加入指定的容器中。 BindingSource (Object, String) 使用指定

8、的資料來源和資料成員,初始化 BindingSource 類別的新執行個體。 */ / 5、 BindingSource bd = new BindingSource(dt, products); /在界面上顯示資料 /dgvData: DataGridView控件 /取得或設定 DataGridView 顯示資料的資料來源。 dgvData.DataSource = bd; /6、 dataConnection.Close(); /釋放所使用的所有資源。 suppliersTableAdapter.Dispose(); catch (Exception e) throw e; / / 用Da

9、taReader讀取數據 / private void CommandConnection() try SqlConnection sqlConn = new SqlConnection(); sqlConn.ConnectionString = Data Source=CCM02SQLEXPRESS;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sh2_123; sqlConn.Open(); SqlCommand sqlComad = new SqlCommand(); sqlComad.C

10、ommandType = CommandType.Text; sqlComad.Connection = sqlConn; sqlComad.CommandText = select * from products; /SqlDataReader:從數據庫獲取行 SqlDataReader sqlDr = sqlComad.ExecuteReader(); /FieldCount 取得目前資料列中的資料行數目。 DataTable dt = new DataTable(); int iDrCount = sqlDr.FieldCount; for (int iCnt = 0; iCnt iDr

11、Count; iCnt+) string a = sqlDr.GetName(iCnt); dt.Columns.Add(a); /dt.Rows.Add(DataRow)sqlDriCnt); /將DataReader前進到下一個資料 while (sqlDr.Read() /定義一個數組,便於讀出每一行資料 String subitems = new StringiDrCount; /用循環讀出每一行資料 for (int iCnt = 0; iCnt iDrCount; iCnt+) /讀出每一行資料保存到數組中 subitemsiCnt = sqlDriCnt.ToString();

12、string sValue = sqlDriCnt.ToString(); /將讀出的行資料增表的行中 dt.Rows.Add(subitems); /DataGridView的來源為“dt”這個表 dgvData.DataSource = dt; sqlDr.Close(); sqlComad.Dispose(); sqlConn.Close(); catch (Exception e) throw e; / / 用表進行填充DataGridView / private void DataTableCommandConnect() DataTable dtb = new DataTable(

13、); SqlConnection sqlConn = new SqlConnection(); sqlConn.ConnectionString = Data Source=CCM02SQLEXPRESS;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sh2_123; sqlConn.Open(); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = s

14、elect * from products; sqlCmd.Connection = sqlConn; SqlDataAdapter sqlDapter = new SqlDataAdapter(); sqlDapter.SelectCommand = sqlCmd; dtb.Clear(); /將讀出的資料填充一表dtb中 sqlDapter.Fill(dtb); /DataGridView的來源為“dt”這個表 dgvData.DataSource = dtb; sqlConn.Close(); sqlCmd.Dispose(); sqlDapter.Dispose(); / / 保存已修

15、改的資料 / / / private void btnSave_Click(object sender, EventArgs e) DataSet dsChanges = (DataSet)dtSet.GetChanges(); if (dsChanges=null) return; DataTable dtb = new DataTable(); dtb = dsChanges.Tablesproducts; /GetErrors方法返回一個數組,它由表中具一個或多個檢驗錯誤的行構成。如沒有誤返加空數組 DataRow badRows = dtb.GetErrors(); if (badRo

16、ws.Length=0) /int numRows = suppliersTableAdapter.Update(dsChanges, products); /要在需要?生成所需的命令,必?建 SqlCommandBuilder ?象的?例并使用?构造函?中的 DataAdapter。 /1、 SqlCommandBuilder sqlbuiler = new SqlCommandBuilder(); sqlbuiler.DataAdapter = suppliersTableAdapter; /2、 SqlConnection dataConnection = new SqlConnecti

17、on(); dataConnection.ConnectionString = Data Source=CCM02SQLEXPRESS;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sh2_123; /dataConnection.Open(); string sCmdText = select * from products; /3、 SqlCommand sqlComad = new SqlCommand(); /指出CommandText讀取方式 sqlComad.CommandType

18、= CommandType.Text; /Connection:連接資料庫的Connection sqlComad.Connection = dataConnection; /CommandText:取得或設定要針對資料來源執行的文字命令。 sqlComad.CommandText = sCmdText; /4、 /suppliersTableAdapter.SelectCommand = new SqlCommand(select * from products , dataConnection); /SelectCommand 取得或設定用來在資料來源中選取資料錄的命令。 supplier

19、sTableAdapter.SelectCommand = sqlComad; /取得或設定接資料庫的Connection /取得或設定用來將新的資料錄插入至資料來源的命令。 /GetInsertCommand:取得在資料來源上執行插入時所需之自動產生的 DbCommand 物件。 suppliersTableAdapter.InsertCommand = sqlbuiler.GetInsertCommand(); suppliersTableAdapter.UpdateCommand = sqlbuiler.GetUpdateCommand(); suppliersTableAdapter.

20、DeleteCommand = sqlbuiler.GetDeleteCommand(); /5、 int numRows = suppliersTableAdapter.Update(dsChanges, products); if (numRows0) MessageBox.Show(Update + numRows + rows, Success); /變更資料 dtSet.AcceptChanges(); else /返原所有更 dtSet.RejectChanges(); /清除與這個 DbCommandBuilder 關聯的命令。 sqlbuiler.RefreshSchema()

21、; /關閉聯接 dataConnection.Close(); /釋放資源 sqlComad.Dispose(); sqlbuiler.Dispose(); else string errorMsg = null; foreach (DataRow row in badRows) /每一行都可能一個或多個誤,而GetColumnsInError方法將返加一個集合,其中包含了數據有問題的所有例 foreach (DataColumn col in row.GetColumnsInError() /GetColumnError方法獲取一個無效的列的錯誤消息。每一條錯誤消息都附加到errorMsg字

22、符串上。 errorMsg += row.GetColumnError(col) + n; MessageBox.Show(Errors in data: + errorMsg, please fix, MessageBoxButtons.OK, MessageBoxIcon.Error); / / 新增按鈕,新增一個空行 / / / private void btnAdd_Click(object sender, EventArgs e) if (dgvData.DataSource!=null) bd.AddNew(); / / 刪除按鈕,刪當前資料 / / / private void

23、btnDelete_Click(object sender, EventArgs e) int iSelectRowCount = dgvData.SelectedRows.Count; int iSelectCellCount = dgvData.SelectedCells.Count; int iOldCellIndex = 0; if (iSelectCellCount0) /判斷是否是選擇了行 if (iSelectRowCount0) /循環刪除行 foreach (DataGridViewRow dgvRow in dgvData.SelectedRows) dgvData.Row

24、s.Remove(dgvRow); MessageBox.Show(Delete + iSelectRowCount + rows, Success); / 判斷是否是擇了存儲格 if (dgvData.SelectedCells.Count0) /定義一個ArrayList存放行資料 ArrayList al = new ArrayList(); /循環存儲格,得到行Index存入ArrayList foreach (DataGridViewCell dgvCell in dgvData.SelectedCells) /*IndexOf:搜尋指定的 Object,並傳回在整個 ArrayLi

25、st 中第一個符合元素之以零起始的索引。 如果有找到,則是在整個 ArrayList 內,value 第一次出現的以零起始的索引,否則為 -1。 */ if (al.IndexOf(dgvCell.RowIndex) = -1) /將行號增加到數組中 al.Add(dgvCell.RowIndex); foreach (int iIndex in al) dgvData.Rows.RemoveAt(iIndex); #region 當選擇行時,刪除該行 /int iSelectRowCount = dgvData.SelectedRows.Count; /if (dgvData.Selecte

26、dRows.Count 0) / / foreach (DataGridViewRow dgvRow in dgvData.SelectedRows) / / dgvData.Rows.Remove(dgvRow); / / MessageBox.Show(Delete + iSelectRowCount + rows, Success); / #endregion #region 當選擇存儲格時,刪除該存儲的行 /當選擇的存儲格大於0時(即選擇了多行) /if (dgvData.SelectedCells.Count 0) / / int iSelectCellsCount = dgvDat

27、a.SelectedCells.Count; / /刪除方法1 / foreach (DataGridViewCell dgvCell in dgvData.SelectedCells) / / int iRowIndex = dgvCell.RowIndex; / dgvData.Rows.RemoveAt(iRowIndex); / /刪除方法2 /循環刪除時的方法,(要先刪除前一筆,在刪除下一筆,要不然會出錯) /for (int iCellsCount = iSelectRowCount-1; iCellsCount =0 ; iCellsCount-) / / int iRowIndex = dgvData.SelectedCellsiCellsCount.RowIndex; / /取得所選資料行的索引 / dgvData.Rows.RemoveAt(iRowIndex); / / #endregion /刪除當前資料行 方法1 /dgvData.Rows.Remove(dgvData.CurrentRow); /刪除當前資料行 方法2 /dgvData.Rows.RemoveAt(dgvData.CurrentCell.RowIndex); /刪除當前資料行 方法3 /bd.RemoveCurrent();

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1