0

Hey I just started learning to make application in C# using back-end as sql. I put some breakpoints to see that the control never comes back after executing the cntCeilInn.Open(); soon, the output window shows 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Google tells me its not timing out or something that I couldn't understand. Some help please.

string strServerName="EXPRESSION";

using (SqlConnection cntCeilInn = new SqlConnection("Data Source=" + strServerName + ";" + "Integrated Security=YES"))
{
    SqlCommand cmdCeilInn= new SqlCommand("If exists ("+ "Select name "+ "from sys.database "+ "Where name=N'CeilInn1')"+ "DROP database CeilInn1;"+ "go"+ "create database CeilInn1;", cntCeilInn);
    cntCeilInn.Open();
    cmdCeilInn.ExecuteNonQuery();
}
4
  • You can try catch the Exception and debug with its Exception message. Commented Jan 17, 2013 at 6:11
  • Check the exception message and its details, post it with your question Commented Jan 17, 2013 at 6:11
  • Have you included System.Data.SqlClient Namespace? Commented Jan 17, 2013 at 6:12
  • Why do you have a string literal split up into little parts? Perhaps the problem is that there's no space between ) and DROP. Commented Jan 17, 2013 at 6:39

2 Answers 2

1

As connectionString parameter,you should use:

Data Source=yourServerName;Initial Catalog=yourDatabaseName;Integrated Security=True

make sure to modify youServerName and yourDatabaseName by appropriate data.

Note that you forgot to set the database name in your connectionString(Initial Catalog), and the value for Integrated Security is True.

Sign up to request clarification or add additional context in comments.

Comments

0

There are few mistakes in your sql query .

  1. It's sys.databases not sys.database

  2. No space between Go and Create syntax

  3. You need to place the sql statements in seperate line

  4. Not to mention do check your connection string once as others have stated

Try this out

SqlCommand cmdCeilInn = new SqlCommand("If exists (" + "Select name " + "from sys.databases " + "Where name=N'Sample') DROP database Sample;" 
                      +Environment .NewLine+ "go " + Environment .NewLine 
                     +" create database Sample;", cntCeilInn);

1 Comment

@camelbrush : Please mark it as an answer if any of the suggestion helped you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.