简体   繁体   中英

Oledbconnection to Access Database C#

I face some problem:

I can insert data to my database by following code:

oledbconnection = new oledbconnection (@"Provider=Microsoft.Jet.OLEDB.4.0;Data 
                                   Source=C:\User\yourdbfile.accdb");

the data was successfully insert to my database,

but if i change the code to

oledbconnection = new oledbconnection (@"Provider=Microsoft.Jet.OLEDB.4.0;Data 
                             Source=yourdbfile.accdb");

OR

oledbconnection = new oledbconnection (@"Provider=Microsoft.Jet.OLEDB.4.0;Data 
                       Source=|DataDirectory|\yourdbfile.accdb");

I'm unable to insert data to database, and there is no error.


Another Question,

How can i publish my system with database file? because when i installed this system on another pc, the error show C:\\User\\yourdbfile.accdb not found.

您可以将db源文件放入应用程序文件夹中,例如:install disk folder:d:\\ AA \\ BB \\,DB文件:D:\\ AA \\ BB \\ DB file \\

You can check for the Property of the Access file on its Build property value.

OR

Modifying your ProjectName.csproj is a better way to over come with this problem. See details below:

<Target Name="AfterBuild">
    <Copy SourceFiles="$(OutputPath)yourdbfile.accdb" DestinationFolder="$(YourVariable)" ContinueOnError="true" />
</Target>

See this link for your reference.

After checking those things, You can now use:

var oledbconnection = new Oledbconnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=yourdbfile.accdb");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM