SqlConnection conn = new SqlConnection("server=192.168.1.26;uid=sa;pwd=;database=pubs;"); conn.Open(); SqlDataAdapter da = new SqlDataAdapter("select au_id, au_lname, au_fname from authors order by au_id", conn); DataSet ds = new DataSet(); da.Fill(ds); this.ListBox1.DataSource = ds.Tables[0].DefaultView; this.ListBox1.DataTextField="au_lname"; this.ListBox1.DataValueField="au_id"; this.ListBox1.DataBind();
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select au_id, au_lname, au_fname from authors order by au_id", conn);
DataSet ds = new DataSet();
da.Fill(ds);
this.ListBox1.DataSource = ds.Tables[0].DefaultView;
this.ListBox1.DataTextField="au_lname";
this.ListBox1.DataValueField="au_id";
this.ListBox1.DataBind();
using System.Data.SqlClient;
<add key="CONNECTIONSTRING" value="user id=SYS;data source=XXGL;password=SYS"></add>aspx.cs文件中
using System.Data.OracleClient;OracleConnection MyConnection=new OracleConnection();
DataSet ds=new DataSet();
MyConnection.ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString();
MyConnection.Open();
OracleDataAdapter dr=new OracleDataAdapter("select * from XXJCB",MyConnection);
dr.Fill(ds);
MyConnection.Close();以上代码针对oracle
连接字符串最好写在Config文件中;
<configuration>
<appSettings>
<add key="SQLCONNECTIONSTRING" value="data source=.;uid=sa;pwd=sa;database=NorthWind"></add>
</appSettings>
</configuration>取连接串的时候记得
using System.Configuration;
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString());