✔ 最佳答案
方法一
protected void Button1_Click(object sender, EventArgs e)
{
string x = TextBox1.Text;//將輸入字串變成字串變數
SqlConnection Conn = new SqlConnection(connection_string);
Conn.Open();
SqlCommand cmd = Conn.CreateCommand();
string sql = "CREATE TABLE [dbo].[" + x + "] (" +
"[id] [int] IDENTITY(1,1) NOT NULL," +
"[背號] [int] NULL," +
"[姓名] [nchar](10) NULL," +
"[位置] [nchar](10) NULL," +
"[先發] [int] NULL," +
"[三分球試投] [int] NULL," +
"[三分球試投(中)] [int] NULL," +
"[兩分球試投] [int] NULL," +
"[兩分球試投(中)] [int] NULL )";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
方法二
先在 SQL Server 裏建立一個表單的模板,TempTable
protected void Button1_Click(object sender, EventArgs e)
{
string x = TextBox1.Text;//將輸入字串變成字串變數
SqlConnection Conn = new SqlConnection(connection_string);
Conn.Open();
SqlCommand cmd = Conn.CreateCommand();
string sql = "select * into " + x + " from TempTable";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
方法二新表單會建立 identity 欄,但不會設預設值 (default value),設預設值加以下程式
sql = "ALTER TABLE " + x + " ADD " +
"CONSTRAINT [DF_" + x + "_三分球試投] DEFAULT (0) FOR [三分球試投]";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();