在运行时创建Access数据库 [英] Creating an Access Database at Run time

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

问题描述

你好;

我想在运行时创建一个Access数据库.我用下面的代码.尽管它创建了一个文件,但是无法打开它,说明它具有无法识别的格式.您能告诉我如何在运行时创建Access数据库吗?

非常感谢您

Hello;

I want to create an Access Database at Run time. I used the code below. Although it creates a file, it can not be opened saying that it has unrecognized format. Could you please let me know how I can create an Access Database at Run time?

Thank you very much

...
string FilePath = FolderPath + "\\myDataBasae.accdb" +;
if (!File.Exists(FilePath))
            {
                File.Create(FilePath);
                OleDbConnection myConnection = new OleDbConnection();
                myConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= "
                                               + FilePath;
                OleDbCommand myCommand = new OleDbCommand();
                myCommand.Connection = myConnection;

                myCommand.CommandText = "CREATE TABLE myTable " +
                                          " (" +
                                            " ID int IDENTITY(1,1) PRIMARY KEY," +
                                            " Day DateTime," +
                                            " Events MEMO" +
                                            " )";
                try
                {
                    myConnection.Open();// This line raises an Exception
                    myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    myConnection.Close();
                }

            }

推荐答案

两个替代选项;

  • 使用Access创建一个空的数据库,并将其作为资源添加到可执行文件中.
  • 使用VBScript创建新数据库(另存为.vbs并执行)
Two alternative options;

  • Create an empty DB using Access and add it to the executable as a resource.
  • Use VBScript to create a new database (save as .vbs and execute)
Set objConnection = CreateObject("ADOX.Catalog")

objConnection.Create _
    "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = myDatabeest.mdb"


创建数据库文件不只是执行File.Create()语句而已;这只会在目录中创建一个条目.尽管Eddy使用VBScript的建议也可能有效,但使用Access创建空白数据库文件可能是最好的选择.我从来没有尝试过.
There is more to creating a database file than simply executing a File.Create() statement; this just creates an entry in the directory. Using Access to create a blank database file is probably your best bet, though Eddy''s suggestion using VBScript may also work; I''ve never tried that.


您为什么要动态创建数据库?它只是使整个代码复杂化了.为您的应用程序提供框架数据库.如果数据库不存在,只需停止进一步处理并显示有用的错误消息.
Why on earth do you want to create db on-the-fly? It simply is over complicating the whole code. Provide skeleton database with your application. If the database does not exist, simply stop further processing and display useful error message.


这篇关于在运行时创建Access数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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