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