Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Connect to remote SQL Server securely?

Tags:

c#

sql-server

I know it's easy to connect to a SQL Server database however I'm not sure how I'm supposed to do it remotely and on the same time.. with a secure way .

SqlConnection sqlConnection = this.sqlcon();

SqlCommand insertCommand = new SqlCommand("use " + database_telecaster.ToString() + " SELECT Top 1  sid from dbo.Item order by sid desc", sqlConnection);

sqlConnection.Open();
insertCommand.ExecuteNonQuery();

SqlDataReader reader = insertCommand.ExecuteReader();

while (reader.Read())
{
    MaxSid = (reader.GetInt64(0) + 100).ToString();
}
reader.Close();
sqlConnection.Close();

SQL Server con function :

public SqlConnection sqlcon()
{
    var doc = new XPathDocument(Application.StartupPath + "/DBConn.xml");
    var navigator = doc.CreateNavigator();

    var serverName = navigator.SelectSingleNode("//appsettings/servername");

    var username = navigator.SelectSingleNode("//appsettings/username");
    var password = navigator.SelectSingleNode("//appsettings/password");
    var database = navigator.SelectSingleNode("//appsettings/database");

    object[] objArray = new object[] {
            serverName , database, username , password 
    };

    return new SqlConnection(string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};MultipleActiveResultSets = True", objArray));
}

Assuming that the SQL Server is on a Windows VPS installed and I'm going to give my software to different people and I want them all to access that SQL server... how I can do that without opening the ports of the SQL Server ? Because as far as I know opening the ports will lead to getting hacked since all people will be able to connect remotely to that server .

like image 228
Da black ninja Avatar asked Feb 13 '16 14:02

Da black ninja


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

This questions reminds me of me when I was getting started...

Whatever you do, do not connect to the database directly, because to connect directly you would have to store the database connection strings (and passwords) within your application... you could obfuscate it, make it as obscure as you like, it wont make a difference... You'll essentially hand over the keys to the castle.

Instead, you need to start learning how to create an API, that authenticates the client and connects to the data layer on the clients behalf, performs the operations requested and then returns the result.

Personally, I would use ASP.NET Web API, it's the right tool for the job. There is a slight learning curve to it but just stick with it and you'll have it figured out in a few days. Start off with these PluralSight videos, they're an excellent resource which is completely free thanks to Microsoft, and they'll certainly keep you busy!

like image 176
Aydin Avatar answered Oct 05 '22 04:10

Aydin