Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a desktop application(Developed in c#) which will use SQL SERVER database. The database is on remote Database Server. I need to connect to that database on this database server and some transaction with this database.

I have the internet connection. I know the ip address and port number of sql server in this database server.

So what are the steps to connect to that database server?

share|improve this question

2 Answers 2

IMHO: it is not secure to have sql server listening on public ip address! You should use a vpn or at least a ssh tunnel.

Anyway, try to create an alias with ip address, in the MSSQL Client configuration utilty.

share|improve this answer

You can define a connection string in your App.config:

<connectionStrings>
       <add name="nameOfThisConnectionString" connectionString="Data Source=databaseserver; Initial Catalog=database;User Id=username;Password=password"/>
</connectionStrings>

Then you can use the connection string to execute commands:

string connectionString = ConfigurationManager.ConnectionStrings["nameOfThisConnectionString"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString );
connection.Open(); 
SqlCommand command = new SqlCommand("Select count(*) from members", connection);
int amount= (int)(command.ExecuteScalar());
labelamount.Text = amount.ToString() + " records";
connection.Close();

Thats about it. You can also use other query's like:

string connectionString = ConfigurationManager.ConnectionStrings["nameOfThisConnectionString"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString );
connection.Open(); 
SqlCommand command = new SqlCommand("Select * from Contact order by Name", connection);
SqlDataReader dataReader = command.ExecuteReader();
while (dataReader.Read())
    { 
        ListViewItem item = new ListViewItem(dataReader["ID"].ToString());
        item.SubItems.Add(dataReader["Name"].ToString());
        item.SubItems.Add(dataReader["Email"].ToString());
        listViewContacts.Items.Add(item);
    }   
dataReader.Close();
connection.Close();

With these 2 commands you should be able to get it work.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.