none
VB database project wont see database on target machine.

    Question

  • Okay, So I started my first project with visual studio. I was following a few tutorials on building an application with database. 

    I managed to create a landlord/tenant application with a local database. I now need to install this project on the target computer but when I am testing on a virtual machine running windows, it can find the database. 

    I'm sure its something to do with the file path connection string and placing the database file within the application's files. 

    I am a first timer with databases and would like to know if anyone can help me out here.

    Tuesday, April 11, 2017 4:47 PM

Answers

  • In the following example I use the connection string from the app.config file, no issues.

    Connection string

    Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\CustomerDatabase.mdf;Integrated Security=True;Connect Timeout=30

    Sample usage

    public DataOperations()
    {
        this.CustomerDataTable = new DataTable();
        using (SqlConnection cn = new SqlConnection { ConnectionString = Properties.Settings.Default.ConnectionString })
        {
            using (SqlCommand cmd = new SqlCommand { Connection = cn })
            {
                cmd.CommandText = "SELECT Identifier, CompanyName FROM [Customer]";
                cn.Open();
                this.CustomerDataTable.Load(cmd.ExecuteReader());
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by mega_noob Tuesday, April 18, 2017 8:48 PM
    Saturday, April 15, 2017 10:36 AM
    Moderator
  • Thanks Karen Payne. 

    So would this code force the application to look in the same folder for the LocalDB?

    Will I not need to alter the connection string in App.config?

    Thank you. Very much for taking the time to reply to me. 

    Since you have the connection string in app.config, and if it's like this, DataDirectory will point to the same folder as the executable.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by mega_noob Tuesday, April 18, 2017 8:48 PM
    Saturday, April 15, 2017 5:57 PM
    Moderator

All replies

  • Yes in version 2008 Microsoft made something to make it easier with Express versions for Local databases.

    Since then that has the record of all asked questions in the forums. 

    It is very bad documented. Click in solution on the database and set the properties on Copy if newer 

    https://www.google.com/?hl=en&gws_rd=cr#hl=en&q=local+database+copy+if+newer

     

    Success
    Cor

    Tuesday, April 11, 2017 5:13 PM
  • Hi mega_noob,

    Welcome to the MSDN forum.

    >>I managed to create a landlord/tenant application with a local database. I now need to install this project on the target computer but when I am testing on a virtual machine running windows, it can find the database. 

    Do you mean you created a customer installer and use it to install the application on the VM? If so, what's your project template when you created the landlord/tenant application and the technology that you made the installer like ClickOnce, WIX or others...

    Best regards,

    Sara 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 12, 2017 7:01 AM
  • Thanks for your reply and taking the time. I really appreciate that. 

    Im really new at databases. This is my first project. I still have no luck. I cant fully understand whats happening. 

    I get the app working great. Its moving the app and DB to another machine thats not working. 
    Wednesday, April 12, 2017 9:17 PM
  • Hey, thanks for welcoming me. 

    I will try and explain the problem as best I can. 

    Okay, so I started a project in visual studio 2017 (tenant/landlord). 
    I created a local database within the application. 
    I completed the project. 

    I run the .exe on the host machine and it works no problem. I am able to edit, save and delete data from the local database. 

    Now, I am creating this application for someone and I will need to install it onto his machine. 
    So, I set up a virtual machine running windows 7 to test installing the app. 

    I have tried just copying the debug folder to the VM. The app will run, but as soon as I try and interact with the database, it spits out an error.  Like it cannot see the database. 
    I then used a program called "install creator pro" to create an installer for the .exe. This also will not see the database. and spits out an error.

    You see, I was under the impression that if I made a local database I would not need SQL server running on the target machine. I assumed I could simply move the application with DB file inside the same folder and install it and the app and database would be able to communicate no problem. 
    I feel really stupid at the moment. I have everything ready to go and I simply cannot move my app and database to another machine. 

    After some frustrating google searches, I keep coming back to the possibility that I would need to change the connection string. I have no idea if this is what I have to do. But I'm desperate to get this working. 

    If you could try and help me with this I will be grateful.  

    What do you think my issue is?

    Please, If you need any information at all, I will go and test everything, paste code, show a video of whats happening etc. 

    I'm desperate here. 

    This is my first time with VS and databases. So I apologise if I'm not making much sense. 

    Wednesday, April 12, 2017 9:34 PM
  • Hi mega_noob,

    Thank you for your detail description.

    After discussed with our colleague who are responsible of VB Development Support, your issue is about the development of VB, since our forum is to discuss the VS IDE, I will help you move this thread to the VB forum: https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vbgeneral to seek for a more professional support, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 13, 2017 6:35 AM


  • You see, I was under the impression that if I made a local database I would not need SQL server running on the target machine. I assumed I could simply move the application with DB file inside the same folder and install it and the app and database would be able to communicate no problem. 
    I feel really stupid at the moment. I have everything ready to go and I simply cannot move my app and database to another machine. 

    No you cannot. 

    It depends what database you use. A database is just a base of data. A simple card box is a database. But if it is a computer database and there needs to be a program installed on the other computer which handles that database. 

    Therefore as you want help than don't say, my database does not work, because then even answers to this applies.

    But tell what kind of database, where the data is located and if the software for the database is installed.


    Success
    Cor


    Thursday, April 13, 2017 8:33 AM
  • Hello,

    The following is one method to point to your database assuming the database is in the same folder as the application's executable.

    Create the connection as a private variable in a form, class or code module you are doing database operations. SO when using the connection string with code no matter what machine the app is on the database will be located.

    Private Builder As New OleDbConnectionStringBuilder With
        {
            .Provider = "Microsoft.ACE.OLEDB.12.0",
            .DataSource = IO.Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory, "Database1.accdb")
        }

    Simple usage

    Public Sub Demo()
        Using cn As New OleDbConnection With {.ConnectionString = Builder.ConnectionString}
            Using cmd As New OleDbCommand With {.Connection = cn, .CommandText = "TODO"}
                cn.Open()
            End Using
        End Using
    End Sub

    If using an attached SQL-Server database

    Dim Builder As New Data.SqlClient.SqlConnectionStringBuilder With {.AttachDBFilename = IO.Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory, "Database1.mdf")}


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, April 13, 2017 9:00 AM
    Moderator
  • It's somewhat unclear what type of database you are working with, but it sounds like you might be using SQL Server's LocalDB. While the LocalDB database engine has a smaller footprint than SQL Server or SQL Server Express, you still need to install it when deploying your app. I would recommend reading the below article for more information concerning deployment and the location of the database. Also, if you are encountering exceptions (errors) in your app I would recommend posting them so we may further assist.

    https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-2016-express-localdb


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, April 13, 2017 12:04 PM
  • Okay Sara Lui. Thanks alot. 
    Saturday, April 15, 2017 8:57 AM
  • Thanks Cor Ligthert, for the good description of databases. I appreciate that. 

    As for your question, I used the LocalDB functionality of mssql within the VB application. I was under the impression that the localDB function would allow me to use the database on other machine with an installation of SQL. 

    Saturday, April 15, 2017 9:01 AM
  • Thanks Karen Payne. 

    So would this code force the application to look in the same folder for the LocalDB?

    Will I not need to alter the connection string in App.config?

    Thank you. Very much for taking the time to reply to me. 
    Saturday, April 15, 2017 9:05 AM
  • Hi Paul P Clement IV. Thanks for the link, I plan on following it carefully. 

    You are correct. I'm using the LocalDB mssql. I was under the impression that using this would allow me to make a simple application only needed on one computer (app/database on one computer.) without installing SQL onto that computer. 

    I have never touched databases before and assumed it would be more simple than it is. 

    Like I said. The application works fine on my host machine. When I check running processes, I can see teh application is running and also SQL is running. 

    My issue is simply me not understanding clearly enough how to move the application and database together in the correct manner. I will keep trying all of you guys ideas and keep you all updated. 

    If I get any errors I will post them up. 

    I feel like just making a video and recording every step I do and posting it up. lol. 

    Saturday, April 15, 2017 9:13 AM
  • In the following example I use the connection string from the app.config file, no issues.

    Connection string

    Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\CustomerDatabase.mdf;Integrated Security=True;Connect Timeout=30

    Sample usage

    public DataOperations()
    {
        this.CustomerDataTable = new DataTable();
        using (SqlConnection cn = new SqlConnection { ConnectionString = Properties.Settings.Default.ConnectionString })
        {
            using (SqlCommand cmd = new SqlCommand { Connection = cn })
            {
                cmd.CommandText = "SELECT Identifier, CompanyName FROM [Customer]";
                cn.Open();
                this.CustomerDataTable.Load(cmd.ExecuteReader());
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by mega_noob Tuesday, April 18, 2017 8:48 PM
    Saturday, April 15, 2017 10:36 AM
    Moderator
  • Thanks Karen Payne. 

    So would this code force the application to look in the same folder for the LocalDB?

    Will I not need to alter the connection string in App.config?

    Thank you. Very much for taking the time to reply to me. 

    Since you have the connection string in app.config, and if it's like this, DataDirectory will point to the same folder as the executable.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by mega_noob Tuesday, April 18, 2017 8:48 PM
    Saturday, April 15, 2017 5:57 PM
    Moderator
  • Thanks very much for your help. 

    I decided to rewrite my program whilst using an access database. With you and everyone's help and some more googling, I managed to finish my app with the database and had it working on my host machine and my win7 VM. 

    It turns out that the person I'm making the app for has WinXP 32bit and the application I made had .NET 4.5 framework which is no longer supported in XP (correct me if I am wrong). When I changed my framework and debugged it caused me errors. I am rewriting the program now to work in the windows XP machine with .NET 4 framework. I will let you know how I get on. 
    You really helped me understand how the connection string works and gave me a clear understanding of things. 

    Like I said this was my first VB project and through all the errors I have learned so much more. I'm somewhat grateful for the errors, but I'm more so grateful for the help you had given me along with everyone else who had replied in this thread. 
    Like I said, I will finish this project and get it working on XP, then I will move on and do some practice with SQL Database. 

    Once again. Thanks So much. 

    Tuesday, April 18, 2017 12:12 AM
  • If any of the responses in this thread were helpful in resolving the problem then they should be marked as answers.
    Tuesday, April 18, 2017 12:59 AM