2016-06-08 50 views
0

我試圖保存一些字符串列表,這樣如果關閉程序,我不會丟失所有信息。將應用程序中的變量存儲在c中關閉#

我有這些變量:

list<string> StudentName = new list<string>; 
list<string> Email = new list<string>; 
list<string> StudentID = new list<string>; 

我添加一個新的變量每個列表中的用戶提交完全填寫一張表格後。如果有人要關閉應用程序,我想要保存每個列表中的所有變量。我已經使用應用程序設置來存儲過去的數據,但我不知道有多少人會使用這個應用程序。

+1

取決於你想如何存儲這個。您可以使用所有信息的數據庫,因爲您使用的是列表 –

+0

您可以使用數據庫來存儲此信息,或將您的列表存儲在xml文件中。 – AdrienTorris

+1

downvote是沒有必要的。這是一個合法的問題。 –

回答

2

一個方法可行。將彙總列出了一個類型

public class AppState 
{ 
    public List<string> StudentName = new List<string>(); 
    public List<string> Email = new List<string>(); 
    public List<string> StudentID = new List<string>(); 
} 

,並使用Xml serialization and deserialization將數據存儲在一個文件中。

0

您可以在值存儲在數據庫中:

// I'd create a model to start with 

public class Student 
{ 
    public int StudentID {get; set;} 
    public string StudentName {get; set;} 
    public string Email {get; set;} 
} 

// Followed by a Controller to store the data: 

public class StoreStudentOnAppClose() 
{ 
    public void StoreStudents() 
    { 
     List<Student> studentList = new List<Student>(); 
     // Predefined method which you'll add to your class to grab each student from the database table. 
     studentList = GetStudentList(); 
     foreach(var student in studentList) 
     { 
      var db = UmbracoContext.Application.DatabaseContext.Database; 
      var insert = new Sql("INSERT INTO students VALUES('" + StudentID + "', '" + StudentName +"'. '" + Email +"')"); 
      int res = db.Execute(insert); 
     } 
    } 
} 

這顯然是一把umbraco應用程序上下文不過點是一樣的,你只需要改變你的連接字符串。

+0

請不要推薦字符串連接,它很容易發生SQL注入。 –

+0

爲避免SQL注入,您可以隨時在這裏查看有關SQL注入安全查詢的更多深入信息:[MSDN Protected from SQL Injection](https://msdn.microsoft.com/zh-cn/library/ff648339.aspx) –

相關問題