SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

Database

Last post 03-14-2017 4:30 AM by Robert Blanton. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 05-20-2008 11:54 AM

    • Donk
    • Not Ranked
    • Joined on 05-20-2008
    • Posts 3

    Database

    Hi there.

    I started programming in C# using SharpDevelopment.

    Now I would like to create a database or use the existing db.mdb.

    Since I never did this before, I am asking for a noob explanation on setting this up.

    The database should be in the same directory as the executable and contain several subdatabases.

     It would be great if you could help me

     

    Donk 

  • 05-20-2008 1:05 PM In reply to

    Re: Database

    Hi Donk,

    here are some parts of an answer:

    Working with databases uses ADO.NET, that means you must use the classes of the System.Data namespace to representate your data inside your application and the classes of the System.Data.Common namespace to connect with the database outside your application. Additionally, you need a specific DbProvider to connect with a specific DBMS, e.g. FirebirdSql.Data.FirebirdClient. A simple example:

    //  create a DataSet ds in your application:
    DataSet ds = new DataSet("MyDataSet");
    //  fill in the required data:
    using(FbConnection conn = new FbConnection
            ("User=SYSDBA;Password=masterkey;Database=SampleDatabase.fdb;DataSource=localhost;")) {
        FbDataAdapter da = new FbDataAdapter("SELECT * FROM MyTable WHERE ID < 100)", conn);
        da.Fill(ds, "SelectLowerIDs");
    }

    How to connect to a specific database, take a look at http://www.connectionstrings.com/default.aspx with links to the required or recommended NET Provider.

    In most cases, you can't create a database directly from an application. (Of course, you can use DbCommand.CommandText = "CREATE DATABASE ..."; but you can't connect to a non-existing database.) To create a database, you should use a DB management tool.

    I'm sorry, there is no support to use databases via IDE of #D at the moment. There are some developers to include it, e.g. for SharpReport. But you can use all database work manually coded. In many cases, this is simplier than with working in IDE.

    "The database should be in the same directory as the executable"
    You should not do this. Application and Data should always be separated. In Vista, a normal user has no rights to write in the app directory. For a standard way, you can write the ConnectionString in the app.config (not using the user and password, of course).

    "and contain several subdatabases."
    I don't know what a subdatabase is. Do you mean "tables"?

    Juergen 

  • 05-20-2008 1:33 PM In reply to

    • Donk
    • Not Ranked
    • Joined on 05-20-2008
    • Posts 3

    Re: Database

    Thanks for the quick answer.

    Probably I should write a bit more about my problems: So far I have

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Data.OleDb;

    namespace Maxi
    {
        public partial class MainForm : Form
        {
            public OleDbConnection mycon;
            public MainForm()
            {
                mycon=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/db.mdb;Persist Security Info=False");
                InitializeComponent();
            }
        }
    }

    If I set the Data Source to c:\db.mdb I get an error message.

    The database should be in the same directory as the executable"
    You should not do this. Application and Data should always be separated. In Vista, a normal user has no rights to write in the app directory. For a standard way, you can write the ConnectionString in the app.config (not using the user and password, of course).
    Acutally it does not matter for now, where the database is located.

     

    Now I want to go to db.mdb table:"Kunden" and get the name from the first customer.
    I created a button (b_find) and a label (l_output ). I want the button to show the name in the label.
            void b_findClick(object sender, System.EventArgs e)
            {
                   string s = nameFromDB;
                   l_output.text=s;

            }


  • 05-20-2008 2:28 PM In reply to

    Re: Database

    "If I set the Data Source to c:\db.mdb I get an error message."
    What error? In C#, you must write double backslash instead of one.

    Are you German native speaker? (I suggest because of table "Kunden".) Then you can read the OpenBook http://www.galileocomputing.de/openbook/visual_csharp/index.htm, especially ch.26 for handling databases. (Replace all Sql classes with your OleDb classes.) A wonderful forum is http://www.myCSharp.de.

    You shouldn't create the DbConnection in the form constructor. Most of the ADO.NET instances should exist for a short time. Therefore I created the connection in a using block: create the connection, fetch the data, close and dispose the connection - that's the correct way.

    How to choose the data? Your Click event can call a method GetData("SELECt ..."); this method contains my simple example.

    To get one string out of the database, needs a more complex way. You must create an SQL command and send it via connection to the database - either by DbDataAdapter (my example) or by DbDataReader or by DbCommand.ExecuteScalar. A DbDataAdapter fills the data into a DataTable; a DbDataReader is used to read different values directly; ExecuteScalar returns a single value.

    You must always remember that database handling needs complex interaction. Juergen

  • 05-20-2008 4:55 PM In reply to

    • Donk
    • Not Ranked
    • Joined on 05-20-2008
    • Posts 3

    Re: Database

     Thanks.

    The first problem is solved using \\.

    I am reading through the stuff at the moment (thanks for the link).

    The DataSet is something I can find in the list of items, when I go to the Design section of SharpDevelop.
    What do I have to fill in there to see the database?

  • 05-20-2008 5:37 PM In reply to

    Re: Database

    Hi Torsten, 

    "DataSet ... What do I have to fill in there to see the database?"

    That isn't possible. I forgot the separated handling of data with ADO.NET: DataSet and DataTable representant the data in memory, the database representants the data on storage. These regions have to be linked by DbConnection and the other System.Data.Common classes I mentioned above.

    Juergen 

    PS. This forum wants to help using the #D IDE, but not for C# or the NET Framework in general. Because #D doesn't support database handling at the moment, you'ld better ask in a more general forum.

  • 03-14-2017 4:30 AM In reply to

    Re: Database

    This is an old post but only recently have found sharpdevelop ide so I will respond since this thread has come up in my google search.  You mention that #D doesn't support DB in the IDE.  Is still the case today?  I am finding it difficult to use sqlite db but not impossible.  The sqliteconnection component produces a design error when being dropped on the form.  Since sqlite is the most used db in the world it seems this db should be supported.  Can anyone provide some links to examples for using sqlite in sharpdevelop ide?  Thanks.

Page 1 of 1 (7 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.