时间:2021-07-01 10:21:17 帮助过:14人阅读
1. 连接Oracle
先引入Oracle.ManagedDataAccess.Client;
private void view_Click(object sender, EventArgs e) { string connStr = string.Empty; DataTable datatable = new DataTable(); try { // connStr = string.Format(@"User Id=QXNCFX;Password=NJSLYORCL; Data Source=(description=(address_list = (address = (HOST = {0})(PROTOCOL = TCP)(PORT= {1})))(connect_data=(service_name={2})));", "199.10.10.50", "1521", "orcl", "system", "oracle123"); connStr = "User Id=QXNCFX;Password=NJSLYORCL;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=199.10.10.50)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=orcl)))"; using (OracleConnection conn = new OracleConnection(connStr)) { conn.Open(); OracleCommand comm = new OracleCommand("select * from GEO_GATE_1T", conn); OracleDataAdapter adapter = new OracleDataAdapter(comm); adapter.Fill(datatable); if (datatable.Rows.Count > 0) { this.dataGridView1.DataSource = datatable; } conn.Close(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.WriteLine("连接字符串:" + connStr); } }
2. 连接Sql server
try { string sqlConn = @"server=DELL-PC\SQLEXPRESS1;database=OPGWDB;uid=sa;pwd=sa"; SqlConnection conn = new SqlConnection(sqlConn); conn.Open(); Console.WriteLine(conn.ServerVersion); DataTable dt = new DataTable(); string sql = "Select * from dbo.Yhjsb"; SqlDataAdapter ad = new SqlDataAdapter(sql, conn); ad.Fill(dt); conn.Close(); } catch (Exception ex) { Console.WriteLine(ex); }
C# 链接数据库
标签:GridView OLE tostring except line use EDA select string