繁体   English   中英

使用SQL Server的应用程序中的SqlException

[英]SqlException in app using SQL Server

嘿,我刚刚开始学习使用后端作为sql在C#中制作应用程序。 我放置了一些断点,以查看执行cntCeilInn.Open();之后该控件永不返回cntCeilInn.Open(); 不久,在输出窗口显示“ System.Data.SqlClient.SqlException ”发生在System.Data.dll

Google告诉我它没有超时或我无法理解的事情。 请一些帮助。

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();
}

作为connectionString参数,您应该使用:

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

确保通过适当的数据修改youServerNameyourDatabaseName

请注意,您忘记在connectionString( Initial Catalog )中设置数据库名称,而Integrated Security的值为True

sql查询中几乎没有错误。

  1. 它是sys.databases而不是sys.database

  2. GoCreate语法之间没有空格

  3. 您需要将sql语句放在单独的行中

  4. 更不用说像其他人所说的那样检查您的connection string

试试看

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM