-
大小: 45KB文件類型: .rar金幣: 2下載: 2 次發(fā)布日期: 2021-11-08
- 語言: C#
- 標(biāo)簽: C#??SQL??DataGridView??
資源簡介
C#實現(xiàn)將SQL數(shù)據(jù)庫的內(nèi)容讀取出來顯示到DataGridview中,同時根據(jù)DataGridview更新數(shù)據(jù)庫內(nèi)容

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
using?System.Data.SqlClient;
using?System.Collections;
namespace?SQL
{
????public?partial?class?Form1?:?Form
????{
????????SqlDataAdapter?sa?=?null;
????????SqlCommandBuilder?sb?=?null;
????????DataTable?dt?=?null;
????????BindingSource?bs?=?null;?
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????private?void?button1_Click(object?sender?EventArgs?e)
????????{
???????????//?ArrayList?DBDataArray?=?new?ArrayList();
????????????string?connString?=?“Server=localhost;Database=MySQL;Integrated?Security?=?SSPI;“;
????????????
????????????SqlConnection?conn?=?new?SqlConnection(connString);
????????????
????????????//string?sqlStatement?=?“select?*?from?Infomation“;
????????????//SqlCommand?sqlcmd?=?new?SqlCommand(sqlStatement?conn);????????????//設(shè)置參數(shù)
????????????try?
????????????{?
????????????????conn.Open();
????????????????//SqlDataReader?sdr?=?sqlcmd.ExecuteReader();?//執(zhí)行SQL語句
????????????????//int?cols?=?sdr.FieldCount;???//獲取結(jié)果行中的列數(shù)
?????????????????????????????????????
????????????????//while?(sdr.Read())
????????????????//{
????????????????//????object[]?values?=?new?object[cols];
????????????????//????sdr.GetValues(values);???????//values保存一行數(shù)據(jù)
????????????????//????DBDataArray.Add(values);
???????????????
????????????????//}
????????????????//sdr.Close();
????????????????string?sqlCheckStr?=?“select?*?from?Infomation“;
????????????????sa?=?new?SqlDataAdapter(sqlCheckStr?conn);
????????????????sb?=?new?SqlCommandBuilder(sa);//綁定SqlDataAdapter?
????????????????dt?=?new?DataTable();
????????????????sa.Fill(dt);
????????????????dataGridView1.DataSource?=?dt;
????????????????bs?=?new?BindingSource();
????????????????bs.DataSource?=?dt;?//綁定BindingSource?
????????????????conn.Close();
????????????????//?設(shè)定單元格的ToolTip內(nèi)容
????????????????dataGridView1.Columns[0].ToolTipText?=?“該列單元格的內(nèi)容不能為空“;
????????????}???
????????????catch?(Exception?ex)
????????????{
????????????????conn.Close();
????????????????throw?ex;
????????????}?
????????}
????????private?void?button2_Click(object?sender?EventArgs?e)
????????{
????????????try
????????????{
????????????????if?(?null?==?sa?)
????????????????{
????????????????????MessageBox.Show(“請先打開數(shù)據(jù)庫“);
????????????????????return;
????????????????}
????????????????sa.Update((DataTable)bs.DataSource);
????????????}
????????????catch?(ArgumentNullException)
????????????{
????????????????MessageBox.Show(“DataSet?無效“);
????????????}
????????????catch?(InvalidOperationException)
????????????{
????????????????MessageBox.Show(“源表?無效“);
????????????}
????????????catch?(DBConcurrencyException)
????????????{
????????????????MessageBox.Show(“試圖執(zhí)行?INSERT、UPDATE?或?DELETE?語句,但沒有記錄受到影響“);
???
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????12288??2009-07-08?23:35??SQL\SQL\bin\Debug\SQL.exe
?????文件??????32256??2009-07-08?23:35??SQL\SQL\bin\Debug\SQL.pdb
?????文件??????14328??2009-07-08?23:37??SQL\SQL\bin\Debug\SQL.vshost.exe
?????文件????????490??2007-07-21?01:33??SQL\SQL\bin\Debug\SQL.vshost.exe.manifest
?????文件??????20480??2009-07-03?08:25??SQL\SQL\Databa
?????文件???????4761??2009-07-08?23:34??SQL\SQL\Form1.cs
?????文件???????5394??2009-07-08?23:33??SQL\SQL\Form1.Designer.cs
?????文件???????5814??2009-07-08?23:33??SQL\SQL\Form1.resx
?????文件????????630??2009-07-08?23:35??SQL\SQL\Form2.cs
?????文件???????2627??2009-07-08?23:35??SQL\SQL\Form2.Designer.cs
?????文件???????5814??2009-07-08?23:35??SQL\SQL\Form2.resx
?????文件????????460??2009-07-08?23:37??SQL\SQL\obj\Debug\SQL.csproj.FileListAbsolute.txt
?????文件????????905??2009-07-08?23:35??SQL\SQL\obj\Debug\SQL.csproj.GenerateResource.Cache
?????文件??????12288??2009-07-08?23:35??SQL\SQL\obj\Debug\SQL.exe
?????文件????????180??2009-07-08?23:34??SQL\SQL\obj\Debug\SQL.Form1.resources
?????文件????????180??2009-07-08?23:35??SQL\SQL\obj\Debug\SQL.Form2.resources
?????文件??????32256??2009-07-08?23:35??SQL\SQL\obj\Debug\SQL.pdb
?????文件????????180??2009-07-08?23:34??SQL\SQL\obj\Debug\SQL.Properties.Resources.resources
?????文件????????484??2009-06-30?09:42??SQL\SQL\Program.cs
?????文件???????1364??2009-06-30?09:42??SQL\SQL\Properties\AssemblyInfo.cs
?????文件???????2856??2009-06-30?09:42??SQL\SQL\Properties\Resources.Designer.cs
?????文件???????5612??2009-06-30?09:42??SQL\SQL\Properties\Resources.resx
?????文件???????1088??2009-06-30?09:42??SQL\SQL\Properties\Settings.Designer.cs
?????文件????????249??2009-06-30?09:42??SQL\SQL\Properties\Settings.settings
?????文件???????5758??2009-07-08?23:33??SQL\SQL\SQL.csproj
?????文件????????486??2009-07-03?08:38??SQL\SQL\SQL.csproj.user
?????文件????????899??2009-06-30?09:42??SQL\SQL.sln
????..A..H.?????16384??2009-07-08?23:44??SQL\SQL.suo
?????目錄??????????0??2009-06-30?09:42??SQL\SQL\obj\Debug\TempPE
?????目錄??????????0??2009-07-08?23:34??SQL\SQL\bin\Debug
............此處省略10個文件信息
評論
共有 條評論