您好我正在尝试创建一个从数据库读取数据的通用方法。这是我到目前为止所拥有的:
private static void GetData(string tableName, string key, string values)
{
string connectionString = "data source=DB03RO;initial catalog=NCOI_Dev;User ID=NCOI_Olympus;Password=Olympus;persist security info=False;packet size=4096;Enlist=true;Transaction Binding=Explicit Unbind;";
string commandText = @"SELECT @key , @value FROM @Name";
SqlDataReader reader = null;
using (var connection = new SqlConnection(connectionString))
using (var cmd = new SqlCommand())
{
connection.Open();
cmd.Connection = connection;
cmd.Parameters.Add(new SqlParameter("@Name", tableName));
cmd.Parameters.Add(new SqlParameter("@key", key));
cmd.Parameters.Add(new SqlParameter("@value", values));
cmd.CommandType = CommandType.Text;
cmd.CommandText = commandText;
reader = cmd.ExecuteReader();
while (reader != null)
{
string id = (string)reader[key];
string data = (string)reader[values];
Console.WriteLine(id + " " + data);
}
}
}
当我尝试执行阅读器时,我似乎遇到了错误:
必须声明表变量“@Name”。
我该如何纠正这个问题?