-1

I want to pass a variable to a sql string being executed via C#. I am hitting a compile error tho, and I am not perfectly sure how to end my sql statement with my variable. This is the line of code that I have

string Fire = "Database";
SqlConnection conn = new SqlConnection(connectSQL);
SqlCommand("IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = Main_'" + Fire + "') DROP VIEW dbo.Main_'" + Fire + "');

The errors are

Newline in constant
; expected
) expected

What do I need to do so that this becomes a valid statement?

Big Pimpin
  • 427
  • 9
  • 23
  • 2
    You're treating the `SqlCommand` type like a method. You need to instantiate it with `new`. There's a bazillion (maybe more!) examples of how to create a `SqlCommand` object on the internet. – itsme86 Feb 18 '15 at 19:07

1 Answers1

1

You are missing the closing double quote. See below.

SqlCommand("IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = Main_'" + Fire + "') DROP VIEW dbo.Main_'" + Fire + "'**"**);
WorkSmarter
  • 3,738
  • 3
  • 29
  • 34