如果表不存在,为什么我的代码不起作用?C# - MYSQL - ASP.NET

pftdvrlh  于 2023-10-15  发布在  Mysql
关注(0)|答案(1)|浏览(81)

我正在尝试学习使用C#,asp.net,mysql作为数据库的REST apis。我想开个网上书店。下面的代码是我试图检查我的数据库中是否存在一个book_details表。如果我运行它,它就能很好地告诉我它的存在。但是,如果我将行string tableName = "book_details";改为string tableName = "old_books";,因为我想测试if the table DOES NOT exist的功能,请创建它。它直接进入我的return new JsonResult("Connection was never established");。我好像想不通。请让我知道如果更多的细节需要适当的帮助。

DataTable table = new DataTable();
            string sqlDataSource = _configuration.GetConnectionString("EmployeeAppCon");
            MySqlDataReader myReader;
            using (MySqlConnection mycon = new MySqlConnection(sqlDataSource))
            {
                string tableName = "book_details";
                string query = $"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '{mycon.Database}' AND table_name = '{tableName}'";

                mycon.Open();
                try {
                    using (MySqlCommand myCommand = new MySqlCommand(query, mycon))
                    {
                        int count = Convert.ToInt32(myCommand.ExecuteScalar());

                        if (count > 0)
                        {
                            return new JsonResult("Table Exists");
                        }

                        myReader = myCommand.ExecuteReader();
                        table.Load(myReader);

                        myReader.Close();
                        mycon.Close();
                    }
                } 
                catch {
                    string newQuery = $"CREATE TABLE '{tableName}' (    " +
                        $"ISBN int NOT NULL," +
                        $"BookName varchar(200)," +
                        $"BookDescription varchar(1000)," +
                        $"Price decimal(4,2)," +
                        $"Author varchar(150)," +
                        $"Genre varchar(100)," +
                        $"Publisher varchar(150)," +
                        $"YearPublished int(255)," +
                        $"CopiesSold int(255)," +
                        $"PRIMARY KEY (ISBN));'";

                    using (MySqlCommand myNewCommand = new MySqlCommand(newQuery, mycon))
                    {
                        myNewCommand.ExecuteNonQuery();

                        myReader = myNewCommand.ExecuteReader();
                        table.Load(myReader);

                        myReader.Close();
                        mycon.Close();

                        return new JsonResult("Table Doesn't Exist, it has been created.");
                    }
                }

                return new JsonResult("Connection was never established");
            }
x6492ojm

x6492ojm1#

您正在尝试在查询的错误处理(catch)分支中创建表,该分支检查表是否存在。然而,如果表不存在,那么executescaral将简单地返回0,因为这是一个完全有效的结果,而不是错误。
您必须将创建表的代码移动到if (count > 0)的else分支中:

if (count > 0)
              {
                  return new JsonResult("Table Exists");
              }
else
              {
                  //create table code
              }

相关问题