I have a C# project on VMWare and my database is outside of VMWare. It's on my real PC and the VMWare is on the same PC. My question is how can I access my database on my PC ? I have this class but it does not work.
我在VMWare上有一个C#项目,我的数据库在VMWare之外。它在我的真实PC上,VMWare在同一台PC上。我的问题是如何在我的电脑*问我的数据库?我有这个课但它不起作用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
namespace EyeCareCenter
{
class DataBase
{
private string connectionstring = @"Data Source =127.0.0.1,1433 ; Network Library=DBMSSOCN ;Initial Catalog=EyeCareCenter ; Trusted_Connection=True ";
private SqlConnection connection = new SqlConnection();
public void OpenConnection()
{
try
{
connection.ConnectionString = connectionstring ;
connection.Open();
// MessageBox.Show (" Connection successfull ... ");
}
catch ( Exception a)
{
MessageBox.Show(a.Message + " ");
}
}
public void CloseConnection()
{
connection.Close();
}
public SqlConnection GetConnection()
{
return connection;
}
}
}
1 个解决方案
#1
0
At a minimum, your connection string is trying to connect to database inside your VM, not on host machine:
您的连接字符串至少尝试连接到VM内的数据库,而不是主机上的数据库:
private string connectionstring = @"Data Source =127.0.0.1,1433 ; Network Library=DBMSSOCN ;Initial Catalog=EyeCareCenter ; Trusted_Connection=True ";
private string connectionstring = @“Data Source = 127.0.0.1,1433; Network Library = DBMSSOCN; Initial Catalog = EyeCareCenter; Trusted_Connection = True”;
The IP should be changed to that of your host machine to connect to it. And make sure that your VM is configured for networking, and that networking configuration allows for connecting to your host machine.
应将IP更改为主机的IP以连接到IP。并确保您的VM配置为网络,并且该网络配置允许连接到主机。
#1
0
At a minimum, your connection string is trying to connect to database inside your VM, not on host machine:
您的连接字符串至少尝试连接到VM内的数据库,而不是主机上的数据库:
private string connectionstring = @"Data Source =127.0.0.1,1433 ; Network Library=DBMSSOCN ;Initial Catalog=EyeCareCenter ; Trusted_Connection=True ";
private string connectionstring = @“Data Source = 127.0.0.1,1433; Network Library = DBMSSOCN; Initial Catalog = EyeCareCenter; Trusted_Connection = True”;
The IP should be changed to that of your host machine to connect to it. And make sure that your VM is configured for networking, and that networking configuration allows for connecting to your host machine.
应将IP更改为主机的IP以连接到IP。并确保您的VM配置为网络,并且该网络配置允许连接到主机。