Chapter 1__1.3 Connecting to SQL Server

<p>    <span style="background-color: #99cc00;">连接数据库  </span></p> <p>   您可以连接到一个SQL Server数据库使用SQL Server。NET数据提供程序,与OLE DB。NET数据提供程序,或ODBC。NET数据提供程序。</p> <p>  下面这个解决方案演示了一个创建和打开一个连接使用SQL Server .NET data provider, OLE DB .NET data provider, and ODBC.NET data provider。在所有情况下。都可以通过连接对象属性查看被连接的显示信息。</p> <p>&nbsp;</p> <div class="cnblogs_code"> <pre><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System;<br /></span><span style="color: #008080;"> 2</span> <span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Data.SqlClient;<br /></span><span style="color: #008080;"> 3</span> <span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Data.OleDb;<br /></span><span style="color: #008080;"> 4</span> <span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Data.Odbc;<br /></span><span style="color: #008080;"> 5</span> <span style="color: #000000;"><br /></span><span style="color: #008080;"> 6</span> <span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">namespace</span><span style="color: #000000;"> ConnectSqlServer<br /></span><span style="color: #008080;"> 7</span> <span style="color: #000000;">{<br /></span><span style="color: #008080;"> 8</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Program<br /></span><span style="color: #008080;"> 9</span> <span style="color: #000000;"> {<br /></span><span style="color: #008080;">10</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Main(</span><span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)<br /></span><span style="color: #008080;">11</span> <span style="color: #000000;"> {<br /></span><span style="color: #008080;">12</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">13</span> <span style="color: #000000;"> </span><span style="color: #008000;">//</span><span style="color: #008000;"> Connect using .NET data provider for SQL Server and integrated<br /></span><span style="color: #008080;">14</span> <span style="color: #008000;"> </span><span style="color: #008000;">//</span><span style="color: #008000;"> security</span><span style="color: #008000;"><br /></span><span style="color: #008080;">15</span> <span style="color: #008000;">&nbsp;</span><span style="color: #000000;"> </span><span style="color: #0000ff;">string</span><span style="color: #000000;"> sqlConnectString1 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">Data Source=(local);</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"><br /></span><span style="color: #008080;">16</span> <span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">Integrated security=SSPI;Initial Catalog=AdventureWorks;</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br /></span><span style="color: #008080;">17</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">18</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> (SqlConnection connection </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> SqlConnection(sqlConnectString1))<br /></span><span style="color: #008080;">19</span> <span style="color: #000000;"> {<br /></span><span style="color: #008080;">20</span> <span style="color: #000000;"> connection.Open( );<br /></span><span style="color: #008080;">21</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">22</span> <span style="color: #000000;"> </span><span style="color: #008000;">//</span><span style="color: #008000;"> Return some information about the server.</span><span style="color: #008000;"><br /></span><span style="color: #008080;">23</span> <span style="color: #008000;">&nbsp;</span><span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">---.NET data provider for SQL Server </span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"><br /></span><span style="color: #008080;">24</span> <span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">with Windows Authentication mode---</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br /></span><span style="color: #008080;">25</span> <span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">ConnectionString = {0}n</span><span style="color: #800000;">"</span><span style="color: #000000;">, sqlConnectString1);<br /></span><span style="color: #008080;">26</span> <span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">State = {0}</span><span style="color: #800000;">"</span><span style="color: #000000;">, connection.State);<br /></span><span style="color: #008080;">27</span> <span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">DataSource = {0}</span><span style="color: #800000;">"</span><span style="color: #000000;">, connection.DataSource);<br /></span><span style="color: #008080;">28</span> <span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">ServerVersion = {0}</span><span style="color: #800000;">"</span><span style="color: #000000;">, connection.ServerVersion);<br /></span><span style="color: #008080;">29</span> <span style="color: #000000;"> }<br /></span><span style="color: #008080;">30</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">31</span> <span style="color: #000000;"> </span><span style="color: #008000;">//</span><span style="color: #008000;"> Connect using .NET data provider for SQL Server and SQL Server<br /></span><span style="color: #008080;">32</span> <span style="color: #008000;"> </span><span style="color: #008000;">//</span><span style="color: #008000;"> authentication</span><span style="color: #008000;"><br /></span><span style="color: #008080;">33</span> <span style="color: #008000;">&nbsp;</span><span style="color: #000000;"> </span><span style="color: #0000ff;">string</span><span style="color: #000000;"> sqlConnectString2 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">Data Source=(local);</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"><br /></span><span style="color: #008080;">34</span> <span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">User Id=sa;Password=password;Initial Catalog=AdventureWorks;</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br /></span><span style="color: #008080;">35</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">36</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> (SqlConnection connection </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> SqlConnection(sqlConnectString2))<br /></span><span style="color: #008080;">37</span> <span style="color: #000000;"> {<br /></span><span style="color: #008080;">38</span> <span style="color: #000000;"> connection.Open( );<br /></span><span style="color: #008080;">39</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">40</span> <span style="color: #000000;"> </span><span style="color: #008000;">//</span><span style="color: #008000;"> Return some information about the server.</span><span style="color: #008000;"><br /></span><span style="color: #008080;">41</span> <span style="color: #008000;">&nbsp;</span><span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">n---.NET data provider for SQL Server </span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"><br /></span><span style="color: #008080;">42</span> <span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">with SQL Server Authentication mode---</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br /></span><span style="color: #008080;">43</span> <span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">ConnectionString = {0}n</span><span style="color: #800000;">"</span><span style="color: #000000;">, sqlConnectString2);<br /></span><span style="color: #008080;">44</span> <span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">State = {0}</span><span style="color: #800000;">"</span><span style="color: #000000;">, connection.State);<br /></span><span style="color: #008080;">45</span> <span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">DataSource = {0}</span><span style="color: #800000;">"</span><span style="color: #000000;">, connection.DataSource);<br /></span><span style="color: #008080;">46</span> <span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">ServerVersion = {0}</span><span style="color: #800000;">"</span><span style="color: #000000;">, connection.ServerVersion);<br /></span><span style="color: #008080;">47</span> <span style="color: #000000;"> }<br /></span><span style="color: #008080;">48</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">49</span> <span style="color: #000000;"> </span><span style="color: #008000;">//</span><span style="color: #008000;"> Connect using .NET data provider for OLE DB.</span><span style="color: #008000;"><br /></span><span style="color: #008080;">50</span> <span style="color: #008000;">&nbsp;</span><span style="color: #000000;"> </span><span style="color: #0000ff;">string</span><span style="color: #000000;"> oledbConnectString </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">Provider=SQLOLEDB;Data Source=(local);</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"><br /></span><span style="color: #008080;">51</span> <span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">Initial Catalog=AdventureWorks;User Id=sa;Password=password;</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br /></span><span style="color: #008080;">52</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">53</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> (OleDbConnection connection </span><span style="color: #000000;">=</span><span style="color: #000000;"><br /></span><span style="color: #008080;">54</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> OleDbConnection(oledbConnectString))<br /></span><span style="color: #008080;">55</span> <span style="color: #000000;"> {<br /></span><span style="color: #008080;">56</span> <span style="color: #000000;"> connection.Open( );<br /></span><span style="color: #008080;">57</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">58</span> <span style="color: #000000;"> </span><span style="color: #008000;">//</span><span style="color: #008000;"> Return some information about the server.</span><span style="color: #008000;"><br /></span><span style="color: #008080;">59</span> <span style="color: #008000;">&nbsp;</span><span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">n---.NET data provider for OLE DB---</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br /></span><span style="color: #008080;">60</span> <span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">ConnectionString = {0}n</span><span style="color: #800000;">"</span><span style="color: #000000;">, oledbConnectString);<br /></span><span style="color: #008080;">61</span> <span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">State = {0}</span><span style="color: #800000;">"</span><span style="color: #000000;">, connection.State);<br /></span><span style="color: #008080;">62</span> <span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">DataSource = {0}</span><span style="color: #800000;">"</span><span style="color: #000000;">, connection.DataSource);<br /></span><span style="color: #008080;">63</span> <span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">ServerVersion = {0}</span><span style="color: #800000;">"</span><span style="color: #000000;">, connection.ServerVersion);<br /></span><span style="color: #008080;">64</span> <span style="color: #000000;"> }<br /></span><span style="color: #008080;">65</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">66</span> <span style="color: #000000;"> </span><span style="color: #008000;">//</span><span style="color: #008000;"> Connect using .NET data provider for ODBC.</span><span style="color: #008000;"><br /></span><span style="color: #008080;">67</span> <span style="color: #008000;">&nbsp;</span><span style="color: #000000;"> </span><span style="color: #0000ff;">string</span><span style="color: #000000;"> odbcConnectString </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">Driver={SQL Native Client};</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"><br /></span><span style="color: #008080;">68</span> <span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">Server=(local);Database=AdventureWorks;uid=sa;pwd=password;</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br /></span><span style="color: #008080;">69</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">70</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> (OdbcConnection connection </span><span style="color: #000000;">=</span><span style="color: #000000;"><br /></span><span style="color: #008080;">71</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> OdbcConnection(odbcConnectString))<br /></span><span style="color: #008080;">72</span> <span style="color: #000000;"> {<br /></span><span style="color: #008080;">73</span> <span style="color: #000000;"> connection.Open( );<br /></span><span style="color: #008080;">74</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">75</span> <span style="color: #000000;"> </span><span style="color: #008000;">//</span><span style="color: #008000;"> Return some information about the server.</span><span style="color: #008000;"><br /></span><span style="color: #008080;">76</span> <span style="color: #008000;">&nbsp;</span><span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">n---.NET data provider for ODBC---</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br /></span><span style="color: #008080;">77</span> <span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">ConnectionString = {0}n</span><span style="color: #800000;">"</span><span style="color: #000000;">, odbcConnectString);<br /></span><span style="color: #008080;">78</span> <span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">State = {0}</span><span style="color: #800000;">"</span><span style="color: #000000;">, connection.State);<br /></span><span style="color: #008080;">79</span> <span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">DataSource = {0}</span><span style="color: #800000;">"</span><span style="color: #000000;">, connection.DataSource);<br /></span><span style="color: #008080;">80</span> <span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">ServerVersion = {0}</span><span style="color: #800000;">"</span><span style="color: #000000;">, connection.ServerVersion);<br /></span><span style="color: #008080;">81</span> <span style="color: #000000;"> }<br /></span><span style="color: #008080;">82</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">83</span> <span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">nPress any key to continue.</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br /></span><span style="color: #008080;">84</span> <span style="color: #000000;"> Console.ReadKey( );<br /></span><span style="color: #008080;">85</span> <span style="color: #000000;"> }<br /></span><span style="color: #008080;">86</span> <span style="color: #000000;"> }<br /></span><span style="color: #008080;">87</span> <span style="color: #000000;">}<br /></span><span style="color: #008080;">88</span> <br />运行结果如下:<span style="color: #000000;">&nbsp;</span></div></pre> </div> <p>&nbsp;</p> <p><img src="http://pic002.cnblogs.com/img/%e5%be%ae%e8%bd%af%e4%b8%ad%e5%9b%bd/201002/2010022803250878.jpg" /></p> <p>得出的结论就是:我们不光可以使用native SQL Server 还可以使用OLE DB, and ODBC</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>1.1.3.1Native SQL Server</p> <p>&nbsp;&nbsp;&nbsp;&nbsp; 在Microsoft SQL Server。NET数据提供程序访问在7.0版本开始使用一个内部协议。它在<tt>System.Data.SqlClient命名空间下。一个集成连接的例子在下面:</tt></p> <p><tt> <div class="cnblogs_code"> <pre><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;"><tt>Data Source</tt></span><span style="color: #000000;"><tt>=</tt></span><span style="color: #000000;"><tt>(local);Integrated security</tt></span><span style="color: #000000;"><tt>=</tt></span><span style="color: #000000;"><tt>SSPI;Initial Catalog</tt></span><span style="color: #000000;"><tt>=</tt></span><span style="color: #000000;"><tt>AdventureWorks;<br /></tt></span></div></pre> </div> <p>&nbsp;</p> <br /></tt></p> <p class="docText">Without integrated security, the connection string is:</p> <p class="docText"> <pre class="brush:csharp"><div class="cnblogs_code"><pre><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;">Data Source</span><span style="color: #000000;">=</span><span style="color: #000000;">(local);User Id</span><span style="color: #000000;">=</span><span style="color: #000000;">sa;Password</span><span style="color: #000000;">=</span><span style="color: #000000;">password;Initial Catalog</span><span style="color: #000000;">=</span><span style="color: #000000;">AdventureWorks;<br /><br /></span></div></pre> </div> <p>&nbsp;</p> <br />Native providers比OLE DB or ODBC providers表现的更好。因为他们少了一个间接的一个层。 Microsoft SQL Server。NET是微软自家产品。。。<br /></pre> </p> <h5 class="docSection3Title" id="title-ID0EW4NM">1.4.3.2. OLE DB的连接方法</h5> <p class="docText"><a name="with the"></a>You can use the OLE DB .NET data provider with the SQL Server OLE DB provider (SQL Server or SQL Native Client) to access SQL Server. An example of the connection string is shown here:</p> <p> <div class="cnblogs_code"> <pre><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;">Provider</span><span style="color: #000000;">=</span><span style="color: #000000;">SQLOLEDB;Data Source</span><span style="color: #000000;">=</span><span style="color: #000000;">(local);Initial Catalog</span><span style="color: #000000;">=</span><span style="color: #000000;">AdventureWorks;<br /> User Id</span><span style="color: #000000;">=</span><span style="color: #000000;">sa;Password</span><span style="color: #000000;">=</span><span style="color: #000000;">password;<br /><br /></span></div></pre> </div> </p> <h5 class="docSection3Title" id="title-ID0E64NM">1.4.3.3. ODBC的连接方法。</h5> <p>Finally, the ODBC .NET data provider can connect to an SQL Server database. An example of the connection string is shown here:</p> <p> <div class="cnblogs_code"> <pre><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;">Driver</span><span style="color: #000000;">=</span><span style="color: #000000;">{SQL Native Client};Server</span><span style="color: #000000;">=</span><span style="color: #000000;">(local);Database</span><span style="color: #000000;">=</span><span style="color: #000000;">AdventureWorks;<br /> uid</span><span style="color: #000000;">=</span><span style="color: #000000;">sa;pwd</span><span style="color: #000000;">=</span><span style="color: #000000;">password;</span></div></pre> </div> <p>&nbsp;</p> </p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p>

你可能感兴趣的:(SQL Server)