检查MS Access数据库的表,如果不存在创建它 [英] Check for MS Access database table if not exist create it

查看:488
本文介绍了检查MS Access数据库的表,如果不存在创建它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何编程方式检查MS Access数据库表,如果不存在则创建它?

How do you programmatically check for MS Access database table, if not exist then create it?

推荐答案

简单的执行下面的代码,如果表中会存在它会返回错误其他明智它会创建一个新的:

Simply execute following code if table will exist it will return error other wise it will create a new one:

try
{
        OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + frmMain.strFilePath + "\\ConfigStructure.mdb");
        myConnection.Open();
        OleDbCommand myCommand = new OleDbCommand();
        myCommand.Connection = myConnection;
        myCommand.CommandText = "CREATE TABLE <yourtable name>(<columns>)";
        myCommand.ExecuteNonQuery();
        myCommand.Connection.Close();
}
catch(OleDbException e)
{  
    if(e.ErrorCode == 3010 || e.ErrorCode == 3012)
    // if error then table exist do processing as required
}

是,如果一个表已经存在返回的这些错误代码 - 检查这里所有。

Those error codes are returned if a table already exists - check here for all.

这篇关于检查MS Access数据库的表,如果不存在创建它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆