4

我正在制作一个 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();

让我知道您是否需要其他参考。

4

4 回答 4

6

您只需添加一次参数。您每次都通过循环添加参数。更改它,以便您添加一次参数,但每次在循环中设置它们的值。

SqlCommand cmdDeleteServerSapce = new SqlCommand(sDeleteServerSpace, connection);  
....  
cmdInsertServerSpace.Parameters.AddWithValue("@drive", temp);
cmdDeleteServerSapce.Parameters.AddWithValue("@deletedrive", temp));

for (int i = 0; i < driveStats.Length; i++)     
{        
    temp = driveStats[i].ToString();        
    cmdInsertServerSpace.Parameters["@drive"].value = temp;
    cmdDeleteServerSapce.Parameters["@deletedrive"].value = temp;

    .....

    cmdDeleteServerSapce.ExecuteNonQuery();
    cmdInsertServerSpace.ExecuteNonQuery(); 
} 
于 2012-06-05T20:54:03.437 回答
2

看起来您正在循环中使用该命令。您只需将参数放入一次(AddWithValue)。您可以在后续尝试中更改它,但不要再次添加它。

你应该做:

cmdDeleteServerSapce.Parameters.Add("@deletedrive", SqlDbType.VarChar); 

圈外。

cmdDeleteServerSapce.Parameters["@deletedrive"].Value = temp; 

循环内。

于 2012-06-05T20:52:36.077 回答
0

我相信你错过了对你cmdInsertServerSpace.ExecuteNonQuery()cmdDeleteServerSapce.ExecuteNonQuery()循环的调用。我会推荐以下内容:

cmdInsertServerSpace.Parameters.Add("@drive", SqlDbType.VarChar);
cmdDeleteServerSapce.Parameters.Add("@deletedrive", SqlDbType.VarChar);
for (int i = 0; i < driveStats.Length; i++) {
    string temp = driveStats[i].ToString();
    cmdInsertServerSpace.Parameters["@drive"].Value = temp;
    cmdDeleteServerSapce.Parameters["@deletedrive"].Value = temp;
    i++;
  .....
    cmdInsertServerSpace.ExecuteNonQuery();
    cmdDeleteServerSapce.ExecuteNonQuery();
}
于 2012-06-05T21:00:01.557 回答
0

您必须使用参数 clear

            **cmdInsertServerSpace.Parameters.Clear();**
            cmdInsertServerSpace.Parameters.AddWithValue("@dt", temp);

            temp = server.ToString();
            cmdDeleteServerSapce.Parameters.AddWithValue("@serd", temp);
            cmdInsertServerSpace.Parameters.AddWithValue("@ser", temp);
于 2012-11-23T23:29:18.693 回答