I need to insert the weather forecast (temperature) into a SQL Server database in order to control remotely the heating of a building.
The steps are:
- Getting data from an RSS feed
- Putting them in an array
- Connecting to an SQL database
- Inserting the elements into the SQL database
I did the 3 first steps but I'm stuck with the last one.
The array is created from an RSS feed from Yahoo weather.
string[,] myarray1 = new string[5, 3];
The name of the database's columns are: Date, Templow, Temphigh
I'm trying now to insert the elements of this array into a SQL Server database. I've struggled with that for hours, and I can't figure out how to do it. I looked at many solutions on this website without succeeding.
I tried:
foreach (string str2 in myarray1)
{
var mycommand = new SqlCommand("INSERT INTO RSS2 VALUES(@Date, @Templow, @Temphigh)", myConnection);
mycommand.Parameters.AddWithValue("@Date", str2);
mycommand.Parameters.AddWithValue("@Templow", str2);
mycommand.Parameters.AddWithValue("@Temphigh", str2);
mycommand.ExecuteNonQuery();
}
and
for (k = 0; k < 5; k++)
{
SqlCommand myCommand = new SqlCommand("INSERT INTO RSS2 (Date, Templow, Temphigh)" +
"Values ('myarray1[k,0]','myarray1[k,1]','myarray1[k,2]')", myConnection);
myCommand.ExecuteNonQuery();
}
and many others...
None of those solutions are corrects. This is my first code in c# (I'm used to Basic), so be clement ;-)