添加Microsoft ADO Ext.2.8 for DDL and Security和Microsoft ADO Ext.6.0 for DDL and Security
using System.Data.OleDb;
public static bool CreateAccessDb(string filePath)
{
ADOX.Catalog catalog = new ADOX.Catalog();
if(!System.IO.File.Exists(filePath))
{
try
{
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath +
";Jet OLEDB:Engine Type = 5");
//创建或修改都需要这句话
//转换为ADO连接,并关闭
(catalog.ActiveConnection as ADODB.Connection).Close();
catalog.ActiveConnection = null;
catalog = null;
}
catch(System.Exception e)
{
return false;
}
}
return true;
}
//传入被操作的mdb文件路径,传入要执行的sql语句
public static void ExecuteSQLSentence(string filePath,string sql_sentence)
{
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath;
OleDbConnection connect = new OleDbConnection(connectionString);
{
ADOX.Catalog catalog = new ADOX.Catalog();
if(!System.IO.File.Exists(filePath))
{
try
{
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath +
";Jet OLEDB:Engine Type = 5");
//创建或修改都需要这句话
//转换为ADO连接,并关闭
(catalog.ActiveConnection as ADODB.Connection).Close();
catalog.ActiveConnection = null;
catalog = null;
}
catch(System.Exception e)
{
return false;
}
}
return true;
}
//传入被操作的mdb文件路径,传入要执行的sql语句
public static void ExecuteSQLSentence(string filePath,string sql_sentence)
{
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath;
OleDbConnection connect = new OleDbConnection(connectionString);
try
{
connect.Open();
try
{
OleDbCommand cmdstr = new OleDbCommand();
cmdstr.Connection = connect;
cmdstr.CommandText = sql_sentence;
cmdstr.ExecuteNonQuery();
connect.Close();
}
catch
{
MessageBox.Show("sql语句出现错误");
}
}
catch
{
MessageBox.Show("数据库连接失败!!");
}
}
{
connect.Open();
try
{
OleDbCommand cmdstr = new OleDbCommand();
cmdstr.Connection = connect;
cmdstr.CommandText = sql_sentence;
cmdstr.ExecuteNonQuery();
connect.Close();
}
catch
{
MessageBox.Show("sql语句出现错误");
}
}
catch
{
MessageBox.Show("数据库连接失败!!");
}
}