Create a new table for the database

Source: Internet
Author: User

There are two ways to create a new table for the database,

1.We can useAdo. netWrite and executeT-SQLStatement to create a table:

 Private void createtablebtn_click (Object sender, system. eventargs E)

{

//Open connection

If (conn. State = connectionstate. open)

Conn. Close ();

Connectionstring = "Integrated Security = sspi;" +

"Initial catalog = mydb;" +

"Data Source = localhost ;";

Conn. connectionstring = connectionstring;

 

Conn. open ();

 

SQL = "CREATE TABLE mytable" +

"(Myid integer constraint pkeymyid primary key," +

"Myname char (50), myaddress char (255), mybalance float )";

Cmd = new sqlcommand (SQL, Conn );

Cmd. executenonquery ();

}

 

 

2.We can referenceSMOLibrary usageSMOFunction to create a table

Private void createtablebtn_click (Object sender, system. eventargs E)

{

//Create a database server

String connectionstring = "...";

Sqlconnection connection =

New sqlconnection (connectionstring );

Server =

New server (New serverconnection (connection ));

 

//Create a table in my personal database

Database DB = server. Databases ["mydb"];

//CreateTesttableNew table

Table newtable = new table (dB, "testtable ");

//Add primary keyIDColumn

Column idcolumn = new column (newtable, "ID ");

 

Idcolumn. datatype = datatype. Int;

Idcolumn. nullable = false;

Idcolumn. Identity = true;

Idcolumn. identityseed = 1;

Idcolumn. identityincrement = 1;

 

 //Add"Title"Column

Column titlecolumn = new column (newtable, "title ");

Titlecolumn. datatype = datatype. varchar (50 );

Titlecolumn. nullable = false;

 

//IsTableAdd object Column

Newtable. Columns. Add (idcolumn );

Newtable. Columns. Add (titlecolumn );

 

//Create a primary key index for the table

Index = new index (newtable, "pk_testtable ");

Index. indexkeytype = indexkeytype. driprimarykey;

 

//Primary key indexes include1Column"ID"

Index. indexedcolumns. Add (New indexedcolumn (index, "ID "));

 

//Add a new index to the table.

Newtable. Indexes. Add (INDEX );

 

//Create a table in the database

Newtable. Create ();

}

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.