SQl中的表创建 [英] Table creation in SQl

查看:66
本文介绍了SQl中的表创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击我页面中的一个按钮时,它应该在sqlserver 2008中创建一个表,,,我该怎么办呢,请帮帮我..

解决方案

< blockquote>尝试动态设置表名。试试这个:

  string  connectionString = ConfigurationManager.ConnectionStrings [   TestConnectionString]。ConnectionString; 
SqlConnection con = new SqlConnection(connectionString);
string sql = CREATE TABLE + txtTableName.Text.Trim()+ + Id int NOT NULL IDENTITY(1,1)PRIMARY KEY, + txtColumn.Text.Trim()+ nvarchar(MAX)NULL;
foreach (控制c .Controls)
{
if (c.Name.Contains( temp) && c TextBox)
{
if (!String。 IsNullOrEmpty(c.Text))
{
sql + = + c.Text.Trim()+ nvarchar(MAX)NULL ;
count ++;
}
}


}
sql + = ;
SqlCommand cmd = new SqlCommand(sql,con);
con.Open();
cmd.ExecuteNonQuery();



查看 Original Source [ ^ ]。





- 发送

when i click a button in my page, it should create a table in sqlserver 2008,,, how can i do it,,please help me..

解决方案

Try putting your table name dynamically. Try this:

string connectionString = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
string sql = "CREATE TABLE " + txtTableName.Text.Trim() + "(" + "Id int NOT NULL IDENTITY (1,1) PRIMARY KEY," + txtColumn.Text.Trim() + " nvarchar(MAX) NULL";
foreach (Control c in this.Controls)
 {
     if (c.Name.Contains("temp") && c is TextBox)
       {
          if (!String.IsNullOrEmpty(c.Text))
           {
             sql += "," + c.Text.Trim() + " nvarchar(MAX) NULL";
             count++;
            }
       }


 }
sql += ")";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
cmd.ExecuteNonQuery();


View Original Source[^].


--Amit


这篇关于SQl中的表创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆