none
For anyone who has created a windows application with a database RRS feed

  • Question

  • ok I've had quite a lot of problems with trying to connect to a database after installing the application on a remote computer.

     

    I was just wondering if you have any sample code, or point me in the direction of a good tutorial which is easy to understand. Even if you can provide sample code as little as create database, create table, create table adapter etc... but the more detailed the better

     

    Thanks in advance

    Monday, November 19, 2007 2:08 PM

Answers

  • Hi,
    What is the database/connection string that your application uses?  Please refer to http://www.connectionstrings.com/ to see if it's correct.

    Here are some instructions on how to create a basic Windows Forms application that connects to a database.


    First, create a new project and add a database to the project (Project/Add new item/Database (.mdf)). Double click on that database in the solution explorer to open the database explorer / server explorer. Expand the nodes of the db tree to find the Tables node and right-click on it and select New Table. Add the fields that you require. Save the table (I’ll assume you call it Foo).


    Select the database in the solution explorer and go to the property window. Ensure that the Copy to Output Directory setting is set to Copy if newer.


    Next you need to create a dataset. Project/Add new item/Dataset. I'll assume you call it AppData. The dataset will become the data layer of your application.


    In the dataset designer, drag all your tables from the server explorer. The server explorer is accessed from View/Server Explorer. You can add a new data connection to your database (sql server, access, odbc...) if it’s not already there. Save and compile.


    Create a new form. Open the Data Source pane (data/show data source) and you will see your dataset as a datasource. I'll assume you'll create a form for table Foo. Next you need to decide if you want a grid view or a detail view. Click on the Foo table in the data source window and you will see a dropdown appear. If you want a detail view instead of a grid change it with this dropdown.


    Now drag the table to the empty form. You will see that a grid (or text boxes and labels) will be created with each of the fields. A BindingSource object, a binding navigator, a table adapter and an instance of your dataset are also created. Now switch to code view. You will notice in the Form_Load event some code to load the data and on the save toolstrip code to end editing and validate the data.


    Let's suppose you also want to save back the changes to the database. Add this line of code:

    Me.fooTableAdapter.Update(Me.appData.fooTable)


    That's it. Now you have an application that can insert, delete, update and browse all the data in table Foo.


    A whole bunch of good resources, including numerous books, that could really help you out:


    ASP.NET 2.0: http://quickstarts.asp.net/QuickStartv20/aspnet/

    Winforms: http://www.windowsforms.net/ and http://samples.gotdotnet.com/quickstart/winforms/

    The online MSDN Developer's Guide for Windows Form Applications is also indispensible:

    http://msdn2.microsoft.com/en-us/library/ms644558.aspx

    They will all teach you the basics of databinding and data access.


    Regards,
    Charles


    Sunday, December 2, 2007 7:42 PM

All replies

  •  

    is this the way most people agree is the best way... it's just people will be able to just go into the C:\ and change the database, also what if they have assign their 'C:\' drive letter to something else?
    Monday, November 19, 2007 3:07 PM
  • Hi,
    What is the database/connection string that your application uses?  Please refer to http://www.connectionstrings.com/ to see if it's correct.

    Here are some instructions on how to create a basic Windows Forms application that connects to a database.


    First, create a new project and add a database to the project (Project/Add new item/Database (.mdf)). Double click on that database in the solution explorer to open the database explorer / server explorer. Expand the nodes of the db tree to find the Tables node and right-click on it and select New Table. Add the fields that you require. Save the table (I’ll assume you call it Foo).


    Select the database in the solution explorer and go to the property window. Ensure that the Copy to Output Directory setting is set to Copy if newer.


    Next you need to create a dataset. Project/Add new item/Dataset. I'll assume you call it AppData. The dataset will become the data layer of your application.


    In the dataset designer, drag all your tables from the server explorer. The server explorer is accessed from View/Server Explorer. You can add a new data connection to your database (sql server, access, odbc...) if it’s not already there. Save and compile.


    Create a new form. Open the Data Source pane (data/show data source) and you will see your dataset as a datasource. I'll assume you'll create a form for table Foo. Next you need to decide if you want a grid view or a detail view. Click on the Foo table in the data source window and you will see a dropdown appear. If you want a detail view instead of a grid change it with this dropdown.


    Now drag the table to the empty form. You will see that a grid (or text boxes and labels) will be created with each of the fields. A BindingSource object, a binding navigator, a table adapter and an instance of your dataset are also created. Now switch to code view. You will notice in the Form_Load event some code to load the data and on the save toolstrip code to end editing and validate the data.


    Let's suppose you also want to save back the changes to the database. Add this line of code:

    Me.fooTableAdapter.Update(Me.appData.fooTable)


    That's it. Now you have an application that can insert, delete, update and browse all the data in table Foo.


    A whole bunch of good resources, including numerous books, that could really help you out:


    ASP.NET 2.0: http://quickstarts.asp.net/QuickStartv20/aspnet/

    Winforms: http://www.windowsforms.net/ and http://samples.gotdotnet.com/quickstart/winforms/

    The online MSDN Developer's Guide for Windows Form Applications is also indispensible:

    http://msdn2.microsoft.com/en-us/library/ms644558.aspx

    They will all teach you the basics of databinding and data access.


    Regards,
    Charles


    Sunday, December 2, 2007 7:42 PM