C# How to Connect to SQLServer

I was creating applications with SQLServer and VisualStudio for study.

At that time, I didn’t know how to write the concrete way to access the database with C#.

Let me introduce how to connect to SQLServer.

SQLServer Connection-SELECT

The source code is as follows.

1.Receive SELECT statement in String sql as an argument.

2.Name the table to the DataTable returned by the SELECT statement.

3.Store DataTable into DataSet.

 

SQLServer Connection-INSERT,UPDATE,DELETE

The source code is as follows.

1.Receive SQL statement in String sql as an argument.

2.Update the database using sql.

Note When Connecting SQLServer

 

ConnectionString

What needs to be coded is the following three.
 “Server=PCName or ServerName;
   Database=DBName;
   User ID=sa;Password=PassWord;”;

 

Server

If you have SQLServe, you can easily check it with the following SQL:

※ IP address can also be used, but I think sometimes it may change, so personally it is recommended to use the host name.

Database

You can get list of databeses by following SQL

User ID and Password

It is divided into 2 patterns.

 

For SQLServer Authentication

As I show in the sample of code earlier, you can get authenticated with User ID and Password.

You can check when you open SQL Server Management Studio.

 For Windows Authentication

You can connect with Windows authentication by changing following written in red.

Server=PCName or ServerName;Database=DBName;Integrated Security=True;

For the difference between SQL Server authentication and Windows authentication, I found good explanation.

Please read this site.

In The End

People who have started to study programming may read this article.

I’ve been studying program for about 2 years, but I would like to introduce more useful information to those who are new to the program.

あわせて読みたい

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です