1.数据库的连接代码
需要引用System.Data.OracleClient;
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.OracleClient; namespace Ioperator_dispatch { public class DB { public static OracleConnection getcon() { //string cs = "server=10.4.108.17;uid=admin;pwd=123123;Data Source=test;pooling=true;min pool size=2;max pool size=4"; string cs = System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; OracleConnection con = new OracleConnection(cs); return con; } public DataSet getdataset(String sql) { OracleConnection con = DB.getcon(); con.Open(); OracleDataAdapter oda = new OracleDataAdapter(sql, con); con.Close(); DataSet ds = new DataSet(); oda.Fill(ds); return ds; } public DataTable getdatatable(String sql) { OracleConnection con = DB.getcon(); con.Open(); OracleDataAdapter oda = new OracleDataAdapter(sql, con); con.Close(); DataTable dt = new DataTable(); oda.Fill(dt); return dt; } } }
2.配置数据库连接配置文件Web.config
<connectionStrings> <add name="connstr" connectionString="server=10.4.108.17;uid=admin;pwd=123123;Data Source=test;pooling=true;min pool size=1;max pool size=50"/> </connectionStrings>
3.数据库查询测试.aspx对应的.cs文件
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using Net.RequestHandle; using Ioperator_dispatch.html; namespace Ioperator_dispatch.html { public partial class rep_rbt_dispatch_title : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DB db = new DB(); string sql = "select eqpid from equn@odsprd where eqptype='WIO' and eqpid<>'AWAC01'"; DataTable datatable =db.getdatatable(sql); for (int i = 0; i < datatable.Rows.Count; i++) { Response.Write(datatable.Rows[i][0].ToString() ); } } } }