如何使用自定义表格名称在C#中插入

时间:2019-03-15 19:02:08

标签: c# sql-server

这是我尝试的部分代码

SqlConnection con = new SqlConnection(@"Data Source=ANSARI-PC\;Initial Catalog=BMS;Integrated Security=True");
    string tname = idc + "-InvoiceT";
    string sql = "select count(*) from '"+ tname + "' ";
    con.Open();
    SqlCommand sda = new SqlCommand(sql, con);
    SqlDataReader myreader;
    myreader = sda.ExecuteReader();
    int lid;  

对于上述代码,我收到此错误,表示“ 2-InvoiceT”附近的语法不正确 2-InvoiceT是数据库“ idc”中的表名,包含2作为值。

1 个答案:

答案 0 :(得分:-1)

更改

string sql = "select count(*) from '"+ tname + "' ";

string sql = "select count(*) from ["+ tname + "] ";