简体   繁体   English

如何从正在运行的 asp.net 网站上的 SQL 表中更改列名?

[英]How can I change a column name from a SQL table on a running asp.net website?

I have created a website with ASP.NET which is connected to a SQL database.我创建了一个带有 ASP.NET 的网站,该网站连接到 SQL 数据库。

The website shows a table of this with the help of a grid view.该网站在网格视图的帮助下显示了一个表格。

My target is that the user can add to this table a Column and give this a name.我的目标是用户可以向该表添加一个列并为其命名。 That with the help of a TextBox and a button.在文本框和按钮的帮助下。

I am so far that I can add the table a column with a button click but I don't know how I can give the column a name with the TextBox到目前为止,我可以通过单击按钮为表格添加一列,但我不知道如何使用 TextBox 为列命名

private void disp_data()
{
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select * from table1";
    cmd.ExecuteNonQuery();
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);                                                
    da.Fill(dt);
    GridView3.DataSource = dt;
    GridView3.DataBind();
}

---Try1 ---尝试1

protected void AddRow_Click(object sender, EventArgs e)
{

    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;         
    cmd.CommandText = "ALTER TABLE table1 ADD '"+TextBox3.Text+"' VARCHAR(50) NULL;";            
    cmd.ExecuteNonQuery();

    disp_data();
}

---Try2 ---尝试2

protected void AddRow_Click(object sender, EventArgs e)
{

    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    string Columnname = Convert.ToString(TextBox3.Text);
    cmd.CommandText = "ALTER TABLE table1 ADD @CName VARCHAR(50)   NULL;";
    cmd.Parameters.AddWithValue(@"CName", Columnname);
    cmd.ExecuteNonQuery();

    disp_data();
}

protected void AddRow_Click(object sender, EventArgs e)
{
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;         
    cmd.CommandText = "ALTER TABLE table1 ADD NewColumn VARCHAR(50)        NULL;";            
    cmd.ExecuteNonQuery();

    disp_data();
}                           // This works

---Try1 ---尝试1

System.Data.SqlClient.SqlException: "Incorrect syntax near 'Textboxcontent'." System.Data.SqlClient.SqlException:“'Textboxcontent' 附近的语法不正确。”

---Try2 ---尝试2

System.Data.SqlClient.SqlException: "Incorrect syntax near '@CName'." System.Data.SqlClient.SqlException:“'@CName' 附近的语法不正确。”

I wouldn't suggest Try 1 since is is vulnerable to an SQL Injection.我不建议尝试 1,因为它容易受到 SQL 注入的影响。 Try 2 didn't work because you use a verbatim string instead of using @CName as string.尝试 2 不起作用,因为您使用逐字字符串而不是使用@CName作为字符串。

protected void AddRow_Click(object sender, EventArgs e) {

  SqlCommand cmd = con.CreateCommand();
  cmd.CommandType = CommandType.Text;
  cmd.CommandText = "ALTER TABLE table1 ADD @CName VARCHAR(50)   NULL;";
  cmd.Parameters.AddWithValue("@CName", TextBox3.Text);
  cmd.ExecuteNonQuery();
}

I had to add "Replace" to make @CNAME useful, now it works我必须添加“替换”以使 @CNAME 有用,现在它可以工作了

protected void AddRow_Click(object sender, EventArgs e) { protected void AddRow_Click(object sender, EventArgs e) {

                SqlCommand cmd = con.CreateCommand();
        cmd.CommandType = CommandType.Text;         
        cmd.CommandText = "ALTER TABLE table1 ADD @CNAME VARCHAR(50) NULL;";
        cmd.CommandText = cmd.CommandText.Replace("@CNAME", TextBox3.Text);
       // cmd.Parameters.AddWithValue("@CName", TextBox3.Text);
        cmd.ExecuteNonQuery();

        disp_data();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何 map SQL 服务器表的一列,以数字开头(列名称:2DBIT),到 ASP.NET MVC 项目(存储过程? - How can I map a column of a SQL Server table, beginning with a number (name of the column:2DBIT), to an ASP.NET MVC project (stored procedure) CRUD? 我如何在asp.net MVC中从URL更改隐藏控制器名称 - how can i change-hide controller name from url in asp.net MVC 如何使用ASP.NET 3网站在Razor中动态更改方法名称 - How to change method name dynamically in Razor with ASP.NET 3 website 如何使用ASP.NET从C#中的SQL Server表中自动完成TextBox中的值 - How can I autocomplete values in a TextBox from SQL Server table in C# with ASP.NET 更改asp.net身份中的列名 - Change column name in asp.net identity 我无法在从IIS Windows 7运行的ASP.net网站上显示来自sql server 2005的数据 - I cannot display data from sql server 2005 express on an ASP.net website running from IIS windows 7 我想在ASP.NET中的页面加载时绑定来自SQL Server表列的数据,但是它不起作用 - I want to bind data from SQL Server table column at page load in ASP.NET, but it is not working 如何在ASP.NET网站中显示表格? - How to display table in ASP.NET website? 如何在 asp.net 网站中实现 SOTag 文本框? - How can I implement SOTag textbox in asp.net website? 我如何在ASP.NET网站项目中使用XAML - How can i use xaml in asp.net website project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM