How to publish my windows form application with da

2019-09-12 13:42发布

问题:

I am developing Winform application using vb.net and MS Access in Viusal Studio 2012. I completed my application and now i just wanted to publish it. In my project Solution I have forms and rpt files (crystal reports). I use the following connection string to my database which is not included to my project solution :

     conn.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Application.StartupPath & "\tblcmplist.mdb"

Now the database should be there in folder where my application starts. If I run from the bin folder it works but if i publish my application by right clicking solution->publish and if i run from the published location i am getting error like the database file is missing.

I copied my database file to that startup folder of my application still the error occurs. What is the solution plz help me.

And one more thing I had to ask I am using crystal reports in my application. I installed CRforVS_13_0_5 developer version and I created reports in visual studio and I can view the report it works fine. But if I run the application in clients machin it gives the error could not find crystal report blah blah... what should i install in my client's pc to view the report ? Plz help me Thank you....

回答1:

You will need to deploy your database as "IncludeData" in the application settings. That will make it appear in the folder defined by ApplicationDeployment.DataDirectory (running as a ClickOnce app). To test this from VS as well as deployed, you will want to do something like this:

string dataPath;
if (ApplicationDeployment.IsNetworkDeployed)
  dataPath = ApplicationDeployment.CurrentDeployment.DataDirectory;
else
  dataPath = System.Windows.Forms.Application.StartupPath;
dataPath = System.IO.Path.Combine(dataPath, "yourdatabasefilename");
If you deploy it in the Database folder and mark it as Include instead of Include(Data), the datapath would be 
Path.Combine(System.Windows.Forms.Application.StartupPath, "Database\yourdatabasefilename");

If you deploy it as data, when you issue an update it will be copied forward. If you change the local copy of it, a new version will be deployed and the old version will be placed in a subfolder of the Data folder called .\pre. Then if you need to migrate data, you can. If you want more granularity over the replacement of your database, check out this article, which shows you how to retain data between ClickOnce updates: http://robindotnet.wordpress.com/2009/08/19/where-do-i-put-my-data-to-keep-it-safe-from-clickonce-updates/

Source



回答2:

There might be several ways to trouble shoot: You could make a test program that showed a msgBox showing Application.StartupPath so you could manually go check where the database should be and see if the database is in fact located at that location.

Other than that you could use the Try Catch like following:

    Try
        'your database call
    Catch ex As Exception
         MsgBox(ErrorToString & Environment.NewLine & Environment.NewLine & ex.ToString)
    Finally
        conn.Close()
    End Try

I use ErrorToString cause its easily understandable and gives a basic idea of what the issue may be, then i use Environment.NewLine to make 2 new line to make some spacing for the ex.toString for more specific details of what is going on within the code.

If you keep hitting a dead end every time, there is also the possibility to check if a database file exist on form_load if not create the database file manually through code.