C#编程-Access数据库的简单应用
对于简单的应用,不推荐使用密码。直接使用。
1.查询
2.绑定
绑定代码
3.添加
添加一条记录
4.删除表中所有记录
删除一个数据表中所有记录
5.删除一个表
删除一个表
1 string reg = "drop table " + TableName; 2 OleDbConnection myconnection = new OleDbConnection(@"Provider = Microsoft.Jet.OLEDB.4.0;Data Source =AxisPower.mdb"); 3 myconnection.Open(); 4 5 OleDbCommand mycommand = myconnection.CreateCommand(); 6 mycommand.CommandText = reg; 7 mycommand.ExecuteNonQuery(); 8 myconnection.Close();
6.复制一个表
复制一个表
1 string reg = "select * into " + NewTableName + " from POWER "; 2 3 4 OleDbConnection myconnection = new OleDbConnection(@"Provider = Microsoft.Jet.OLEDB.4.0;Data Source =AxisPower.mdb"); 5 myconnection.Open(); 6 7 OleDbCommand mycommand = myconnection.CreateCommand(); 8 mycommand.CommandText = reg; 9 mycommand.ExecuteNonQuery(); 10 myconnection.Close();
7.获得所有表名
获得所有表名
1 OleDbConnection conn = new OleDbConnection(); 2 try 3 { 4 5 conn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source =AxisPower.mdb"; 6 conn.Open(); 7 DataTable shemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); 8 int n = shemaTable.Rows.Count; 9 string[] strTable = new string[n]; 10 int m = shemaTable.Columns.IndexOf("TABLE_NAME"); 11 for (int i = 0; i < n; i++) 12 { 13 DataRow m_DataRow = shemaTable.Rows[i]; 14 strTable[i] = m_DataRow.ItemArray.GetValue(m).ToString(); 15 } 16 return strTable; 17 } 18 catch 19 { 20 return null; 21 } 22 finally 23 { 24 conn.Close(); 25 conn.Dispose(); 26 }