0

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

谷歌告诉我它没有超时或我无法理解的东西。请帮忙。

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

2 回答 2

1

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

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

确保修改youServerNameyourDatabaseName使用适当的数据。

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

于 2013-01-17T06:24:44.603 回答
0

您的 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);
于 2013-01-17T06:40:49.517 回答