想要创建用户输入的动态表
答案 0 :(得分:0)
您可以动态创建SQL查询。
示例
Table name: <asp:TextBox id="TextBox1" runat="server" />
Columns: <asp:TextBox id="TextBox2" TextMode="multiline" runat="server" />
代码隐藏中的
string tableName = TextBox1.Text;
string[] lines = TextBox2.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
string cs = WebConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString; SqlConnection con = new SqlConnection(cs);
string sqlStatement = "CREATE TABLE dbo."
+ tableName
+ " (" + string.Join(",", lines) + ")";
con.Open();
SqlCommand sqlCmd = new SqlCommand(sqlStatement, con);
sqlCmd.ExecuteNonQuery();
con.Close();
其中textbox1是表格的名称,例如Table1和textbox2是列表及其类型,例如
col1 nvarchar(30)
col2 int
col3 text