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();
}
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 .
There are few mistakes in your sql query .
It's sys.databases
not sys.database
No space between Go
and Create
syntax
You need to place the sql statements in seperate line
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.