Dotnet 的C#用SQL语句直接操作ACCESS的相关示例
- 2017-09-04 10:25:00
- 生活无限的BLOG 转贴
- 4128
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 |