.net 动态创建sql数据库表
程序员文章站
2023-08-23 10:56:51
//必须的命名空间
using system;
using system.data;
using system.data.sqlclient;
//中间常规内容略
string tabname =...
//必须的命名空间
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 += " )";
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();
上一篇: 阳必吃的小吃有哪些?岳阳还有什么特产呢?
下一篇: 中秋节为什么吃月饼?中秋节有哪些美食?