SQL计入ExecuteScalar()时出错

时间:2014-08-29 18:13:50

标签: sql sql-server vb.net executescalar

执行以下代码时,我收到以下错误。

"Additional information: Conversion from string "_01202478334" to type 'Double' is not valid."

代码:

          Using connn As New SqlClient.SqlConnection("server=inlt01\SQLEXPRESS; database=DaisyServices; integrated security=yes")
          Using cmdz As SqlClient.SqlCommand = conn.CreateCommand()

          cmdz.CommandText = "SELECT CLI, FromDate, ToDate, [Description], TotalCost, COUNT(*) as Count FROM [" + FileNameOnly + "] GROUP BY CLI, FromDate, ToDate, [Description], TotalCost HAVING COUNT(*) > 1"
          connn.Open()

          If cmdz.ExecuteScalar() > 1 Then

          'Error if name in use
          MessageBox.Show("Duplicate records exist on imported file!!")

为了排除故障,我删除了CLI字段,但后来又出现了新的错误

"Additional information: Operator '>' is not defined for type 'Date' and type 'Integer'"

我在一个单独的表单上使用了一些非常相似的代码,它运行时没有任何错误。

这是我的工作代码:

          Using connn As New SqlClient.SqlConnection("server=inlt01\SQLEXPRESS; database=DaisyBilling; integrated security=yes")
          Using cmdz As SqlClient.SqlCommand = conn.CreateCommand()

          cmdz.CommandText = "SELECT CustomerCLI, calldate, calltime, duration, TelephoneNumber, COUNT(*) as Count FROM [" + FileNameOnly + "] GROUP BY CustomerCLI, calldate, calltime, duration, TelephoneNumber HAVING COUNT(*) > 1"
          connn.Open()

          If cmdz.ExecuteScalar() > 1 Then

          'Error if name in use
          MessageBox.Show("Duplicate records exist on imported file!!")

为什么代码在我的其他表单上有效,但在这个表单上却没有?

NB。如果我直接从SQL服务器

运行查询,则SQL执行正常

任何帮助非常感谢

1 个答案:

答案 0 :(得分:1)

ExecuteScalar更适用于查询仅返回一个值的情况。如果您将count作为SELECT中的第一个元素,则可以使用。

来自SqlCommand.ExecuteScalar Method的文档:

  

执行查询,并返回查询返回的结果集中第一行的第一列。其他列或行将被忽略。