C # how to operate SQL dynamically!

Source: Internet
Author: User

Just written to Program You need to add databases and tables to the SQL statements. The information you are looking for on the Internet is not comprehensive. I have sorted it out. The detailed information is as follows:
// __________________ Create a database ____________________
String connectionstring = "Data Source = localhost; Integrated Security = true; pooling = false ";
Sqlconnection conn = new sqlconnection (connectionstring );
Conn. open ();
SQL = "CREATE DATABASE clsky on primary (name = ABC, filename = 'e: \ clsky. MDF ')";
Sqlcommand cmd = new sqlcommand (SQL, Conn );
Cmd. executenonquery ();
MessageBox. Show ("the database is created successfully! ");
//_________________________________________________

// ________________ Create a table ___________________________

String connectionstring1 = "Data Source = localhost; initial catalog = clsky; Integrated Security = true; pooling = false ";
Sqlconnection conn1 = new sqlconnection (connectionstring1 );
Conn1.open ();
SQL = "CREATE TABLE clsky" +
"(ID integer constraint pkeymyid primary key," +
"Name char (50), address char (255), balance float )";
Sqlcommand cmd1 = new sqlcommand (SQL, conn1 );
Statement 1.executenonquery ();
MessageBox. Show ("the database is created successfully! ");
//__________________________________________________

// _______________ Delete a table _____________

SQL = "Drop table clsky ";
Sqlcommand cmd2 = new sqlcommand (SQL, conn1 );
Listen 2.executenonquery ();
MessageBox. Show ("Table deleted! ");
Conn1.close ();
Conn. Close ();
//_________________________________
If you want to delete this database, you can use:
String connectionstring = "Data Source = localhost; Integrated Security = true; pooling = false ";
Sqlconnection conn = new sqlconnection (connectionstring );
Conn. open ();
String SQL = "DROP DATABASE clsky ";
Sqlcommand cmd = new sqlcommand (SQL, Conn );
Cmd. executenonquery ();

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.