gpt4 book ai didi

c# - 将信息附加到文本文件

转载 作者:太空宇宙 更新时间:2023-11-03 18:16:10 34 4
gpt4 key购买 nike

我有一个网站,我正在将记录添加到数据库中,并且我还将信息写入作为网站一部分的日志文件。
信息被写入文件很好......我唯一的问题是如何将每个输入的信息添加到数据库中。
由于目前信息被放入 lof 文件,但每次新插入数据库时​​,日志文件中的现有记录似乎被覆盖并替换为新插入,因此始终只有一条记录添加到日志文件。
建议也许...
这是我在 btnClick 事件中使用的代码。

protected void btnSubmitRating_Click1(object sender, EventArgs e)
{
int rating = 0;
try
{
if (radRating.Items[0].Selected)
{
rating = 1;
}
if (radRating.Items[1].Selected)
{
rating = 2;
}
else if (radRating.Items[2].Selected)
{
rating = 3;
}
else if (radRating.Items[3].Selected)
{
rating = 4;
}
else if (radRating.Items[4].Selected)
{
rating = 5;
}
FileStream rate = new FileStream(Server.MapPath("~/rateBooks.log"), FileMode.Open, FileAccess.ReadWrite);
BufferedStream bs = new BufferedStream(rate);
rate.Close();


StreamWriter sw = new StreamWriter(Server.MapPath("~/rateBooks.log"));
sw.WriteLine("userName " + txtUserName.Text + " " + "bookTitle " + txtBookTitle.Text + " " + "ISBN " + txtISBN.Text);
sw.Close();


Service s = new Service();
s.bookRatedAdd(txtBookTitle.Text, rating, txtReview.Text, txtISBN.Text, txtUserName.Text);
btnSubmitRating.Enabled = false;
radRating.Enabled = false;
}
catch (Exception em)
{
lblError.Text = em.Message;
//lblError.Text = "This book has already been reviewed by yourself?";
}
}

亲切的问候

最佳答案

更改整个代码块:

FileStream rate = new FileStream(Server.MapPath("~/rateBooks.log"), FileMode.Open, FileAccess.ReadWrite);
BufferedStream bs = new BufferedStream(rate);
rate.Close();

StreamWriter sw = new StreamWriter(Server.MapPath("~/rateBooks.log"));
sw.WriteLine("userName " + txtUserName.Text + " " + "bookTitle " + txtBookTitle.Text + " " + "ISBN " + txtISBN.Text);
sw.Close();

到这一行:
File.AppendAllText(Server.MapPath("~/rateBooks.log"), "userName " + txtUserName.Text + " " + "bookTitle " + txtBookTitle.Text + " " + "ISBN " + txtISBN.Text + Environment.NewLine);

请注意,我现在手动添加 NewLine到每一行。

关于c# - 将信息附加到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5963726/

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