Reading .db file

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2410 Views - Last Post: 20 November 2015 - 10:36 AM Rate Topic: -----

#1 Brandon222   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 159
  • Joined: 16-November 08

Reading .db file

Posted 05 November 2015 - 01:06 PM

I am just looking for an example of someone reading a .db file in C# on visual studios. I have done it before using access files but never a db file. Does anyone have an example of how I can connect this?
Is This A Good Question/Topic? 0
  • +

Replies To: Reading .db file

#2 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Reading .db file

Posted 05 November 2015 - 01:13 PM

What program created this ".db" file? What's in it? File extensions are relatively meaningless; you can make them whatever you want. The question is, what format is this file in? Is it SQLite? Is it an old Access file? What kind of database?

Regardless, you'd access it pretty much the same way. Check out ConnectionStrings.com to get an idea.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Reading .db file

Posted 05 November 2015 - 01:13 PM

Do you know what kind of file it is? I could be sqlite but there are others.

If you confirm that it is sqlite then you would start searching ".net sqlite".
Was This Post Helpful? 0
  • +
  • -

#4 Brandon222   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 159
  • Joined: 16-November 08

Re: Reading .db file

Posted 05 November 2015 - 01:15 PM

Sorry I should have mentioned it is an SQLite Database.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Reading .db file

Posted 05 November 2015 - 01:16 PM

So first searching ".net sqlite" I'm sure there is plenty of information and examples/tutorials.

This post has been edited by andrewsw: 05 November 2015 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Reading .db file

Posted 05 November 2015 - 01:48 PM

Agree with Andrew, the first thing you should do when you have a question like this is search ".NET whatever" or "C# whatever".

In this case, a search of "c# sqlite" gives this as the first result:

https://system.data..../www/index.wiki

Which is exactly the component you need to use.

Then, you can search "C# Example System.Data.Sqlite" and get a tutorial like this:

http://blog.tigranga...-in-c-part-one/

But one thing you'll notice, the code in that tutorial looks a lot like standard database stuff in C#. They're all designed that way, so that querying any database is as similar as possible. In fact, all of the built-in Database objects as well as many of the third-party ones like Sqlite and MySQL extend the System.Data.Db* objects. Example: System.Data.SQLiteCommand is a System.Data.DbCommand. So is System.Data.SqlCommand and System.Data.OleDbCommand. They all have the same external interface; their internals work differently.

So I suggest you also read the tutorial in my signature about how to query databases. Mix and match. Use the techniques I show, with the objects you've learned from the other tutorial.

Also don't forget about Nuget. You don't need to manually download and add the reference, you can use the Nuget Package Manager to download and reference the package:

https://www.nuget.or...em.Data.SQLite/
Was This Post Helpful? 0
  • +
  • -

#7 Brandon222   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 159
  • Joined: 16-November 08

Re: Reading .db file

Posted 05 November 2015 - 02:49 PM

So I copied this code and just commented out the lines I don't need and removed the ability to create a new file. I than swapped the database file for the one I already have created. But I am getting an error when trying to open the file. It is telling me that the file is unsupported.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Finisar.SQLite;

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // [snip] - As C# is purely object-oriented the following lines must be put into a class:

            // We use these three SQLite objects:
            SQLiteConnection sqlite_conn;
            SQLiteCommand sqlite_cmd;
            SQLiteDataReader sqlite_datareader;

            // create a new database connection:
            sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;");

            // open the connection:
            sqlite_conn.Open();

            // create a new SQL command:
            sqlite_cmd = sqlite_conn.CreateCommand();

            // Let the SQLiteCommand object know our SQL-Query:
            //sqlite_cmd.CommandText = "CREATE TABLE test (id integer primary key, text varchar(100));";

            // Now lets execute the SQL ;D
            //sqlite_cmd.ExecuteNonQuery();

            // Lets insert something into our new table:
            //sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (1, 'Test Text 1');";

            // And execute this again ;D
            //sqlite_cmd.ExecuteNonQuery();

            // ...and inserting another line:
            //sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (2, 'Test Text 2');";

            // And execute this again ;D
            //sqlite_cmd.ExecuteNonQuery();

            // But how do we read something out of our table ?
            // First lets build a SQL-Query again:
            sqlite_cmd.CommandText = "SELECT * FROM tickets";

            // Now the SQLiteCommand object can give us a DataReader-Object:
            sqlite_datareader = sqlite_cmd.ExecuteReader();

            // The SQLiteDataReader allows us to run through the result lines:
            while (sqlite_datareader.Read()) // Read() returns true if there is still a result line to read
            {
                // Print out the content of the text field:
                //System.Console.WriteLine(sqlite_datareader["text"]);
                string Myreader = sqlite_datareader.GetString(0);
                MessageBox.Show(Myreader);
            }

            // We are ready, now lets cleanup and close our connection:
            sqlite_conn.Close();
        }
    }
}



By the way the db file is an SQLite and it is created by spiceworks.com
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Reading .db file

Posted 05 November 2015 - 03:10 PM

You should post the full error details.

I would do a quick test with Version=2, and provide the full path. reference

I would also consider installing DB Browser to see if the file can be read.

This post has been edited by andrewsw: 05 November 2015 - 03:11 PM

Was This Post Helpful? 0
  • +
  • -

#9 Brandon222   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 159
  • Joined: 16-November 08

Re: Reading .db file

Posted 05 November 2015 - 03:16 PM

I opened the file with SQLIteBrowser and it opens fine.

The error I get is as follows:

An unhandled exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll

Additional information: unsupported file format
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Reading .db file

Posted 05 November 2015 - 03:22 PM

What did you discover when searching "sqlite unsupported file format"?
Was This Post Helpful? 0
  • +
  • -

#11 Brandon222   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 159
  • Joined: 16-November 08

Re: Reading .db file

Posted 05 November 2015 - 05:20 PM

ok seems the reference was outdated and needed to be changed from using Finisar.SQLite; to using System.Data.SQLite;. Thanks for the help!!!

One last question if you dont mind. Right now it displays the information through a message box. I want to come up in a gridview. How would I go about doing that?
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Reading .db file

Posted 05 November 2015 - 06:15 PM

View PostBrandon222, on 06 November 2015 - 12:20 AM, said:

One last question if you dont mind. Right now it displays the information through a message box. I want to come up in a gridview. How would I go about doing that?

The message isn't getting through. It is a requirement for you to demonstrate effort. Do some searching, find articles and tutorials. Obviously displaying data in a DataGridView is a very common requirement and there is a mountain of information for you to find.

Post a specific question if and when you get stuck.
Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Reading .db file

Posted 06 November 2015 - 07:56 AM

Like I said earlier, the components are all derived from a common set of database classes. You don't need specific examples of querying Sqlite into a DataGridView; you can look at any database tutorial that uses a DataGridView and leverage that code. Just use the Sqlite objects instead of the System.Data.SqlClient ones.
Was This Post Helpful? 0
  • +
  • -

#14 Brandon222   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 159
  • Joined: 16-November 08

Re: Reading .db file

Posted 20 November 2015 - 08:21 AM

Alright so after a long pause and than a lot of research I got it to work as I want my next question is and I have search all over google and havnt been able to figure it out. I am trying to split fields in my gridview into 2 or 3 seperate rows. For example:

Subject | Time

aaa ccc | bbb

ccc eee | ddd

I want it to end up like this


T1 | T2 | Time

aaa | ccc | bbb

ccc | eee | ddd


I know I can do this through SQL functions I would just have to create one, but how would a create and call it? Or if anyone knows a simpler way than please let me know.

This is my code:

 System.Data.OleDb.OleDbConnection con;
            System.Data.OleDb.OleDbDataAdapter da;
            con = new System.Data.OleDb.OleDbConnection();
            con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"
            + "Data Source=C:\\Users\\brand_000\\Desktop\\Emails.mdb";
            con.Open();
            string split_com1 = "CREATE [AGGREGATE] FUNCTION function_name RETURNS {STRING|INTEGER|REAL|DECIMAL}";
            string split_com2 = "CREATE FUNCTION SPLIT_STR(x VARCHAR(255), delim VARCHAR(12), pos INT) RETURNS VARCHAR(255) RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos), LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),delim, '')";

            DataTable t = new DataTable();
            da = new System.Data.OleDb.OleDbDataAdapter(split_com1, con);
            da.Fill(t);
            da = new System.Data.OleDb.OleDbDataAdapter(split_com2, con);
            da.Fill(t);

            string sql = "SELECT SPLIT_STR ('Subject', ':', 1) As M1, SPLIT_STR ('Subject', '-', 2) As M2, SPLIT_STR ('Subject', '-', 3) As M3 From Emails Where Subject Like '%Stopped%'";
            //string sql = "SELECT Subject, Time_Recieved From Emails Where Subject Like '%Stopped%'";
            da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
            da.Fill(t);
            GridView1.DataSource = t;
            GridView1.DataBind();
            con.Close();


Was This Post Helpful? 0
  • +
  • -

#15 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Reading .db file

Posted 20 November 2015 - 08:51 AM

Now I'm confused. Is this an Access database or a Sqlite database? Originally you said it was a Sqlite DB file, now you're using the JET engine to query it?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2