这个查询有一些问题::
var q2 =
from sd in db.ServerDrives
where sd.DriveLetter == driveList[i].Name
select sd;
ServerDrive existingServerDrives = q.First();
existingServerDrives.FreeSpace = driveList[i].FreeSpace;
//..
db.SubmitChanges();
我的问题是我不知道将 [i] 的引用放在哪里。
[i] 的参考 ::
for (int i = 0; i < driveList.Count; i++)
我可以把它放在上面,
existingServerDrives.FreeSpace = driveList[i].FreeSpace;
该行可以识别它,但是由于 .(下面的代码),我需要整个查询来识别它。线
where sd.DriveLetter == driveList[i].Name;
有谁知道我怎么可能做到这一点?
编辑:完整代码::
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 - You will need get information of all servers
server.ServerID = 0; //Here is necessery put PK key. I recommend doing the SQL server will automatically generate the PK.
server.ServerName = string.Concat(System.Environment.MachineName);
//Inserts information in the newServers object
for (int i = 0; i < driveList.Count; i++)
{
//Put here all the information to object Server
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);
var q2 =
from s in db.Servers
where s.ServerName == "LAPTOP-043"
select s;
Server existingServers = q2.First();
for (int i = 0; i < driveList.Count; i++)
existingServers.ServerName = string.Concat(System.Environment.MachineName);
//..
for (int i = 0; i < driveList.Count; i++)
{
var q =
from sd in db.ServerDrives
where sd.DriveLetter == driveList[i].Name
select sd;
ServerDrive existingServerDrives = q.First();
existingServerDrives.FreeSpace = driveList[i].FreeSpace;
//..
db.SubmitChanges();
我要做的是让查询找到与此计算机上的驱动器 C:\ 相同的“DriveLetter”行。然后,第二部分应该更改 FreeSpace 值并将其替换为从控制台应用程序检索到的新 FreeSpace 值。
任何反馈将不胜感激,在此先感谢:)