ADO.NET入門學習備忘
//連線字串_直接寫
string conStr = "Data Source=computer;Initial Catalog=AdoTest;Integrated Security=True; Persist Security Info=True;";
//連線字串_寫在Web.Config裡
string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["constring"].ToString();
//Web.Config裡設定
<connectionStrings>
<add name="constring" connectionString="Data Source=visingcomputer;Initial Catalog=AdoTest;Integrated Security=True; Persist Security Info=True;"/>
connectionStrings>
//連線資料庫
string conStr =System.Configuration.ConfigurationManager.ConnectionStrings[1].ToString();
SqlConnection sqlCon = new SqlConnection(conStr);
sqlCon.Open();
this.Label1.Text = sqlCon.State.ToString();
//插入資料
string sqlIntoCmd="insert into info values('123','地理學','g')";
SqlCommand sqlInto = new SqlCommand(sqlIntoCmd, sqlCon);
sqlInto.ExecuteNonQuery(); //這句執行後,資料才真正插入資料庫中.
/**/////使用SqlDataReader顯示資料
//string sqlSelectCmd = "select * from info";
//SqlCommand sqlSelect = new SqlCommand(sqlSelectCmd, sqlCon);
//SqlDataReader sqlDr=sqlSelect.ExecuteReader();
//while (sqlDr.Read())
//{
// Response.Write(sqlDr[0]);
// Response.Write(sqlDr[1]);
// Response.Write(sqlDr[2]);
// Response.Write("
");
//}
//sqlDr.Close();
//使用SqlDataAdapter,DataSet顯示資料
string sqlSelectCmd = "select * from info";
SqlDataAdapter da = new SqlDataAdapter(sqlSelectCmd, sqlCon);
DataSet ds = new DataSet();
da.Fill(ds, "info");
if (ds.Tables[0].Rows.Count == 0)
{
Response.Write("NOdata");
}
else
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
Response.Write(ds.Tables[0].Rows[i][0]);
Response.Write(ds.Tables[0].Rows[i][1]);
Response.Write(ds.Tables[0].Rows[i][2]);
Response.Write("
");
}
}
/**/////使用SqlDataAdapter,SqlCommandBuilder,DataSet插入資料
//string sqlSelectCmd = "select * from info";
//SqlDataAdapter da = new SqlDataAdapter(sqlSelectCmd, sqlCon);
//SqlCommandBuilder cb = new SqlCommandBuilder(da);
//DataSet ds = new DataSet();
//da.Fill(ds, "info");
//DataRow newrow = ds.Tables[0].NewRow();
//newrow[0] = "text";
//newrow[1] = "text";
//newrow[2] = "text";
//ds.Tables[0].Rows.Add(newrow);
//da.Update(ds, "info");
sqlCon.Close();
string conStr = "Data Source=computer;Initial Catalog=AdoTest;Integrated Security=True; Persist Security Info=True;";
//連線字串_寫在Web.Config裡
string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["constring"].ToString();
//Web.Config裡設定
<connectionStrings>
<add name="constring" connectionString="Data Source=visingcomputer;Initial Catalog=AdoTest;Integrated Security=True; Persist Security Info=True;"/>
connectionStrings>
//連線資料庫
string conStr =System.Configuration.ConfigurationManager.ConnectionStrings[1].ToString();
SqlConnection sqlCon = new SqlConnection(conStr);
sqlCon.Open();
this.Label1.Text = sqlCon.State.ToString();
//插入資料
string sqlIntoCmd="insert into info values('123','地理學','g')";
SqlCommand sqlInto = new SqlCommand(sqlIntoCmd, sqlCon);
sqlInto.ExecuteNonQuery(); //這句執行後,資料才真正插入資料庫中.
/**/////使用SqlDataReader顯示資料
//string sqlSelectCmd = "select * from info";
//SqlCommand sqlSelect = new SqlCommand(sqlSelectCmd, sqlCon);
//SqlDataReader sqlDr=sqlSelect.ExecuteReader();
//while (sqlDr.Read())
//{
// Response.Write(sqlDr[0]);
// Response.Write(sqlDr[1]);
// Response.Write(sqlDr[2]);
// Response.Write("
");
//}
//sqlDr.Close();
//使用SqlDataAdapter,DataSet顯示資料
string sqlSelectCmd = "select * from info";
SqlDataAdapter da = new SqlDataAdapter(sqlSelectCmd, sqlCon);
DataSet ds = new DataSet();
da.Fill(ds, "info");
if (ds.Tables[0].Rows.Count == 0)
{
Response.Write("NOdata");
}
else
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
Response.Write(ds.Tables[0].Rows[i][0]);
Response.Write(ds.Tables[0].Rows[i][1]);
Response.Write(ds.Tables[0].Rows[i][2]);
Response.Write("
");
}
}
/**/////使用SqlDataAdapter,SqlCommandBuilder,DataSet插入資料
//string sqlSelectCmd = "select * from info";
//SqlDataAdapter da = new SqlDataAdapter(sqlSelectCmd, sqlCon);
//SqlCommandBuilder cb = new SqlCommandBuilder(da);
//DataSet ds = new DataSet();
//da.Fill(ds, "info");
//DataRow newrow = ds.Tables[0].NewRow();
//newrow[0] = "text";
//newrow[1] = "text";
//newrow[2] = "text";
//ds.Tables[0].Rows.Add(newrow);
//da.Update(ds, "info");
sqlCon.Close();
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-444296/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Dart 學習備忘錄Dart
- 深度學習調參備忘深度學習
- jquery備忘學習筆記jQuery筆記
- Webstorm常用快捷鍵備忘(Webstorm入門指南)WebORM
- Mandrakelinux 入門學習必備資料(轉)Linux
- MySQL 8:備份&匯入【備忘】MySql
- mybatis入門學習MyBatis
- Nginx入門學習Nginx
- Vue入門學習Vue
- ROS入門學習ROS
- nuxt 入門學習UX
- GORM學習入門GoORM
- afl入門學習
- Spark入門學習Spark
- React入門學習React
- C#快速入門教程(28)—— ADO.NETC#
- Linux入門學習Linux
- MyBatisPlus入門學習MyBatis
- spring入門學習Spring
- Nacos整合學習入門
- Mybatis框架 入門學習MyBatis框架
- leaflet學習一 入門
- linux學習——入門Linux
- MyBatis入門學習(一)MyBatis
- Android學習 - 入門Android
- JavaScript入門學習學習筆記(上)JavaScript筆記
- Java入門學習-學習static的用法Java
- 初學Java的備忘錄Java
- 學習C語言的必備書籍-從入門到精通C語言
- ADO.NET 快速入門(十):過濾資料
- ADO.net學習記錄 (一) (轉)
- TS入門學習筆記筆記
- 【PostgreSQL】入門學習筆記SQL筆記
- Nacos入門學習&實踐
- 學習Java怎麼入門?Java
- Nestjs入門學習教程JS
- git入門學習筆記Git筆記
- iOS入門學習計劃iOS