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 ();
}