dimanche 29 novembre 2015

Table Not saved in SQLite database

I need to create a .exe file for a project and I use SQLite database embeded into my project.

click here to know the steps that i followed to embed the database to my project

now I used following query to create the table.

cmd.CommandText = "CREATE TABLE TaskTable (UserName varchar(100), PassWord varchar(100));"; cmd.ExecuteNonQuery();

But the table does not seem to be saved once I close the project. This is confirmed because if i reopen the project and try to insert new values using the following command

cmd.CommandText = "INSERT INTO TaskTable (UserName,PassWord)

values ('" + textBox1.Text + "','" + textBox2.Text + "')";

I got the exception saying table does not exist. (here I have commented the crete table command while running the solution assuming that I table is already creted on the first run)

public partial class Form2 : Form {

    string connectionString;
    public Form2()
    {
        InitializeComponent();
        connectionString = @"Data Source=database.db;Version=3;New=True;Compress=True;";
    }

    private void button1_Click(object sender, EventArgs e)
    {

        using (SQLiteConnection sqlite_conn = new SQLiteConnection(connectionString))
           {

               try
               {
                   SQLiteCommand cmd = sqlite_conn.CreateCommand();
                   sqlite_conn.Open();


                   cmd.CommandText = "CREATE TABLE  TaskTable (UserName varchar(100), PassWord varchar(100));";
                   cmd.ExecuteNonQuery();


                   cmd.CommandText = "INSERT INTO TaskTable (UserName,PassWord) values ('" + textBox1.Text + "','" + textBox2.Text + "')";



                   int i = cmd.ExecuteNonQuery();


                   if (i == 1)
                   {

                       MessageBox.Show("User created successfully....");
                   }
               }

               catch (Exception ex)
               {
                   MessageBox.Show(ex.Message);
               }

           }

please modify and complete the code. Table should be created only once and should be permanently stored in db.

Aucun commentaire:

Enregistrer un commentaire