gpt4 book ai didi

c# - 选择查询中表的变量名

转载 作者:行者123 更新时间:2023-11-29 13:19:33 26 4
gpt4 key购买 nike

我想从字符串变量 tablename 添加到表的查询名称中。在c#中可以吗?我尝试了如下所示的方法。但这不起作用。正如你所看到的,我想在分数上设置FROM @tablename

public 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;
}

最佳答案

cmd.CommandText = ("INSERT INTO dailyrecords(recorddate, firstname, lastname, score ) " +
string.Format("SELECT NOW(), name, lname, score FROM {0} ", tablename) );

您的查询只是一个字符串,并且您有值:)

关于c# - 选择查询中表的变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21034028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com