简体   繁体   English

C# 带参数的存储过程

[英]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 .我收到的错误是ExecuteNonQuery It says它说

InvalidCastException Failed to convert parameter value from a Task to a String. InvalidCastException 无法将参数值从任务转换为字符串。

I thought it had something to do with where I put the try (inside the if statement) but I am not sure.我认为这与我在哪里尝试(在 if 语句中)有关,但我不确定。 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 不是字符串?

t is of type Task but when you pass it to your stored procedure you are passing it as nvarchar . t 属于Task类型,但是当您将其传递给存储过程时,您将其作为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假设您的name变量确实是@TaskName只需使用name

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

or if you override ToString()或者如果你覆盖ToString()

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

or if you have Name on your Task object或者如果您的Task中有Name 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).我认为您必须在任务名称上传递t.Namet.ToString() (我不知道)。

I think the problem is with this line:我认为问题出在这一行:

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

t cannot be converted to a string. t 不能转换为字符串。 You could try t.Name or t.ToString() (not sure what properties are available on that class off the top of my head.)您可以尝试t.Name or t.ToString() (不确定我头顶上的 class 有哪些属性可用。)

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 T 是 Task 的类型,并且尝试将其转换为字符串,您应该将t.GetType().Name或真正的任务名称放在这里,因为我们不能将对象作为参数传递,唯一的类型是已知的 sqltypes

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

t is a Task not a string. t是一个Task而不是一个字符串。 This should probably by name .这应该是name

On the line:在线上:

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

't' is a task, not a string. 't' 是一个任务,而不是一个字符串。 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.您传递的是任务 object 而不是任务的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM