ADO.net資料訪問

來源:互聯網
上載者:User

標籤:

需要引用對應命名空間:System.Data.SqlClient;

SqlConnection:連線物件
SqlCommand:命令對象
SqlDataReader:讀取器對象

//造連接字串
string connstring = "server=.;database=mydb;user=sa;pwd=123";

//造連線物件
SqlConnection conn = new SqlConnection(connstring);

//建立命令對象
SqlCommand cmd = conn.CreateCommand();

//寫要執行的SQL語句
cmd.CommandText = "select * from Info";

//開啟串連
conn.Open();

//執行操作(讀取操作,返回讀取器對象)
SqlDataReader dr = cmd.ExecuteReader();

//執行操作(增刪改操作,返回行數)
//cmd.ExecuteNonQuery();

//處理資料
if (dr.HasRows)
{
dr.Read();
Console.WriteLine(dr[0]);
Console.ReadLine();
}
else
{
Console.WriteLine("讀取失敗!");
}

//關閉串連
conn.Close();

//dr.Read() 是資料庫資料訪問指標,每執行一次都會向下走一行,如果有內容則返回true,同時dr訪問為當前行資料集合,可以使用索引或是列名來訪問相對應的資料

 

ADO.net資料訪問

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.