SQL Server C# 콘솔 연결
1. 연결할 사용자 및 sa 사용
01. SQL Server 외부 연결 설정
01. sa 설정



02. User 생성




2. 콘솔에 연결
01. Select
using System;
using System.Data.SqlClient;
namespace DBTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // SQL 연결 정보(server = 127.0.0.1, 포트:1433, 아이디 : sa or test, 비밀번호 : sa or test, db : dbName)
            string connectionString  = "server = 127.0.0.1,1433; uid = test; pwd = 1234; database = testdb";
            // 새 연결 정보 생성
            SqlConnection sqlConn = new SqlConnection(connectionString);
            SqlCommand sqlComm = new SqlCommand();
            sqlComm.Connection = sqlConn;
            // query문
            sqlComm.CommandText = "SELECT * FROM test1";
            sqlConn.Open();
            using (SqlDataReader SqlRs = sqlComm.ExecuteReader())
            {
                Console.WriteLine("ID \t \t | Name");
                while(SqlRs.Read())
                {
                    Console.WriteLine($"{SqlRs[0].ToString()} \t \t | {SqlRs[1].ToString()}");
                }
            }
            sqlConn.Close();
            {
            }
        }
    }
}
using System;
using System.Data.SqlClient;
namespace DBTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // SQL 연결 정보(server = 127.0.0.1, 포트:1433, 아이디 : sa or test, 비밀번호 : sa or test, db : dbName)
            string connectionString  = "server = 127.0.0.1,1433; uid = test; pwd = 1234; database = testdb";
            // 새 연결 정보 생성
            SqlConnection sqlConn = new SqlConnection(connectionString);
            SqlCommand sqlComm = new SqlCommand();
            sqlComm.Connection = sqlConn;
            // query문
            sqlComm.CommandText = "SELECT * FROM test1";
            sqlConn.Open();
            using (SqlDataReader SqlRs = sqlComm.ExecuteReader())
            {
                Console.WriteLine("ID \t \t | Name");
                while(SqlRs.Read())
                {
                    Console.WriteLine($"{SqlRs[0].ToString()} \t \t | {SqlRs[1].ToString()}");
                }
            }
            sqlConn.Close();
            {
            }
        }
    }
}
02. Insert, update, delete
using System;
using System.Data.SqlClient;
namespace DBTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // SQL 연결 정보(server = 127.0.0.1, 포트:1433, 아이디 : sa or test, 비밀번호 : sa or test, db : dbName)
            string connectionString  = "server = 127.0.0.1,1433; uid = test; pwd = 1234; database = testdb";
            // 새 연결 정보 생성
            SqlConnection sqlConn = new SqlConnection(connectionString);
            SqlCommand sqlComm = new SqlCommand();
            sqlComm.Connection = sqlConn;
            // query문
            sqlComm.CommandText = "insert into test1 values(@param1, @param2)";
            // sqlComm.CommandText = "update test1 set name=@param2 where id=@param1";
            // sqlComm.CommandText = "delete test1 where id=@param1";
            sqlComm.Parameters.AddWithValue("@param1", 3);
            sqlComm.Parameters.AddWithValue("@param2", "c");
            sqlConn.Open();
            sqlComm.ExecuteNonQuery();
            sqlConn.Close();
            {
            }
        }
    }
}



Author And Source
이 문제에 관하여(SQL Server C# 콘솔 연결), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@ansalstmd/SQL-Server-C-콘솔-연결저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)