C#连接数据库的普通方法
1.首先,配置web
<configuration> <connectionStrings> <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/> <add name="conn" connectionString="Data Source=orcl;User ID=xue;Password=xue123;Unicode=True" providerName="System.Data.OracleClient"/> </connectionStrings>
2.连接程序
protected void Button1_Click1(object sender, EventArgs e) { string oc = ConfigurationManager.ConnectionStrings["conn"].ToString(); OracleConnection orc = new OracleConnection(oc); OracleCommand omd1 = new OracleCommand(); OracleCommand omd2 = new OracleCommand(); omd1.Connection = orc; omd2.Connection = orc; orc.Open(); String sql2= "select * from ADMIN"; String sql1 = "insert into ADMIN values('lang','lang123')"; this.Label1.Text = "已将数据插入到Oracle数据库"; omd1.CommandText = sql1; omd2.CommandText = sql2; omd1.ExecuteNonQuery(); omd2.ExecuteNonQuery(); }