C # error when dynamically creating an Access database: "Unrecognized database format"

Source: Internet
Author: User

You must use C # To dynamically create an Access database.

Office 2007 is used, and the database file format is. accdb

At the beginning, I thought I would check whether the file exists. If it does not exist, I would just create a database file.

The Code is as follows:

String filepath = new directoryinfo ("."). fullname + @ "\ data. accdb"; if (! File. exists (filepath) {console. writeline ("file data. accdb does not exist "); filestream FS = file. create (filepath); FS. close (); string connstr = string. format ("provider = Microsoft. ace. oledb.12.0; Data Source = {0} ", filepath); oledbconnection conn = new oledbconnection (connstr); Conn. open (); oledbcommand created STR = new oledbcommand (); created Str. connection = conn; Response Str. commandtext = "create table usrinfo (usrid int not null primary key, usrname char (16), kgname char (16), usrsignature char (255)"; Comment Str. executenonquery ();}

When the conn. open () method is called, the following error occurs: Unrecognized database format

Error:

Check the file. The size is 0. If you create the file manually, the initial size of data. accdb is not 0.

Error cause:

When using the CREATE () method of the file class, only empty files are created, and the basic format of the Access database is not available.

Solution:

Use C # the correct method to dynamically create an Access database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.