简体   繁体   中英

Unknown Error Connecting To SQL Server in c#

Let me add a quick explanation to this post:

I am using visual studio and if I create a connection to a database it works. I can query the database through the database designer (The one where you see the tables and can create new queries) and the data processes correctly.

HOWEVER even using this route I am getting the same sql exception. To me this says something in visual studio is not set up correctly however I could be wrong.

I am using the following code to connect to a database on my server (with using System.Data.SqlClient; at the top):

SqlConnection thisConnection = new SqlConnection();
thisConnection.ConnectionString = 
                              "Data Source=192.168.0.0,1433;" +
                              "Initial Catalog=test-db;" +
                              "User Id=UserName;" +
                              "Password=Password;";
thisConnection.Open();

And I am getting the following error:

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

If you have any idea how to resolve this it would be greatly appreciated!

Exception is below:

System.Data.SqlClient.SqlError: SQL Server does not exist or access denied

However I am positive I have permission to this server as I can access it and query it in visual studio.

I'm guessing it can't find the server. Given the default configuration on most routers/modems, I doubt that 192.168.0.0 is the IP you're looking for. If it's on your local machine, use localhost or 127.0.0.1 or your computer name (eg hans-pc ). If it's on another machine, you need to make sure you have the right IP and that it is configured for remote access in both the firewall and the SQL Server Configuration Manager (enable the TCP/IP and/or named pipes protocols).

Also, you can use a few aliases for the properties in that string to shorten it up:

server=192.168.0.0;database=test-db;user id=UserName;password=Password
                                                                      ↑
                                      Make sure no trailing semicolon ┘

I dropped the port as 1433 is the default port for SQL server.

To get more information about the exception, try this code:

try
{
   ...
   thisConnection.Open();
   ...
}
catch (SqlException ex)
{
    for (int i = 0; i < ex.Errors.Count; i++)
    {
        Console.WriteLine(ex.Errors[i].ToString());
        // or output them wherever you need to see them
    }
}

Alright so here is the answer everyone!!!

The emulator was not set up correctly...THIS HAS NOTHING TO DO WITH CODE!!! You need to set up the emulator to connect to the internet, if using a mobile emulator see this link:

http://www.xdevsoftware.com/blog/post/Enable-Network-Connection-Windows-Mobile-6-Emulator.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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