Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
Need help with this piece of code 
....
Whether the table exists or not I still have Message 2 coming.
How do I know if a table exists?

thank you,


What I have tried:

C#
Program.Connex_Bdd.Open();
   var query_ttle = "SELECT count(*) FROM sqlite_master WHERE type='table' AND name='My_Tble' COLLATE NOCASE";
   var cmd = new SQLiteCommand(query_ttle, Program.Connex_Bdd);
   int i = 0;
   i = int.Parse(cmd.ExecuteScalar().ToString());

   if (i > 0)
   {
       MessageBox.Show("1");
   }
   else
   {

       MessageBox.Show("2");
   }

   Program.Connex_Bdd.Close();
Posted
Updated 1-Apr-20 2:56am
Comments
ZurdoDev 1-Apr-20 10:01am    
What is your question? You have code that looks like it works so what are you asking us?
Richard Deeming 1-Apr-20 12:15pm    
Have you double-checked the spelling of your table name?

Is it a real table, or a temporary table?

1 solution

Use the debugger to see exactly what is being returned on the call to ExecuteScalar .
 
Share this answer
 
Comments
LSB71 1-Apr-20 9:34am    
Hello Richard,
query_ttle "SELECT count(*) FROM sqlite_master WHERE type='table' AND name='My_Tble' COLLATE NOCASE" string
cmd {System.Data.SQLite.SQLiteCommand} System.Data.SQLite.SQLiteCommand
i 0 int
Whether the table is present or not, i = 0 !!!
Richard MacCutchan 1-Apr-20 12:36pm    
Check that your type is correct.

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