-1

这是我的存储过程

[dbo].[DFW_Completed_Safety] (
    @StartDate VARCHAR(10),
    @Station VARCHAR(50),
    @EmployeeID INT)

当我编写以下代码时:

SqlDataAdapter daAC_CSM = new SqlDataAdapter();
DataSet dsAC_CSM = new DataSet();
try
{
    using (SqlConnection sqlConnection = new SqlConnection(connectionString))
    {
        sqlCmd = new SqlCommand();
        sqlCmd.CommandType = CommandType.StoredProcedure;               
        sqlCmd.Connection = sqlConnection;
        sqlCmd.CommandTimeout = 0;
        sqlCmd.CommandText = "DFW_Completed_Safety";
        sqlCmd.Parameters.AddWithValue("@StartDate", startdate);
        sqlCmd.Parameters.AddWithValue("@Station", station);
        sqlCmd.Parameters.AddWithValue("@EmployeeID", "0");
        daAC_CSM.SelectCommand = sqlCmd;
        daAC_CSM.Fill(dsAC_CSM);
    }
    return dsAC_CSM;
}
catch (Exception)
{
    throw;
}

它抛出异常:EmployeeID作为 varchar 接收。

Conversion failed when converting the varchar value 'd ' to data type int.

我尝试过的事情:

1- 许多其他人在 StackOverflow 上发帖建议这样Convert.ToInt32(0);做。由于默认情况下 0 是 Int32,因此这不是解决方案。
2-更改接收varchar的方法(发送“0”),它也不起作用。

感谢您的任何想法!(将方法签名保持为 Int 会更好)。

更新:这个问题还没有回答,因为将我的存储过程更改为 varchar 并没有成功。有什么想法吗?

4

3 回答 3

0

Finally it wasn't the first line. The FormName is a field that stores the FormID. The programmer that was here before was probably a noob or changed the Column datatype to int, making all queries not to work. Thanks anyways @Matt_Whitfield & @Luxspes. By the way Luxpes, you were right, it was written line 1 even on SSMS, but I did it using the same:

EXEC    @return_value = [dbo].[DFW_Completed_Safety]
        @StartDate = N'07-18-2012',
        @Station = N'YHZ',
        @EmployeeID = 0

And by doing Print @SqlStatement, I was able to copy & paste in a new Query and see that it was the Form*Name* that was an Int. Who knew that a Name could be an Int?

于 2012-07-23T18:24:34.637 回答
0

当您从 SSMS 运行您的程序时,您很可能会遇到相同的错误,因为该错误很可能源自您的程序主体,而不是您如何调用它。如果您正在查询的表中的列中有值“d” - 并且您正在将该列与整数类型进行比较,那么您将收到该错误。另外,还有几点:

  • 您应该将您的SqlCommandSqlConnection实例放在 using 子句中或手动处理它们,因为它们是 IDisposable。
  • 你可能不想throw ex在你的 catch 块中 - 你可能只是想要throw. 通过使用throw ex,您会弄乱原始异常中可用的堆栈跟踪。
于 2012-07-23T15:10:27.187 回答
0

请像这样重写您的代码:

try
{
    sqlCon = new SqlConnection(connectionString);
    sqlCmd = new SqlCommand();
    sqlCmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter daAC_CSM = new SqlDataAdapter();
    DataSet dsAC_CSM = new DataSet();
    sqlCmd.Connection = sqlCon;
    sqlCmd.CommandTimeout = 0;
    sqlCmd.CommandText = "DFW_Completed_Safety";
    sqlCmd.Parameters.AddWithValue("@StartDate", startdate);  //Using "@"
    sqlCmd.Parameters.AddWithValue("@Station", station);    //Using "@"
    sqlCmd.Parameters.AddWithValue("@EmployeeID", 0); //Using "@"

    foreach(SqlParameter p in  sqlCmd.Parameters){
      //Will print Name, Type and Value
      System.Diagnostics.Trace.WriteLine("Name:" + p.ParameterName + "Type: " + p.DbType+" Value: "+p.Value); 
    }

    sqlCon.Open();
    daAC_CSM.SelectCommand = sqlCmd;
    daAC_CSM.Fill(dsAC_CSM);
    sqlCon.Close();
    return dsAC_CSM;
}
catch (Exception ex)
{
    throw ex;
}

它打印什么?你得到什么错误?

于 2012-07-22T23:53:15.930 回答