2014-08-29 47 views
0

當執行下面的代碼我碰到下面的錯誤。錯誤的SQL算入的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!!") 

代碼爲什麼我的作品其它形式,但不是在這一個?

注意:在SQL執行確定,如果我從SQL Server

運行查詢直接任何幫助非常感謝

+0

'ExecuteScalar'旨在用於當查詢只返回一個值。如果你把'count'作爲你的'SELECT'中的第一個元素,它可能會工作。 – 2014-08-29 18:17:49

+0

謝謝你,工作!如果您想在下面添加,歡迎標記爲已回答。再次感謝 – user3580480 2014-08-29 18:21:11

回答

1

ExecuteScalar旨在爲更多的查詢時,只返回一個值。如果您將count作爲您的SELECT中的第一個元素,它將起作用。

從文檔SqlCommand.ExecuteScalar Method

執行查詢,並在查詢返回的結果集返回第一行的第一列。其他列或行將被忽略。