我有與我添加記錄到數據庫和信息,我也寫這是網站的一部分,一個日誌文件,一個網站。 的信息寫入到文件罰款...我有唯一的問題是如何與每個輸入到數據庫中添加信息。 由於目前信息被放在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?";
}
}
親切的問候
不需要創建任何流,'File'類爲此擁有靜態助手。 :-) – 2011-05-11 11:56:30
@Shadow Wizard - 是的,我同意。這不是我怎麼做的,但我認爲傳遞信息並展示他如何對他現有的代碼進行小修改是很好的。最好是儘可能展示最佳實踐...... – 2011-05-11 11:59:28
是的,我也傾向於多次提供現有代碼的小修補程序,所以它都很好。 :-) – 2011-05-11 12:08:08