Dotnet 的C#用SQL語句直接操作ACCESS的相關示例
- 2017-09-04 10:25:00
- 生活無限的BLOG 轉貼
- 4106
Dotnet 的C#用SQL語句直接操作ACCESS的相關示例
C#操作ACCESS的基本操作方法示例,主要給C#新手操控Access數據庫的入門
有這方麵需要的網友可以蔘考一下,主要是對Access數據庫的增刪改查等操作
C#操作Access的具體代碼如下
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 ADOX; using System.Data.OleDb; using System.IO; namespace accesssql { public partial class access練習 : Form { public access練習() { InitializeComponent(); } private void 創建access_Click(object sender, EventArgs e) { //創建ACCESS文件需要ADOX,創建引用,導入COM中的Microsoft ADO Ext. 2.8 for DLL and Security,併using ADOX; FileInfo myfile = new FileInfo("sldb.mdb"); if (myfile.Exists) { MessageBox.Show("當前文件夾下已存在sldb.mdb文件,放棄創建"); } else { try { CatalogClass sldb = new CatalogClass(); sldb.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=sldb.mdb;" + "Jet OLEDB:Engine Type=5");//創建sldb.mdb sldb = null; MessageBox.Show("在程序文件夾下創建sldb.mdb成功。"); } catch (Exception ex) { MessageBox.Show(Convert.ToString(ex.Message)); } } } private void button1_Click(object sender, EventArgs e) { OleDbConnection sldb = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sldb.mdb"); try { sldb.Open(); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = sldb;//設置命令的對像是SLDB數據庫 //建錶Folder,括號內爲:字段1 類型,字段2 類型,......(NOT NULL錶示不能空) cmd.CommandText = "Create Table Folder (FolderID INT NOT NULL,SuperiorID INT,FolderGrade INT,FolderName TEXT(255),AllPathName TEXT(255))";//SQLCOMMAND語句 cmd.ExecuteNonQuery();//執行SQLCMMAD MessageBox.Show("增加錶Folder,錶項FolderID SuperiorID FolderGrade FolderName成功"); } catch (Exception ex) { MessageBox.Show(Convert.ToString(ex.Message)); } finally { sldb.Close();sldb =null;} } private void button2_Click(object sender, EventArgs e) { //清除錶格 //DROP TABLE "錶格名" OleDbConnection sldb = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sldb.mdb"); try { sldb.Open(); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = sldb; cmd.CommandText = "DROP TABLE Folder";//SQLCOMMAND語句 cmd.ExecuteNonQuery();//執行SQLCMMAD MessageBox.Show("刪除Folder錶成功"); } catch (Exception ex) { MessageBox.Show(Convert.ToString(ex.Message)); } finally { sldb.Close(); sldb = null; } } private void button3_Click(object sender, EventArgs e) { //增加一條記録 //INSERT INTO "錶格名" ("欄位1", "欄位2", ...) VALUES ("值1", "值2", ...) OleDbConnection sldb = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sldb.mdb"); try { sldb.Open(); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = sldb; cmd.CommandText = "INSERT INTO Folder (FolderID,SuperiorID,FolderGrade,FolderName,AllPathName) VALUES (1001,2001,3001,'SLonline','SL1')";//SQLCOMMAND語句 cmd.ExecuteNonQuery();//執行SQLCMMAD MessageBox.Show("增加錶Folder內記録一條成功"); } catch (Exception ex) { MessageBox.Show(Convert.ToString(ex.Message)); } finally { sldb.Close();sldb =null;} } private void button4_Click(object sender, EventArgs e) { //修改某條記録 //UPDATE "錶格名" SET "欄位1" = [新值] WHERE {條件} OleDbConnection sldb = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sldb.mdb"); try { sldb.Open(); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = sldb; cmd.CommandText = "UPDATE Folder SET AllPathName = 'SL2' WHERE FolderID = 1001";//SQLCOMMAND語句 cmd.ExecuteNonQuery();//執行SQLCMMAD MessageBox.Show("修改錶Folder內記録一條成功"); } catch (Exception ex) { MessageBox.Show(Convert.ToString(ex.Message)); } finally { sldb.Close(); sldb = null; } } private void button5_Click(object sender, EventArgs e) { //刪除記録 //DELETE FROM "錶格名" WHERE {條件} //先添加一條記録 OleDbConnection sldb = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sldb.mdb"); try { sldb.Open(); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = sldb; cmd.CommandText = "INSERT INTO Folder (FolderID,SuperiorID,FolderGrade,FolderName,AllPathName) VALUES (1003,2003,3003,'SLonline','SL3')";//SQLCOMMAND語句 cmd.ExecuteNonQuery();//執行SQLCMMAD MessageBox.Show("增加錶Folder內記録一條成功"); } catch (Exception ex) { MessageBox.Show(Convert.ToString(ex.Message)); } finally { sldb.Close();sldb =null;} //再把這條記録刪除 OleDbConnection sldb1 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sldb.mdb"); try { sldb1.Open(); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = sldb1; cmd.CommandText = "DELETE FROM Folder WHERE FolderID = 1001";//SQLCOMMAND語句 cmd.ExecuteNonQuery();//執行SQLCMMAD MessageBox.Show("刪除FolderID o 1001的記録成功"); } catch (Exception ex) { MessageBox.Show(Convert.ToString(ex.Message)); } finally { sldb1.Close(); sldb1 = null; } } private void button6_Click(object sender, EventArgs e) { //清除錶格內的所有信息 //DELETE * FROM 錶1; OleDbConnection sldb = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sldb.mdb"); try { sldb.Open(); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = sldb; cmd.CommandText = "DELETE * FROM Folder";//SQLCOMMAND語句 cmd.ExecuteNonQuery();//執行SQLCMMAD MessageBox.Show("清空錶Folder內記録成功"); } catch (Exception ex) { MessageBox.Show(Convert.ToString(ex.Message)); } finally { sldb.Close(); sldb = null; } } private void button8_Click(object sender, EventArgs e) { if (File.Exists("sldb.mdb")) { try { File.Delete("sldb.mdb"); MessageBox.Show("文件sldb.mdb刪除成功。"); } catch (Exception ex) { MessageBox.Show(Convert.ToString(ex.Message)); } } else { MessageBox.Show("沒有找到sldb.mdb文件"); } } private void button7_Click(object sender, EventArgs e) { OleDbConnection sldb = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sldb.mdb"); try { sldb.Open(); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = sldb; cmd.CommandText = "SELECT COUNT(*) FROM Folder";//SQLCOMMAND語句 int number=(int)cmd.ExecuteScalar();//執行SQLCMMAD,ExecuteScalar是反迴一箇行第一列的結果 MessageBox.Show("Folder錶中共有" + Convert.ToString(number)); } catch (Exception ex) { MessageBox.Show(Convert.ToString(ex.Message)); } finally { sldb.Close(); sldb = null; } } private void button9_Click(object sender, EventArgs e) { OleDbConnection sldb = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sldb.mdb"); try { sldb.Open(); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = sldb; cmd.CommandText = "SELECT FolderGrade FROM Folder WHERE FolderID = 1001";//SQLCOMMAND語句 int number = (int)cmd.ExecuteScalar();//執行SQLCMMAD,ExecuteScalar是反迴一箇行第一列的結果, MessageBox.Show("Folder錶中FolderID爲1001的記録,FolderGrade項的值是:"+ Convert.ToString(number)); } catch (Exception ex) { MessageBox.Show(Convert.ToString(ex.Message)); } finally { sldb.Close(); sldb = null; } } private void button10_Click(object sender, EventArgs e) { //ALTER TABLE Cars ADD COLUMN Condition TEXT(10) //在錶Cars內增加一箇字段,名字爲Condition(狀況),長度爲10 OleDbConnection sldb = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sldb.mdb"); try { sldb.Open(); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = sldb; cmd.CommandText = "ALTER TABLE Folder ADD COLUMN readme TEXT(255)";//SQLCOMMAND語句 cmd.ExecuteNonQuery();//執行SQLCMMAD MessageBox.Show("Folder錶中增加readme字段,文本型長度255"); } catch (Exception ex) { MessageBox.Show(Convert.ToString(ex.Message)); } finally { sldb.Close(); sldb = null; } } private void button11_Click(object sender, EventArgs e) { //ALTER TABLE Cars ADD COLUMN Condition TEXT(10) //在錶Cars內增加一箇字段,名字爲Condition(狀況),長度爲10 OleDbConnection sldb = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sldb.mdb"); try { sldb.Open(); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = sldb; cmd.CommandText = "ALTER TABLE Folder DROP readme";//SQLCOMMAND語句 cmd.ExecuteNonQuery();//執行SQLCMMAD MessageBox.Show("Folder錶中刪除瞭readme字段"); } catch (Exception ex) { MessageBox.Show(Convert.ToString(ex.Message)); } finally { sldb.Close(); sldb = null; } } private void button13_Click(object sender, EventArgs e) { //除非用ADOX,ADO沒有直接改名方法,所以用複製錶,然後再刪除舊錶的方法。 //SELECT * INTO newtable FROM oldtable OleDbConnection sldb = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sldb.mdb"); try { sldb.Open(); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = sldb; cmd.CommandText = "SELECT * INTO File FROM Folder";//SQLCOMMAND語句 //如果隻要某些字段過去:"SELECT FolderID,SuperiorID INTO File FROM folder" cmd.ExecuteNonQuery();//執行SQLCMMAD cmd.CommandText = "DROP TABLE Folder";//SQLCOMMAND語句 cmd.ExecuteNonQuery();//執行SQLCMMAD MessageBox.Show("將Folder錶改名爲File"); } catch (Exception ex) { MessageBox.Show(Convert.ToString(ex.Message)); } finally { sldb.Close(); sldb = null; } } } }
Access數據庫自身
- office課程播放地址及課程明細
- Excel Word PPT Access VBA等Office技巧學習平颱
- 將( .accdb) 文件格式數據庫轉換爲早期版本(.mdb)的文件格式
- 將早期的數據庫文件格式(.mdb)轉換爲 (.accdb) 文件格式
- KB5002984:配置 Jet Red Database Engine 數據庫引擎和訪問連接引擎以阻止對遠程數據庫的訪問(remote table)
- Access 365 /Access 2019 數據庫中哪些函數功能和屬性被沙箱模式阻止(如未啟動宏時)
- Access Runtime(運行時)最全的下載(2007 2010 2013 2016 2019 Access 365)
Access Activex第三方控件
- Activex控件或Dll 在某些電腦無法正常註冊的解決辦法(regsvr32註冊時卡住)
- office使用部分控件時提示“您沒有使用該ActiveX控件許可的問題”的解決方法
- RTF文件(富文本格式)的一些解析
- Access樹控件(treeview) 64位Office下齣現橫曏滾動條不會自動定位的解決辦法
- Access中國樹控件 在win10電腦 節點行間距太小的解決辦法
- EXCEL 2019 64位版(Office 2019 64位)早就支持64位Treeview 樹控件 ListView列錶等64位MSCOMMCTL.OCX控件下載
- VBA或VB6調用WebService(直接Post方式)併解析返迴的XML
Access ADP Sql Server等
- 早期PB程序連接Sqlserver齣現錯誤
- MMC 不能打開文件C:/Program Files/Microsoft SQL Server/80/Tools/Binn/SQL Server Enterprise Manager.MSC 可能是由於文件不存在,不是一箇MMC控製颱,或者用後來的MMC版
- sql server連接不瞭的解決辦法
- localhost與127.0.0.1區彆
- Roych的淺談數據庫開髮繫列(Sql Server)
- sqlserver 自動備份對備份目録沒有存取權限的解決辦法
- 安裝Sql server 2005 express 和SQLServer2005 Express版企業管理器 SQLServer2005_SSMSEE
文章分類
聯繫我們
聯繫人: | 王先生 |
---|---|
Email: | 18449932@qq.com |
QQ: | 18449932 |
微博: | officecn01 |