【发布时间】:2013-04-26 02:40:06
【问题描述】:
try
{
//Create our connection strings
string sSqlConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Path.GetDirectoryName(Path.GetDirectoryName(Application.StartupPath)) + "\\ClaimFiles.mdf;Integrated Security=True;User Instance=True";
MessageBox.Show(sSqlConnectionString);
//Execute a query to erase any previous data from our destination table
string sClearSQL = "DELETE FROM PA";
SqlConnection SqlConn = new SqlConnection(sSqlConnectionString);
SqlCommand SqlCmd = new SqlCommand(sClearSQL, SqlConn);
SqlConn.Open();
MessageBox.Show(SqlCmd.ExecuteNonQuery().ToString());
SqlConn.Close();
}
catch (SqlException ex)
{
//handle exception
StringBuilder errorMessages = new StringBuilder();
for (int i = 0; i < ex.Errors.Count; i++)
{
errorMessages.Append("Index #: " + i + "\n" +
"Message: " + ex.Errors[i].Message + "\n" +
"ErrorNumber: " + ex.Errors[i].Number + "\n" +
"Source: " + ex.Errors[i].Source + "\n" +
"Severity Level: " + ex.Errors[i].Class + "\n" +
"Server:" + ex.Errors[i].Server + "\n");
MessageBox.Show(errorMessages.ToString());
}
}
以上是我的 C# 代码,我使用的是 Microsoft SQL express。上面的代码在点击时被激活。当我在 Visual Studio 中运行代码时,一切正常。但是当我将项目的文件夹复制到另一台计算机(操作系统:Windows XP)并运行 .exe 文件时,程序会捕获 SqlException:
建立与服务器的连接时发生错误。连接到 SQL Server 2005 时,此故障可能是由于在默认设置下 SQL Server 不允许远程连接造成的。 (提供者:SQL 网络接口,错误 26 - 错误定位服务器/指定实例)
谁能帮我解决这个问题,这将是一个很大的帮助,因为程序必须在不同的计算机上运行。顺便说一下程序的目标框架是.NET 3.5
【问题讨论】:
-
您确定服务正在运行吗?您是否确保 SQL Server Express 甚至安装在第二台计算机上?
-
另一台计算机上没有 SQL Server。
-
其他电脑是否需要SQL server express?
-
是的,除非您可以通过网络连接到某个现有的 SQL Server 实例,否则此处的连接字符串肯定假定 SQL Server 在本地运行。如果它不在本地运行,它必须在某个地方运行。
-
感谢 Aaron Bertrand 的回复:))
标签: sql-server