Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

sql - C# stored procedure with parameters

I am receiving an error in my application and i can not figure out how to resolve it. Here is the code:

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();
         }

The error I am receiving is with the ExecuteNonQuery. It says

InvalidCastException Failed to convert parameter value from a Task to a String.

I thought it had something to do with where I put the try (inside the if statement) but I am not sure. Any help would be much appreciated.

Thanks,

Matt

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My guess is that in

   myCommand2.Parameters.Add("@TaskName", SqlDbType.NVarChar, 50).Value = t;

t is not a string?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...