我想从我的字符串变量 tablename 添加到我的查询名称表中。在c#中有可能吗?我试过下面的东西。但它不起作用。如您所见,我想在分数
上设置 FROM @tablenamepublic void tableInsertTest()
{
MySqlConnection conn = null;
string tablename = "scores";
try
{
conn = new MySqlConnection(cs);
conn.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = conn;
cmd.CommandTimeout = 90;
cmd.CommandText = (
"INSERT INTO dailyrecords(recorddate, firstname, lastname, score ) " +
"SELECT NOW(), name, lname, score FROM @tablename " );
cmd.Prepare();
cmd.Parameters.AddWithValue("@tablename", tablename);
{
cmd.ExecuteNonQuery();
}
conn.Close();
}
finally
{
if (conn != null)
{
conn.Close();
}
}
return;
}
答案 0 :(得分:1)
cmd.CommandText = ("INSERT INTO dailyrecords(recorddate, firstname, lastname, score ) " +
string.Format("SELECT NOW(), name, lname, score FROM {0} ", tablename) );
您的查询只是一个字符串,您有值:)