将表名作为c#windows窗体中更新查询的参数

时间:2015-04-20 05:19:26

标签: c# winforms

我正在制作一个Windows应用程序,其中我有多个控件,显示来自不同sql数据库表的值。我想创建一个更新函数,它将表名,columnName作为参数,但它不起作用。

这是代码

    void updateThis(string tableName, string columnName, string textBoxValue)
    {
        using (SqlConnection conn = new SqlConnection("Server=.\\SQLEXPRESS;Database=FIR_db; User Id = sa; Password = 9889922527"))
        {
            //try
            {
                cmd = new SqlCommand("update @t set @a = @b where profile_id= @c", conn);
                conn.Open();
                cmd.Parameters.AddWithValue("@t", tableName);
                cmd.Parameters.AddWithValue("@a", columnName);
                cmd.Parameters.AddWithValue("@b", textBoxValue);
                cmd.Parameters.AddWithValue("@c", lblprofil.Text);
                cmd.ExecuteNonQuery();
            }
            //catch (SqlException exc)
            //{
            //    DialogResult dr = MessageBox.Show("Error in server. Could not load Designation.", "Error in server", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
        }
    }

当我使用这个函数时,它显示了一个sqlException“必须声明表变量”@t“。顺便说一句,这是否可行。

0 个答案:

没有答案