Checking if table exists during installation of a plugin.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Hi.
I have a plugin where I give the user the option to keep the table during un-installation. Then the user can keep all of the data and install the plugin at a later date with all the data intact.

The problem is that when the user installs the plugin again then an error is thrown because the database script tries to create a table that already exists.

I have been trying to figure out how to check if the table exists before trying to install the table but to no avail. Currently I simply catch the exception and check if the message contains a certain sentence to ignore that exception or rethrow it. It works but it is ugly.

So the question is, is there any nice method to check for the existence of a table in NopCommerce?

I imagine something like:
Database.TableExists(string nameOfTableToCheck)


Thanks!
6 years ago
It's old, but maybe this still works
https://www.nopcommerce.com/boards/t/38150/data-table-exists.aspx
6 years ago
Ah yes.
That works.
I did not find this post even though I searched the forum.

Thank you.
3 years ago
if Table Exits in the database and we don't want to check again create table script will run
so we can use this below method in 4.1 using in dbcontext class

try
            {
                //create the table
                var dbScript = GenerateCreateScript();
                var dbInstallationScript = string.Empty;
                var goString = Environment.NewLine;
                goString += "GO";
                goString += Environment.NewLine;
                var scriptBlocks = dbScript.Split(';');
                for (var i = 0; i < scriptBlocks.Length; i++)
                {
                    var scriptBlock = scriptBlocks[i].Replace(goString, "").Trim();
                    if (!string.IsNullOrEmpty(scriptBlock))
                    {
                        var firstBracketIndex = scriptBlock.IndexOf('[');
                        var tableName = scriptBlock.Substring(firstBracketIndex, (scriptBlock.IndexOf('(') - firstBracketIndex)).Trim();
                        tableName = tableName.Replace("[", string.Empty).Replace("]", string.Empty);
                        if (!string.IsNullOrEmpty(dbInstallationScript))
                            dbInstallationScript += Environment.NewLine;
                            dbInstallationScript += "IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='" + tableName + "')";
                        dbInstallationScript += Environment.NewLine + "BEGIN" + Environment.NewLine;
                        dbInstallationScript += scriptBlock + ";";
                        dbInstallationScript += Environment.NewLine + "END";
                        dbInstallationScript += Environment.NewLine;
                    }
                }
                dbInstallationScript += "GO";
                this.ExecuteSqlScript(dbInstallationScript);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.