[英]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.