我在我的应用程序中收到一个错误,我不知道如何解决它。这是代码:
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString); myConnection.Open(); SqlCommand cmd = new SqlCommand("SELECT ServerIP FROM Servers", myConnection); SqlDataReader rdr = cmd.ExecuteReader(); if (rdr.HasRows) { while (rdr.Read()) { string serverIP = rdr["ServerIP"].ToString(); ScheduledTasks st = new ScheduledTasks(@"\\" + serverIP); string[] taskNames = st.GetTaskNames(); foreach (string name in taskNames) { Task t = st.OpenTask(name); var status = t.Status; var recentRun = t.MostRecentRunTime; var nextRun = t.NextRunTime; var appName = t.ApplicationName; SqlConnection myConnection2 = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString); SqlCommand myCommand2 = new SqlCommand("sp_AddScheduledTasks", myConnection2); try { myConnection2.Open(); myCommand2.CommandType = CommandType.StoredProcedure; myCommand2.Parameters.Add("@ID", SqlDbType.Int); myCommand2.Parameters["@ID"].Direction = ParameterDirection.Output; myCommand2.Parameters.Add("@ServerIP", SqlDbType.NVarChar, 20).Value = serverIP; myCommand2.Parameters.Add("@TaskName", SqlDbType.NVarChar, 50).Value = t; myCommand2.Parameters.Add("@MostRecentRun", SqlDbType.DateTime).Value = recentRun; myCommand2.Parameters.Add("@NextRunTime", SqlDbType.DateTime).Value = nextRun; myCommand2.Parameters.Add("@AppName", SqlDbType.NVarChar, 50).Value = appName; myCommand2.Parameters.Add("@Status", SqlDbType.NVarChar, 50).Value = status; int rows = myCommand2.ExecuteNonQuery(); } finally { myConnection2.Close(); }
我收到的错误是与ExecuteNonQuery。它说
ExecuteNonQuery
InvalidCastException无法将参数值从任务转换为字符串。
我认为这与我尝试放置位置有关(在if语句内部),但我不确定。任何帮助将非常感激。
谢谢,
马特
我的猜测是
myCommand2.Parameters.Add("@TaskName", SqlDbType.NVarChar, 50).Value = t;
t不是字符串吗?