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. Database & SysAdmin
  3. Database
  4. Executing a query

Executing a query

Scheduled Pinned Locked Moved Database
sharepointdatabasehelp
3 Posts 3 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.
  • P Offline
    P Offline
    Pothirajan C
    wrote on last edited by
    #1

    I have the following query statement in RichTextBox control While executing i am reading the text from the Richtextbox and passing it to SqlDataAdapter object. But it is telling me that incorrect syntax near 'GO' statement MyQuery: if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_createAddEditscript]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_createAddEditscript] GO MyCode: try { SqlConnection con = new SqlConnection(conStr); SqlDataAdapter da = new SqlDataAdapter(this.richTextBox1.Text.ToString(), con); DataSet ds = new DataSet(); da.Fill(ds); //this.dataGrid1.DataSource = ds.Tables[0]; } catch (SqlException e1) { MessageBox.Show(e1.Message); } Please help me Thanks in Advance, Pothirajan C

    M C 2 Replies Last reply
    0
    • P Pothirajan C

      I have the following query statement in RichTextBox control While executing i am reading the text from the Richtextbox and passing it to SqlDataAdapter object. But it is telling me that incorrect syntax near 'GO' statement MyQuery: if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_createAddEditscript]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_createAddEditscript] GO MyCode: try { SqlConnection con = new SqlConnection(conStr); SqlDataAdapter da = new SqlDataAdapter(this.richTextBox1.Text.ToString(), con); DataSet ds = new DataSet(); da.Fill(ds); //this.dataGrid1.DataSource = ds.Tables[0]; } catch (SqlException e1) { MessageBox.Show(e1.Message); } Please help me Thanks in Advance, Pothirajan C

      M Offline
      M Offline
      MasudM
      wrote on last edited by
      #2

      Hi In SqlDataAdapter da = new SqlDataAdapter(this.richTextBox1.Text.ToString(), con); The first parameter in the Select Command and DA. Use this command to fill the dataset. As you are written you drop a procedure and does not select anything ! To run such command against Sql Server use SQlCommand class.Like this : SqlCommand cmd = new SqlCommand("",); cmd.ExecuteNonQuery();

      1 Reply Last reply
      0
      • P Pothirajan C

        I have the following query statement in RichTextBox control While executing i am reading the text from the Richtextbox and passing it to SqlDataAdapter object. But it is telling me that incorrect syntax near 'GO' statement MyQuery: if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_createAddEditscript]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_createAddEditscript] GO MyCode: try { SqlConnection con = new SqlConnection(conStr); SqlDataAdapter da = new SqlDataAdapter(this.richTextBox1.Text.ToString(), con); DataSet ds = new DataSet(); da.Fill(ds); //this.dataGrid1.DataSource = ds.Tables[0]; } catch (SqlException e1) { MessageBox.Show(e1.Message); } Please help me Thanks in Advance, Pothirajan C

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        The reason is that "GO" is not part of SQL. It is a command for the Query Analyser to tell it to run the next set of statements in a different batch. Also, as your query is not returning a result set you should not be trying to Fill a DataSet. You should create a SqlCommand object then ExecuteNonQuery() on it. For example:

        SqlCommand cmd = new SqlCommand("if exists (select * from dbo.sysobjects where id ="+
        "object_id(N'[dbo].[sp_createAddEditscript]') and OBJECTPROPERTY(id, N'IsProcedure')"+
        " = 1) drop procedure [dbo].[sp_createAddEditscript]", myConnection);
        myConnection.Open();
        cmd.ExecuteNonQuery();
        myConnection.Close();

        Does this help?


        My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

        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