Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Trying to run below query from Ado.net connection but getting error

Trying to run below query from Ado.net connection but getting error

Scheduled Pinned Locked Moved C#
databasehelpcsharpsql-serversysadmin
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 8621695
    wrote on last edited by
    #1

    Hi, I am trying to run below query from C# code using ADO.Net but I am getting an error "System.Data.SqlClient.SqlException: 'Incorrect syntax near '.'. Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon." This query is working fine with Sql server query window.

    SELECT DISTINCT CONVERT(DECIMAL(18, 2) , user_seeks * avg_total_user_cost * ( avg_user_impact * 0.01 )) AS [index_advantage] ,
    migs.last_user_seek ,
    mid.[statement] AS [Database.Schema.Table] ,
    mid.equality_columns ,
    mid.inequality_columns ,
    mid.included_columns ,
    migs.unique_compiles ,
    migs.user_seeks ,
    migs.avg_total_user_cost ,
    migs.avg_user_impact ,
    OBJECT_NAME(mid.[object_id]) AS [Table Name] ,
    p.rows AS [Table Rows]
    FROM sys.dm_db_missing_index_group_stats AS migs WITH ( NOLOCK )
    INNER JOIN sys.dm_db_missing_index_groups AS mig WITH ( NOLOCK ) ON migs.group_handle = mig.index_group_handle
    INNER JOIN sys.dm_db_missing_index_details AS mid WITH ( NOLOCK ) ON mig.index_handle = mid.index_handle
    INNER JOIN sys.partitions AS p WITH ( NOLOCK ) ON p.[object_id] = mid.[object_id]
    WHERE mid.database_id = DB_ID()
    ORDER BY index_advantage DESC
    OPTION ( RECOMPILE );

    So to fix this issue I added ; before With but still it's not working saying some error near '.'. Can someone please help here. I am using below code to call this query:

    SqlCommand sqlCommand = new SqlCommand(query, connection);
    connection.Open();
    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader()

    Thanks

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups