728x90
using System; using System.Data.SqlClient; namespace CS_ConsoleApp { class Program { static void Main(string[] args) { SqlConnection con = new SqlConnection(); //SqlCommand cmd = new SqlCommand(); Console.WriteLine("연결시도"); con.ConnectionString = "server=210.119.12.79,1617; database=ADO; uid=sa; pwd=it17"; con.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM ADO", con); Console.WriteLine("DB 연결 성공"); /* Console.WriteLine("테이블 만들기 시도"); cmd.Connection = con; cmd.CommandText = "CREATE table 성적표 (" + "번호 int primary key," + "이름 varchar(6) not null," + "국어 int," + "영어 int," + "수학 int)"; cmd.ExecuteNonQuery(); Console.WriteLine("테이블 만들기 성공"); */ Console.WriteLine("데이터 추가 시도"); cmd.CommandText = "INSERT INTO 성적표 VALUES(1, '추경호', 95, 100, 85)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO 성적표 VALUES(2, '조수현', 100, 80, 85)"; cmd.ExecuteNonQuery(); con.Close(); } } }
using System; using System.Data.SqlClient; namespace CS_ConsoleApp { class Program { static void Main(string[] args) { SqlConnection con = new SqlConnection(); Console.WriteLine("DB 연결시도"); con.ConnectionString = "server=210.119.12.79,1617; database=ADO; uid=sa; pwd=it4321"; con.Open(); Console.WriteLine("DB 연결 성공"); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; Display(cmd); cmd.CommandText = "update 성적표 set 영어=@영어,수학=@수학 " + "where 이름='조수현'"; cmd.Parameters.AddWithValue("@영어", 99); cmd.Parameters.AddWithValue("@수학", 88); cmd.ExecuteNonQuery(); Display(cmd); cmd.CommandText = "DELETE FROM 성적표 WHERE 이름 = @이름"; cmd.Parameters.AddWithValue("@이름", "조수현"); cmd.ExecuteNonQuery(); Display(cmd); con.Close(); } static void Display(SqlCommand cmd) { SqlDataReader sqlReader = null; cmd.CommandText = "select * from 성적표"; sqlReader = cmd.ExecuteReader(); for (int i = 0; i < sqlReader.FieldCount; i++) { Console.Write("{0,7}", sqlReader.GetName(i)); } Console.WriteLine(); string field = ""; while (sqlReader.Read()) { for (int i = 0; i < sqlReader.FieldCount; i++) { field = sqlReader.GetName(i); Console.Write("{0,8}", sqlReader[field]); } Console.WriteLine(); } sqlReader.Close(); } } }
윈폼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DB3 { public partial class Form1 : Form { SqlConnection con; SqlDataAdapter mSqlDataAdapter; DataSet mDataSet; DataTable mDataTable; public Form1() { InitializeComponent(); try { if (con == null) con = new SqlConnection(); textBox1.AppendText("DB연결 시도\r\n"); con.ConnectionString = "server=PKNU1\\SQLEXPRESS;database=ADO;uid=sa;pwd=1234"; con.Open(); textBox1.AppendText("DB연결 성공\r\n"); } catch { textBox1.AppendText("DB연결 실패\r\n"); } updateDataView(); } public void updateDataView() { SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "select * from 성적표"; mSqlDataAdapter = new SqlDataAdapter(); mSqlDataAdapter.SelectCommand = cmd; mDataSet = new DataSet("성적관리"); mSqlDataAdapter.Fill(mDataSet, "성적표"); mDataTable = mDataSet.Tables["성적표"]; dataGridView1.DataSource = mDataTable; } } }
'Study > C#' 카테고리의 다른 글
IP 주소 정수로 변환 (0) | 2017.05.16 |
---|---|
C# Thread (0) | 2017.04.24 |
C# 네트워크 기본 (1) | 2017.04.12 |
C# 예외처리, delegate (0) | 2017.04.10 |
C# 인터페이스 (0) | 2017.04.07 |