c# asp .net 动态创建sql数据库表:
//必须的命名空间
using System;
using System.Data;
using System.Data.SqlClient;
//中间常规内容略
string tabName = "table1";
//声明要创建的表名,你也可以改为从textbox中获取;
string sqlStr = "create table ";
sqlStr += tabName + "( ";
sqlStr += "col0 numeric identity(1,1) primary key,";
//col0为列名,同样可以改为通过从textbox中获取
//identity(1,1)是标记递增种子
//primary key定义主键
sqlStr += "col1 varchar(20),";
sqlStr += "col2 smalldatetime,";
sqlStr += "col3 varchar(20),";
sqlStr += "col4 int,";
sqlStr += "col5 varchar(20),";
sqlStr += " )";
//实例化sql连接
SqlConnection conn = new SqlConnection("server=.; database=dbName; uid=sa; pwd=sa");
conn.Open();
//实例化sql命令
SqlCommand cmd = new SqlCommand(sqlStr, conn);
//执行sql命令
cmd.ExecuteNonQuery();
//关闭连接
conn.Close();
//必须的命名空间
using System;
using System.Data;
using System.Data.SqlClient;
//中间常规内容略
string tabName = "table1";
//声明要创建的表名,你也可以改为从textbox中获取;
string sqlStr = "create table ";
sqlStr += tabName + "( ";
sqlStr += "col0 numeric identity(1,1) primary key,";
//col0为列名,同样可以改为通过从textbox中获取
//identity(1,1)是标记递增种子
//primary key定义主键
sqlStr += "col1 varchar(20),";
sqlStr += "col2 smalldatetime,";
sqlStr += "col3 varchar(20),";
sqlStr += "col4 int,";
sqlStr += "col5 varchar(20),";
sqlStr += " )";
//实例化sql连接
SqlConnection conn = new SqlConnection("server=.; database=dbName; uid=sa; pwd=sa");
conn.Open();
//实例化sql命令
SqlCommand cmd = new SqlCommand(sqlStr, conn);
//执行sql命令
cmd.ExecuteNonQuery();
//关闭连接
conn.Close();
版权声明:《 C# asp .net sql 动态创建表 》为zhangkang原创文章,转载请注明出处!
最后编辑:2019-5-5 20:05:07