Executing a query
-
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
-
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
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();
-
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
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
aDataSet
. You should create aSqlCommand
object thenExecuteNonQuery()
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