[데이터베이스] - ADO.NET(SqlClient, OracleClient) MSSQL, ORACLE 연동하기
○ ADO.NET(SqlClient, OracleClient) MSSQL, ORACLE 연동하기 |
* MSSQL - SqlConnection, SqlCommand, SqlDataReader 사용 - command.Paramters.Add(":empno1", num1) 으로 변수 대입 - ExecuteReader() : select, ExecuteNonQuery() : insert, update, delete 사용 static public void SqlClientSelect() { string connectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=데이터베이스명;user id=계정명; password=패스워드"; string queryString = "SELECT ProductID, UnitPrice, ProductName from dbo.products " + "WHERE UnitPrice > @pricePoint " + "ORDER BY UnitPrice DESC;"; int paramValue = 5; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(queryString, connection); command.Parameters.AddWithValue("@pricePoint", paramValue); try { connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader.GetInt32(0) + "\t" + reader.GetString(1) + "\t" + reader.GetString(2)); } reader.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } * ORACLE - OracleConnection, OracleCommand, OracleDataReader 사용 - command.Parametes.AddWithValue("@pricePoint", paramValue) 으로 변수 대입 - ExecuteReader() : select, ExecuteNonQuery() : insert, update, delete 사용 static public void OracleClientSelect() { string connectionString = "data source=//연결하려는ip:1521/orcl;user id=scott; password=tiger"; string queryString ="select * from EMP where EMPNO IN(:empno1, :empno2)"; using (OracleConnection connection = new OracleConnection(connectionString)) { OracleCommand command = connection.CreateCommand(); command.CommandText = queryString; int num1 = 7369, num2 = 1111; command.Parameters.Add(":empno1", num1); command.Parameters.Add(":empno2", num2); try { connection.Open(); OracleDataReader reader = command.ExecuteReader(); Console.WriteLine(reader.GetName(0) + "\t" + reader.GetName(1) + "\t" + reader.GetName(2) + "\n"); while (reader.Read()) { Console.WriteLine(reader.GetInt32(0) + "\t" + reader.GetString(1) + "\t" + reader.GetString(2)); } reader.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } |