这是我试过的:
SqlConnection cn = new SqlConnection(@"Data Source=10.0.0.2\MSSQLLocalDB,8;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=;Password=");
SqlDataAdapter da;
DataTable dt = new DataTable();
string query = "Select * from [Employees]";
da = new SqlDataAdapter(query, cn);
da.Fill(dt);
dataGridView1.DataSource = dt;
答案 0 :(得分:0)
获得您所获得的错误会非常有帮助。我想它就是这样的
TITLE: Connect to Server
------------------------------
Cannot connect to 10.0.0.2
------------------------------
ADDITIONAL INFORMATION:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 53)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&EvtSrc=MSSQLServer&EvtID=53&LinkId=20476
------------------------------
The network path was not found
------------------------------
无论如何,在我看来,在你提供的连接字符串中
"Data Source=10.0.0.2\MSSQLLocalDB,8;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=;Password="
在数据源的连接上指定端口号8。这似乎有点可疑,因为默认情况下1433是MSSQL端口,甚至任何数字为1024或更少的端口都应该是系统保留的,不能被用户驱动的应用程序使用。
如果这不能回答您的问题,请提供您收到的错误消息。