المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Disposing of values in foreach loop



C# Programming
10-15-2011, 12:21 AM
Greetings Gurus,

The problem:
"The variable name '@Tstamp' has already been declared. Variable names must be unique within a query batch or stored procedure."

Guesstimate cause:
The data in the variable '@Tstamp' is not being released

The codeSqlCommand inst = new SqlCommand("INSERT INTO dbo.DrvInfo(Tstamp)" + "VALUES (@Tstamp)", DrvInfo); DriveInfo[] drives = DriveInfo.GetDrives(); foreach (DriveInfo d in drives) { Console.WriteLine(d.Name); Console.WriteLine(d.DriveType); if (d.IsReady == true) { string label = d.VolumeLabel; if (label.Length == 0) label = "No Label"; Console.WriteLine(d.VolumeLabel); Console.WriteLine(d.AvailableFreeSpace / 1024000000); Console.WriteLine(d.TotalSize / 1024000000); DrvInfo.Open(); inst.Parameters.Add("@Tstamp", SqlDbType.NVarChar, 50).Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InstalledUICulture);// the other values follow this but I am using this as an example inst.ExecuteNonQuery(); DrvInfo.Close(); } else Console.WriteLine("uh oh"); }
The question:
In iterating through the values of the foreach, how do I dispose of the first set of collected data so that the next set can be written to the table?

Can you help please? Do I need to declare each variable as "Nothing" before restarting the loop?