所以我想从数据库中创建一个文本(如日志文件)。每个文本都代表用户的记录。 显然,根据数据库,每条记录都有不同的名称。 我想知道如何在创建文本文件时将字符串变量放在doubleqoute中。如何将一个字符串变量放在双引号内?
public static void createLog()
{
MySqlConnection con = new MySqlConnection();
con.ConnectionString = "SERVER=localhost;DATABASE=s_project;UID=root;PASSWORD='';";SELECT
con.Open();
MySqlCommand thiscommand = con.CreateCommand();
thiscommand.CommandText = "SELECT user_name FROM user_info";
MySqlDataReader read = thiscommand.ExecuteReader();
while (read.Read())
{
string user_name;
user_name = read.GetString("user_name");
StreamWriter SW;
SW = File.CreateText("c:\\MyTextFile.txt"); //what should I put instead of MyTextFile if I would like it to be the variable user_name?
}
con.Close();
}
太感谢你了, –
虽然AVD回答的是100%正确的,用字符串连接,我建议使用'Path.Combine(...)'文件系统时。 'SW = File.CreateText(Path.Combine(“C:\\”,user_name,“.txt”));' –
@NickBabcock,你应该将它作为答案发布 – Habib