【问题标题】:How to create dynamic tables in asp input by user C# asp.net如何在用户C#asp.net的asp输入中创建动态表
【发布时间】:2014-07-08 16:58:24
【问题描述】:

想创建用户输入的动态表格

【问题讨论】:

  • 您的问题到底是什么?你的html代码在哪里?你已经尝试了什么?
  • 字符串 cs = WebConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString; SqlConnection con = new SqlConnection(cs); string sqlStatement = "创建表 dbo.prince (stuname CHAR(50), stuaddress CHAR(255), stubalance FLOAT)"; con.Open(); SqlCommand sqlCmd = new SqlCommand(sqlStatement, con); sqlCmd.ExecuteNonQuery(); con.Close();
  • 但我的代码,但我想输入表单用户创建动态表列名称等

标签: asp.net sql-server dynamic


【解决方案1】:

您可以动态创建您的 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

【讨论】:

  • 列名由用户输入,类型请指导
  • 如何通过列名这是我的问题,请指导我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-24
  • 2011-10-25
  • 1970-01-01
相关资源
最近更新 更多