[英]SqlException in app using SQL Server
Hey I just started learning to make application in C# using back-end as sql. 嘿,我刚刚开始学习使用后端作为sql在C#中制作应用程序。 I put some breakpoints to see that the control never comes back after executing the cntCeilInn.Open();
我放置了一些断点,以查看执行cntCeilInn.Open();
之后该控件永不返回cntCeilInn.Open();
soon, the output window shows ' System.Data.SqlClient.SqlException
' occurred in System.Data.dll
不久,在输出窗口显示“ System.Data.SqlClient.SqlException
”发生在System.Data.dll
Google tells me its not timing out or something that I couldn't understand. Google告诉我它没有超时或我无法理解的事情。 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: 作为connectionString参数,您应该使用:
Data Source=yourServerName;Initial Catalog=yourDatabaseName;Integrated Security=True
make sure to modify youServerName
and yourDatabaseName
by appropriate data. 确保通过适当的数据修改youServerName
和yourDatabaseName
。
Note that you forgot to set the database name in your connectionString( Initial Catalog ), and the value for Integrated Security is True . 请注意,您忘记在connectionString( Initial Catalog )中设置数据库名称,而Integrated Security的值为True 。
There are few mistakes in your sql query . sql查询中几乎没有错误。
It's sys.databases
not sys.database
它是sys.databases
而不是sys.database
No space between Go
and Create
syntax Go
和Create
语法之间没有空格
You need to place the sql statements in seperate line 您需要将sql语句放在单独的行中
Not to mention do check your connection string
once as others have stated 更不用说像其他人所说的那样检查您的connection string
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.