开发者

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 SqlComman开发者_运维知识库d("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


My guess is that in

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

t is not a string?


t is of type Task but when you pass it to your stored procedure you are passing it as nvarchar.

Here is your code:

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


Assuming your name variable is indeed the @TaskName simply use name

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

or if you override ToString()

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

or if you have Name on your Task object

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


In the line:

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

I think you have to pass t.Name or t.ToString() up on the task's name (I don't know it).


I think the problem is with this line:

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

t cannot be converted to a string. You could try t.Name or t.ToString() (not sure what properties are available on that class off the top of my head.)


T is type of Task and and ado try convert it to string you should put t.GetType().Name or the real task name here because we can't pass objects as parameter the only type are known sqltypes


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

t is a Task not a string. This should probably by name.


On the line:

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

't' is a task, not a string. You need to get the task name instead (by the looks of it)


The issue is on this line:

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

You are passing the task object in instead of the name of the task.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜