Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a simple form with one button and two texrfield. i want to save data from the textfields to SQLite database by pressing the button. i created SQLite database and i wrote my code as below. when i run the code from Visual studio (Start), it works correctly But if i create a setup file from my project and installed it, then i can not even open the software which i published! when i double click on the icon of my software, nothing will happen! (it doesn't open!).I know The problem is ONLY because of SQLite part because if i delete the code from SQLite and install the setup file, the software Run correctly (but without database!). Does anyone know how can i fix this problem? i also uploaded my project in google drive below, if it helps... Thanks

simpleDatabase8 SQLite - Test.7z - Google Drive[^]

What I have tried:

<pre>and this is again my connection code:
<pre> public static SQLiteConnection con = new SQLiteConnection("Data Source= C:\\Users\\...\\sqliteDB.db3");

    private void button1_Click(object sender, EventArgs e)
    {


        con.Open();
        SQLiteCommand cmd = new SQLiteCommand("insert into [Table](name,code) VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "')", con);


        cmd.Parameters.AddWithValue("@name", textBox1.Text);
        cmd.Parameters.AddWithValue("@code", textBox2.Text);

        SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
        cmd.ExecuteNonQuery();

        con.Close();
        MessageBox.Show("Inserted");

    }
Posted
Updated 11-Dec-19 22:47pm

1 solution

Two major things wrong here:

1) Don't hard code connections - always read them from a configuration file, or you have to change you app in many places each time you release it - and that menas releasing untested code which is going to fail one day, or developing against a "live" database which is much, much worse. When you make a mistake in yoru code, you can mess up the production database far, far too easily. Always use a config file, so you change the connection in one location.

2) Never use strings to build a SQL command with parameter values. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you use strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

I'd also suggest you have a look here: Where should I store my data?[^] and use that to locate the SQLite DB - you can use the same code in your setup program to copy the file there.
 
Share this answer
 
Comments
Member 10943256 12-Dec-19 5:01am    
Thanks for your good information.
about the first tip, what is the address of my configuration file?
i could't find it.
Thanks
OriginalGriff 12-Dec-19 5:35am    
How do you expect me to know?
Google "Configuration file" and the type of your app and see what it comes back with. At a guess, you don't have one - or aren't aware you do - so look at the results to find out how to use one with your environment (Winapps and Web use totally different config mechanisms for example).
Member 10943256 12-Dec-19 5:40am    
I thought you saw my windows application!
i uploaded it on google drive and shared it above the text :"What I have tried:"
https://drive.google.com/file/d/1XV0GtTODcVJQW1gyI6xvZXUWQ1JvS7ua/view?usp=sharing
OriginalGriff 12-Dec-19 5:56am    
Nope. Very few of us go off to random sites to wade through gawd-knows-how-much code in search of anything that might be relevant. We're all volunteers, and this cuts into actual paying work. We're not going to waste our time wading through your code because you can't be bothered to show us just the relevant bits.
Member 10943256 12-Dec-19 5:43am    
i guess, i should add the database physically to my visual studio first, then i can see Configuration file, ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900