C # query whether a table exists in the MySQL database. If the table does not exist, create the table.

Source: Internet
Author: User

For example, C # queries whether a table named "menutable" exists in the test database. If not, create a menutable table.

Method: Use the method of throwing an exception to check whether the table exists.

 

String myconnectionstring = "Server = localhost; uid = root; Pwd = ******; database = test ";

Mysqlconnection conn = new mysqlconnection (myconnectionstring );
Conn. open ();
Mysqldataadapter ADP = new mysqldataadapter ();
Datatable dt = conn. getschema ();
Try
{
Mysqlcommand cmd = new mysqlcommand ("select * From menutable", Conn );
Cmd. executenonquery ();
}
Catch (mysqlexception ex)
{
Switch (ex. Number)
{
Case 1146:
MessageBox. Show ("The menutable table does not exist. The system is creating it for you! ");
String myselectquery = "create table menutable (name varchar (20), sex char (1 ))";
Mysqlcommand cmd = new mysqlcommand (myselectquery, Conn );
Cmd. executenonquery ();

......
Break;
Default:
MessageBox. Show (ex. Message );
Break;
}
}

 

Beijing besman Information Technology Co., Ltd. Technology Department Feng Huacheng

 

Beijing wireless ordering system catering management system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.