2

I developed C# Word add-in which populates some data from database file. It is working fine while I am running through Visual studio running. But if I publish it cannot connect to database.

Here is how I get data from database:

SQLiteConnection con = new SQLiteConnection(ThisAddIn.connectionString);
            con.Open();

            var command = con.CreateCommand();
            command.CommandText =
            @"
                SELECT *
                FROM JK
                WHERE article_number = $article_number
            ";
            command.Parameters.AddWithValue("$article_number", textFromDoc);

            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var title = reader.GetString(2);
                    var article = reader.GetString(3);

                    ResultForm resultForm = new ResultForm();
                    resultForm.setArticle(title, article);
                    resultForm.ShowDialog();
                }
            }

public static string connectionString = @"Data Source=E:\projects\c#\TBPWordAddin\WordAddIn1\codexes.db";

Am I doing something wrong or do I need to include file in another way? Any help will be appreciated.

Also I tried publishing using Visual studio installer and it connected to database but the add in didnot lounched on other computers.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
Whiteq
  • 427
  • 1
  • 8
  • 18

1 Answers1

1

The connection string contains an absolute path which can be changed after publishing an application. I'd suggest using a relative path instead - in that case you will be able to find the Db easily. You may check out the following threads for more information on such kind of issues:

But if I publish it cannot connect to database.

Make sure the Db path (see the connection string) corresponds to the hardcoded value in the application used.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • I have tried using relative path but no success. Also I pasted db file in My documents folder so I could easily access it. In the debug mode it works but when I publish it it falls – Whiteq Jan 17 '22 at 13:25
  • How do you publish the add-in? Do you use a ClickOnce installer? Where do you install the add-in? – Eugene Astafiev Jan 17 '22 at 13:39
  • Yes by using ClickOnce installer. I have installed on C disk program files folder. But I also used Visual Studio Installer. But on other computers it did not run on others – Whiteq Jan 18 '22 at 05:03
  • It seems you have mixed the installation. You need to use a single installer - ClickOnce or MSI (not two together). – Eugene Astafiev Jan 18 '22 at 21:58