我正在制作一个 Windows 服务来收集大量信息,并将其发送到数据库。我知道下面的方法是有效的,因为我已经在其他表格上使用它而没有错误。但是我不得不扩展我的 DELETE 命令以使用两个条件,现在我收到一个错误。
这是我收到的错误:“已经声明了变量名'@deletedrive'。变量名在查询批处理或存储过程中必须是唯一的。”
这是我的代码(结尾省略了,因为它只是捕获了我们得到的异常。也忽略注释行):
private static void ServerSpaceTimerAction(object source, ElapsedEventArgs e, string InstallLocation)
{
//StreamWriter RecordServerSpace = new StreamWriter(InstallLocation + "\\Temp\\ServerSpaceTemp.csv");
try
{
ServerSpace GetSpace = new ServerSpace();
GetConfigurations Configs = new GetConfigurations();
Configs.GetServers();
Configs.GetDatabase();
var connection = new SqlConnection("Data Source=" + Configs.DataSource + "; Initial Catalog=" + Configs.DatabaseCatalog + "; Integrated Security=" + Configs.IntegratedSecurityProtocol);
connection.Open();
char[] delim = { ' ', ',' };
string sInsertServerSpace = "INSERT INTO ServerSpace (date_time, server, region, farm, technology, role, drive, total_space, free_space) VALUES (@dt, @ser, @reg, @farm, @tech, @role, @drive, @ts, @fs)";
string sDeleteServerSpace = "DELETE FROM ServerSpace WHERE server=@serd AND drive=@deletedrive";
foreach (string server in Configs.servers)
{
string temp;
SqlCommand cmdInsertServerSpace = new SqlCommand(sInsertServerSpace, connection);
SqlCommand cmdDeleteServerSapce = new SqlCommand(sDeleteServerSpace, connection);
Configs.GetServerAttributes(server);
if (Configs.technology != "SQL")
{
GetSpace.DriveSpaceByServer(server);
}
else
{
GetSpace.DriveSpaceByServerMount(server);
}
string[] driveStats = GetSpace.ServerSpaceStats.Split(delim);
temp = DateTime.Now.ToString();
cmdInsertServerSpace.Parameters.AddWithValue("@dt", temp);
temp = server.ToString();
cmdDeleteServerSapce.Parameters.AddWithValue("@serd", temp);
cmdInsertServerSpace.Parameters.AddWithValue("@ser", temp);
temp = Configs.regiongroup.ToString();
cmdInsertServerSpace.Parameters.AddWithValue("@reg", temp);
temp = Configs.farmgroup.ToString();
cmdInsertServerSpace.Parameters.AddWithValue("@farm", temp);
temp = Configs.technology.ToString();
cmdInsertServerSpace.Parameters.AddWithValue("@tech", temp);
temp = Configs.rolegroup.ToString();
cmdInsertServerSpace.Parameters.AddWithValue("@role", temp);
for (int i = 0; i < driveStats.Length; i++)
{
temp = driveStats[i].ToString();
cmdInsertServerSpace.Parameters.AddWithValue("@drive", temp);
cmdDeleteServerSapce.Parameters.AddWithValue("@deletedrive", temp);
i++;
temp = driveStats[i].ToString();
cmdInsertServerSpace.Parameters.AddWithValue("@ts", temp);
i++;
temp = driveStats[i].ToString();
cmdInsertServerSpace.Parameters.AddWithValue("@fs", temp);
cmdDeleteServerSapce.ExecuteNonQuery();
cmdInsertServerSpace.ExecuteNonQuery();
}
//RecordServerSpace.WriteLine(DateTime.Now + "," + server + "," + Configs.regiongroup + "," + Configs.farmgroup + "," + Configs.technology + "," + Configs.rolegroup + "," + GetSpace.ServerSpaceStats);
}
connection.Close();
让我知道您是否需要其他参考。