我正在改进控制台应用程序的过程中,此刻我无法获取它来更新行,而不是创建一个新的行,其中包含更新的信息。C# - 使用LINQ更新行
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()
方法,这样,而不是创建新条目它运行的方法,然后从方法插入信息更新当前存储的信息,如果需要意志输入一个新条目,而不是每次输入新条目时都输入新条目。
目前正在运行该方法,收集相关数据,然后在两个表中作为新行输入。
任何帮助,将不胜感激:)
只是为了澄清,我想要做的是让我的数据库更新RealDriveInfo方法收集的信息。它所做的是创建一个新条目,并将更新后的TotalSpace和FreeSpace变量添加到数据库中,我想要的是查找服务器的名称(例如Server1),然后更新信息而不是添加新条目。 – Ghostyy 2012-08-09 08:24:03
因此,首先您可以选择保存在数据库中的所有服务器条目。然后你会循环访问driveList(和你一样)。如果driveList [i]不存在于您选择的服务器条目列表中=插入新服务器;否则用当前的潜水表数据更新现有的服务器列表条目。 – 2012-08-09 08:59:20
这就是我想要做的,但我只是不知道如何得到一个查询循环driveList .. – Ghostyy 2012-08-09 09:13:55