让我在这篇文章中添加一个快速解释:
我正在使用 Visual Studio,如果我创建到数据库的连接,它就可以工作。我可以通过数据库设计器(您可以看到表并可以创建新查询的那个)查询数据库并且数据处理正确。
但是,即使使用这条路线,我也会得到相同的 sql 异常。对我来说,这说明 Visual Studio 中的某些内容设置不正确,但我可能是错的。
我正在使用以下代码连接到服务器上的数据库(使用 System.Data.SqlClient; 在顶部):
SqlConnection thisConnection = new SqlConnection();
thisConnection.ConnectionString =
"Data Source=192.168.0.0,1433;" +
"Initial Catalog=test-db;" +
"User Id=UserName;" +
"Password=Password;";
thisConnection.Open();
我收到以下错误:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, TdsParserState state)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, TdsParserState state)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Connect(String host, SqlInternalConnection connHandler, Int32 timeout)
at System.Data.SqlClient.SqlInternalConnection.OpenAndLogin()
at System.Data.SqlClient.SqlInternalConnection..ctor(SqlConnection connection, Hashtable connectionOptions)
at System.Data.SqlClient.SqlConnection.Open()
at InventoryControl.Login.validUserName()
at InventoryControl.Login.LoginButton_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
at System.Windows.Forms.Application.Run(Form fm)
at InventoryControl.Program.Main()
如果您知道如何解决此问题,将不胜感激!
例外情况如下:
System.Data.SqlClient.SqlError: SQL Server does not exist or access denied
但是我很肯定我有权访问该服务器,因为我可以访问它并在 Visual Studio 中查询它。