.net dynamically created sql database tables

// namespace must be
a using System;
a using the System.Data;
a using the System.Data.SqlClient;
// middle conventional content Skip
String tabName = "table1";
// statement to create the table name, you can also change from textbox obtaining;
String sqlstr = "Create Table";
sqlstr tabName + + = "(";
sqlstr + = "COL0 numeric Identity (1,1) Primary Key,";
// COL0 name of a column, it can also be changed by the textbox acquired
// identity (1,1) is labeled incremented seed
// primary key defines the primary key
sqlstr + = "col1 VARCHAR (20 is),";
sqlstr + = "col2 smalldatetime,";
sqlstr + = "col3 VARCHAR (20 is ), ";
sqlstr + =" int COL4, ";
sqlstr + =" COL5 VARCHAR (20 is), ";
sqlstr = +") ";

// Instantiate sql connecting
the SqlConnection the SqlConnection Conn new new = ( "Database Server .; = = dbName; UID = SA; pwd = SA");
conn.Open ();
// instantiate sql command
SqlCommand cmd = new SqlCommand (sqlStr , Conn);
// execute sql command
the cmd.ExecuteNonQuery ();
// close the connection
conn.Close ();

Guess you like

Origin blog.csdn.net/love33jing2012/article/details/37502835