C#後台擷取資料庫資料--ADO.NET

來源:互聯網
上載者:User

標籤:.text   sql   string   reader   執行   ring   資料庫   read   span   

關於擷取資料庫中的資料到後台,可分為4個步驟:1.擷取串連資料庫的字串;2.建立SqlConnection對象用於開啟資料庫;3.建立SqlCommand對象用於執行操作,設定它的一系列屬性即可;4.關閉資料庫連接。完成了這個四個步驟就可擷取到資料庫中的資料。

            //第一步            string connStr = "Data Source=.;Initial CataLog=MyData;User ID=sa;Password=123456";            //第二步            SqlConnection conn = new SqlConnection(connStr);            conn.Open();            //第三步            SqlCommand cmd = new SqlCommand();            cmd.Connection = conn;            cmd.CommandType = CommandType.Text;            string sqlText = "select * from UserInfo";                     cmd.CommandText = sqlText;                        SqlDataReader dr = cmd.ExecuteReader();            while (dr.Read())            {                txtBox1.Text = dr["userName"].ToString();            }            dr.Close();            //第四步            conn.Close();

 

把握這個步驟,就可以完成對資料庫的操作。

C#後台擷取資料庫資料--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.