[sql server] 串連sql server – .net

來源:互聯網
上載者:User

 .net串連sql server主要方式有:SqlClient、OleDB、ODBC

 

1、SqlClient專用於微軟的MSSQL,因此SqlClient速度會塊些。

串連串的形式:"server=localhost;database=northwind;uid=sa;pwd=;"

或者"Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind"

 

System.Data.SqlClient.SqlConnection conn = new SqlConnection("server=.;database=master;uid=sa;pwd=;");<br />conn.Open();<br />SqlCommand cmd=new SqlCommand("select * from tb",conn);<br />System.Data.SqlClient.SqlDataReader myDR=cmd.ExecuteReader();<br />while (myDR.Read()) //如果未到尾記錄<br />{<br /> Response.Write(myDR.GetString(0)); //0所代表的是第一個欄位,然後下一個是1<br /> Response.Write("<br>");<br />}<br />myDR.close();<br />conn.close(); </p><p>

2、OleDB

OleDbConnection nwindConn = new OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;Integrated"+<br />" Security=SSPI;Initial Catalog=northwind"); </p><p>OleDbCommand salesCMD = new OleDbCommand("select * from tb", nwindConn);</p><p>nwindConn.Open();</p><p>OleDbDataReader myReader = salesCMD.ExecuteReader();</p><p>while (myReader.Read())<br />{<br /> Console.WriteLine("/t{0}, ${1}", myReader.GetString(0), myReader.GetDecimal(1));<br />}</p><p>myReader.Close();<br />nwindConn.Close();<br />

 

 

 

3、ODBC

 

OdbcConnection nwindConn = new OdbcConnection("Driver={SQL Server};Server=localhost;Trusted_Connection=yes;" +<br /> "Database=northwind");<br />nwindConn.Open();</p><p>OdbcCommand salesCMD = new OdbcCommand("select * from tb", nwindConn);</p><p>OdbcDataReader myReader = salesCMD.ExecuteReader();<br />while (myReader.Read())<br />{<br /> Console.WriteLine("/t{0}, ${1}", myReader.GetString(0), myReader.GetDecimal(1));<br />}</p><p>myReader.Close();<br />nwindConn.Close();

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.