0

我在 VS 2010 中创建了一个以 SQL Server 2008 作为后端的桌面应用程序。当我创建应用程序的 exe 并尝试将其安装在客户端计算机上时,它显然要求我提供 SQL 数据库实例。但是我有自己的连接字符串,其数据源名称仅适用于我的机器,我怎样才能让我的应用程序像这样工作,当我将它安装在客户端机器上时它应该创建所需的数据库。(我已经在客户端机器上安装了 SQL Server 2008)。我尝试使用以下代码在客户端计算机上创建数据库,但出现错误:40 - 无法打开与 SQL Server 的连接)

String str;
SqlConnection myConn = new SqlConnection ("Server=localhost;Integrated security=SSPI;database=master");
str = "CREATE DATABASE MyDatabase"
SqlCommand myCommand = new SqlCommand(str, myConn);
try 
{
    myConn.Open();
myCommand.ExecuteNonQuery();
MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
if (myConn.State == ConnectionState.Open)
{
    myConn.Close();
}
}

任何建议都会有很大帮助!谢谢!

4

0 回答 0