0

执行以下代码时,出现以下错误。

"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!!")

代码怎么能在我的另一种形式上工作,但不能在这个形式上工作?

注意。如果我直接从 SQL 服务器运行查询,则 SQL 执行正常

非常感谢任何帮助

4

1 回答 1

1

ExecuteScalar更多地用于查询仅返回一个值时。如果你将 'count作为第一个元素放在你的 'SELECT中,它将起作用。

SqlCommand.ExecuteScalar Method的文档中:

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

于 2014-08-29T18:26:36.327 回答