none
Add a database to SQlite project

    Pertanyaan

  • Hi how are you, Sorry I'm using the English forum,but I have not obtained any help in Spanish, so excuse my bad English please

    I've been trying to add a database (already created) to my project (in the Debug folder) but I have not managed to work on another PC other than mine when I install it, I have the dll and everything, but it works only if I install on my pc. This is the error message and my code.

            string connetionString;
            SQLiteConnection connection;
            SQLiteDataAdapter adapter;
            SQLiteCommand cmdQ;
            DataSet ds = new DataSet();
            string Sql;
    
     private void Form1_Load(object sender, EventArgs e)
     {
    
    connetionString = @"Data Source=C:\Folder....\Forde2..\ database.db;Version=3; New=True;Compress=True;";
    connection = new SQLiteConnection(connetionString);
    
       Sql = "select *  from People";
                  
       try
      {
       connection.Open();
       adapter = new SQLiteDataAdapter(Sql, connection);
       adapter.Fill(ds);
       connection.Close();
       dataGridView1.DataSource = ds.Tables[0];
                    }
       catch (Exception ex)
       {
       MessageBox.Show(ex.ToString());
       }       
            }

    I tried changing the connection to "Data Source = database.db; Version = 3; New = True; Compress = True;" , but even in this case it does not even work on my PC, the message in this case is

     So try another way creating database once it is installed (programming the base and tables) and it works after 2 days is all that I managed to work, I attach my current code that works 

    private void Form1_Load(object sender, EventArgs e)
            {
    connetionString = @"Data Source= database.db;Version=3;    New=True;Compress=True;";
                connection = new SQLiteConnection(connetionString);
                if (!File.Exists("./Tomas.db"))
                {
                    string createQuery =    @"CREATE TABLE IF NOT EXISTS
                                              [People] (
                                              [Id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
                                              [Name] NVARCHAR(2048)  NULL,
                                              [Gender] NVARCHAR(2048) NULL )";
    
                    SQLiteConnection.CreateFile("database.db");
                    cmdQ = new SQLiteCommand(connection);
                    connection.Open();
                    cmdQ.CommandText = createQuery;
                    cmdQ.ExecuteNonQuery();
                    cmdQ.CommandText = "INSERT INTO Personas (Name,Gender) values('Jhon','male') ;";
                    cmdQ.ExecuteNonQuery();
                    cmdQ.CommandText = "INSERT INTO Personas (Name,Gender) values('Ana','female') ;";
                    cmdQ.ExecuteNonQuery();
                    connection.Close }
    }
     private void BtnMostrar_Click(object sender, EventArgs e)
            {
                Sql = "select *  from People";
                try
                {
                    connection.Open();
                    adapter = new SQLiteDataAdapter(Sql, connection);
                    adapter.Fill(ds);
                    connection.Close();
                    dataGridView1.DataSource = ds.Tables[0];
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
    I am new to programming I do not understand many things, especially the assembly and I do not have much time to read books about the subject. That's why I use these forums. I would like to not have to program each database and use some manager to create it and paste it into my project. I would like you to tell me. what am I doing wrong? and if it is possible to tell me how to do it or pass me a link that helps me, I have heard something of attaching the database to the project in configurations but I have no idea what it refers to.
    Thank you very much for bothering to read my question

    • Dipindahkan oleh CoolDadTx 15 Oktober 2018 17:36 Third party product
    15 Oktober 2018 15:39

Semua Balasan

  • Hello, 

    It looks like database file can't be open.

    Read this:

    https://www.devart.com/dotconnect/sqlite/docs/Devart.Data.SQLite~Devart.Data.SQLite.SQLiteConnection~ConnectionString.html

    pay special attention to Data Source & FailIfMissing.

    Try to put full database file name, possibly - with "file://" protocol. 

    Second option - permission for folder where DB are stored - account from which running app need read/write/creste/delete access.

    Hmmm... I have noted you use to check "./Tomas.db", but try to work with "database.db". I would recommend for beginning check your PC for existing Tomas.db and database.db - most likely there are a multiple copy - and decide which one required.


    Sincerely, Highly skilled coding monkey.

    15 Oktober 2018 16:07
  • Please post questions related to third party products like SQLite in their forums. They can provide you guidance on getting your connection string set up properly. 

    You can also refer to sites like ConnectionStrings to get help.


    Michael Taylor http://www.michaeltaylorp3.net


    • Disarankan sebagai Jawaban oleh CoolDadTx 15 Oktober 2018 17:34
    • Saran Jawaban dibatalkan oleh CoolDadTx 15 Oktober 2018 17:35
    • Diedit oleh CoolDadTx 15 Oktober 2018 17:36 incomplete post
    15 Oktober 2018 17:34