串連SQLserver資料庫:
string albumName = TextBoxName.Text.Trim();//擷取建立相簿的名字 SqlConnection conn = new SqlConnection("Server=(local);database=電子相簿;uid=sa;pwd=zouming");//串連資料庫 conn.Open(); string sql = "insert into Table_Name (name)values('" + albumName + "')"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); conn.Close(); SqlConnection conn = new SqlConnection("Server=(local);database=電子相簿;uid=sa;pwd=zouming"); conn.Open(); string sql = "select name from Table_Name"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds); DropDownList1.DataSource = ds.Tables[0]; DropDownList1.DataTextField = "name";
串連Oracler資料庫:
using System.Data.OracleClient;
OracleConnection con = new OracleConnection("Data Source=myOracle;user id=xxx;password=xxx;");OracleCommand cmd = new OracleCommand("select * from table", con);con.Open();OracleDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);GridView1.DataSource = dr;GridView1.DataBind();dr.Close();