Pergunta

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.

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;

    }

}

}

Foi útil?

Solução

At a minimum, your connection string is trying to connect to database inside your VM, not on host machine:

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top