S_pot

C#_MSSQL 서버와 연결하기 본문

C#

C#_MSSQL 서버와 연결하기

S_pot 2021. 7. 15. 10:30

SqlConnection()안에 로컬DB때 넣은 연결문자열이 아닌 

서버IP, ID, PWD, DATABASE이름을 넣어준다.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApp5
{
    public partial class Form1 : Form
    {
    	// 서버IP, ID, PWD, DATABASE 입력
        SqlConnection con = new SqlConnection("server = *****; uid = ****; pwd = ****; database = ****;");

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
            con.Open();
            display();
        }

        public void display()   
        {
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from TABLE";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            dataGridView1.DataSource = dt;

        }
    }
}

'C#' 카테고리의 다른 글

C#_프로그램 구조  (0) 2021.08.24
C#_C# 구조 및 탄생 배경  (0) 2021.08.24
C#_Split: 입력값 띄어쓰기  (0) 2021.06.27
C#_try-catch-finally문  (1) 2021.06.27
C#_this키워드를 사용하는이유  (0) 2021.06.27