我正在改进控制台应用程序,目前我无法让它更新行,而不仅仅是创建一个包含更新信息的新行。
class Program
{
List<DriveInfo> driveList = DriveInfo.GetDrives().Where(x => x.IsReady).ToList<DriveInfo>(); //Get all the drive info
Server server = new Server(); //Create the server object
ServerDrive serverDrives = new ServerDrive();
public static void Main()
{
Program c = new Program();
c.RealDriveInfo();
c.WriteInToDB();
}
public void RealDriveInfo()
{
//Insert information of one server
server.ServerID = 0; //(PK) ID Auto-assigned by SQL
server.ServerName = string.Concat(System.Environment.MachineName);
//Inserts ServerDrives information.
for (int i = 0; i < driveList.Count; i++)
{
//All Information used in dbo.ServerDrives
serverDrives.DriveLetter = driveList[i].Name;
serverDrives.TotalSpace = driveList[i].TotalSize;
serverDrives.DriveLabel = driveList[i].VolumeLabel;
serverDrives.FreeSpace = driveList[i].TotalFreeSpace;
serverDrives.DriveType = driveList[i].DriveFormat;
server.ServerDrives.Add(serverDrives);
}
}
public void WriteInToDB()
{
//Add the information to an SQL Database using Linq.
DataClasses1DataContext db = new DataClasses1DataContext(@"sqlserver");
db.Servers.InsertOnSubmit(server);
db.SubmitChanges();
我希望它用来更新信息的是RealDriveInfo()
方法,因此它不是创建新条目,而是通过运行该方法更新当前存储的信息,然后插入该方法中的信息,如果需要,将输入一个新条目而不是简单地输入每次有新信息时都有新条目。
目前它正在运行该方法,收集相关数据,然后将其作为两个表中的新行输入。
任何帮助,将不胜感激 :)