SqlDataSource.ConnectionString プロパティ
アセンブリ: System.Web (system.web.dll 内)

Dim instance As SqlDataSource Dim value As String value = instance.ConnectionString instance.ConnectionString = value
/** @property */ public String get_ConnectionString () /** @property */ public void set_ConnectionString (String value)
public function get ConnectionString () : String public function set ConnectionString (value : String)
SqlDataSource が表す SQL データベースに接続するために使用する .NET Framework データ プロバイダ固有の文字列。既定値は空の文字列 ("") です。

SqlDataSource コントロールは、さまざまな ADO.NET プロバイダと共に使用でき、基になるデータ ソースへの接続に使用される接続文字列の構文はプロバイダに固有です。
ConnectionString プロパティを変更すると、DataSourceChanged イベントが発生し、SqlDataSource コントロールにバインドされたコントロールがバインドし直されます。
![]() |
---|
接続文字列の格納方法については、「方法 : データ ソース コントロールを使用するときに接続文字列をセキュリティ保護する」を参照してください。 |
Topic | Location |
---|---|
方法 : SqlDataSource コントロールを使用して ODBC データベースに接続する | ASP .NET Web アプリケーションの作成 |
方法 : SqlDataSource コントロールを使用して ODBC データベースに接続する (Visual Studio) | Visual Studio での ASP .NET Web アプリケーションの作成 |

このセクションには、2 つのコード例が含まれています。ConnectionString プロパティを設定して Microsoft SQL Server データベースに接続し、SelectCommand プロパティの結果を GridView コントロールに表示する方法を最初のコード例に示します。より複雑なシナリオとして、SqlDataSource コントロールを使用して、パスワードで保護された Microsoft Access データベースのデータを表示および更新する方法を 2 番目のコード例に示します。
ConnectionString プロパティを設定して SQL Server データベースに接続し、SelectCommand プロパティの結果を GridView コントロールに表示する方法のコード例を次に示します。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML> <BODY> <FORM runat="server"> <asp:SqlDataSource id="SqlDataSource1" runat="server" DataSourceMode="DataReader" ConnectionString="<%$ ConnectionStrings:MyNorthwind%>" SelectCommand="SELECT FirstName, LastName, Title FROM Employees"> </asp:SqlDataSource> <asp:GridView id="GridView1" runat="server" DataSourceID="SqlDataSource1"> </asp:GridView> </FORM> </BODY> </HTML>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML> <BODY> <FORM runat="server"> <asp:SqlDataSource id="SqlDataSource1" runat="server" DataSourceMode="DataReader" ConnectionString="<%$ ConnectionStrings:MyNorthwind%>" SelectCommand="SELECT FirstName, LastName, Title FROM Employees"> </asp:SqlDataSource> <asp:GridView id="GridView1" runat="server" DataSourceID="SqlDataSource1"> </asp:GridView> </FORM> </BODY> </HTML>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML> <BODY> <FORM runat="server"> <asp:SqlDataSource id="SqlDataSource1" runat="server" DataSourceMode="DataReader" ConnectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;" SelectCommand="SELECT FirstName, LastName, Title FROM Employees"> </asp:SqlDataSource> <asp:GridView id="GridView1" runat="server" DataSourceID="SqlDataSource1"> </asp:GridView> </FORM> </BODY> </HTML>
前のコード例より複雑なシナリオとして、SqlDataSource コントロールを使用して、パスワードで保護された Access データベースのデータを表示および更新する方法のコード例を次に示します。SqlDataSource は Access で使用されるため、ProviderName プロパティは System.Data.OleDb プロバイダに設定され、ConnectionString プロパティは UNC 共有 Access データベースの適切な接続文字列に設定されます。GridView コントロールには、注文と出荷日が表示されます。適切なチェック ボックスをオンにして、[更新] ボタンをクリックすると、注文を更新できます。
<%@Page Language="VB" %> <%@Import Namespace="System.Data" %> <%@Import Namespace="System.Data.Common" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <SCRIPT runat="server"> Private Sub UpdateRecords(source As Object, e As EventArgs) ' This method is an example of batch updating using a ' data source control. The method iterates through the rows ' of the GridView, extracts each CheckBox from the row and, if ' the CheckBox is checked, updates data by calling the Update ' method of the data source control, adding required parameters ' to the UpdateParameters collection. Dim cb As CheckBox Dim row As GridViewRow For Each row In GridView1.Rows cb = CType(row.Cells(0).Controls(1), CheckBox) If cb.Checked Then Dim oid As String oid = CType(row.Cells(1).Text, String) Dim param1 As New Parameter("date", TypeCode.DateTime, DateTime.Now.ToString()) MyAccessDataSource.UpdateParameters.Add(param1) Dim param2 As New Parameter("orderid", TypeCode.String, oid) MyAccessDataSource.UpdateParameters.Add(param2) MyAccessDataSource.Update() MyAccessDataSource.UpdateParameters.Clear() End If Next End Sub ' UpdateRecords </SCRIPT> <HTML> <BODY> <FORM runat="server"> <!-- Security Note: The SqlDataSource uses a QueryStringParameter, Security Note: which does not perform validation of input from the client. Security Note: To validate the value of the QueryStringParameter, handle the Selecting event. --> <asp:SqlDataSource id="MyAccessDataSource" runat="server" ProviderName="<%$ ConnectionStrings:MyPasswordProtectedAccess.providerName%>" ConnectionString="<%$ ConnectionStrings:MyPasswordProtectedAccess%>" SelectCommand="SELECT OrderID, OrderDate, RequiredDate, ShippedDate FROM Orders WHERE EmployeeID=?" UpdateCommand="UPDATE Orders SET ShippedDate=? WHERE OrderID = ?"> <SelectParameters> <asp:QueryStringParameter Name="empId" QueryStringField="empId" /> </SelectParameters> </asp:SqlDataSource> <asp:GridView id ="GridView1" runat="server" DataSourceID="MyAccessDataSource" AllowPaging="True" PageSize="10" AutoGenerateColumns="False"> <columns> <asp:TemplateField HeaderText=""> <ItemTemplate> <asp:CheckBox runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Order" DataField="OrderID" /> <asp:BoundField HeaderText="Order Date" DataField="OrderDate" /> <asp:BoundField HeaderText="Required Date" DataField="RequiredDate" /> <asp:BoundField HeaderText="Shipped Date" DataField="ShippedDate" /> </columns> </asp:GridView> <asp:Button id="Button1" runat="server" Text="Update the Selected Records As Shipped" OnClick="UpdateRecords" /> <asp:Label id="Label1" runat="server" /> </FORM> </BODY> </HTML>
<%@Page Language="C#" %> <%@Import Namespace="System.Data" %> <%@Import Namespace="System.Data.Common" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <SCRIPT runat="server"> private void UpdateRecords(Object source, EventArgs e) { // This method is an example of batch updating using a // data source control. The method iterates through the rows // of the GridView, extracts each CheckBox from the row and, if // the CheckBox is checked, updates data by calling the Update // method of the data source control, adding required parameters // to the UpdateParameters collection. CheckBox cb; foreach(GridViewRow row in this.GridView1.Rows) { cb = (CheckBox) row.Cells[0].Controls[1]; if(cb.Checked) { string oid = (string) row.Cells[1].Text; MyAccessDataSource.UpdateParameters.Add(new Parameter("date" ,TypeCode.DateTime,DateTime.Now.ToString())); MyAccessDataSource.UpdateParameters.Add(new Parameter("orderid" ,TypeCode.String,oid)); MyAccessDataSource.Update(); MyAccessDataSource.UpdateParameters.Clear(); } } } </SCRIPT> <HTML> <BODY> <FORM runat="server"> <!-- Security Note: The SqlDataSource uses a QueryStringParameter, Security Note: which does not perform validation of input from the client. Security Note: To validate the value of the QueryStringParameter, handle the Selecting event. --> <asp:SqlDataSource id="MyAccessDataSource" runat="server" ProviderName="<%$ ConnectionStrings:MyPasswordProtectedAccess.providerName%>" ConnectionString="<%$ ConnectionStrings:MyPasswordProtectedAccess%>" SelectCommand="SELECT OrderID, OrderDate, RequiredDate, ShippedDate FROM Orders WHERE EmployeeID=?" UpdateCommand="UPDATE Orders SET ShippedDate=? WHERE OrderID = ?"> <SelectParameters> <asp:QueryStringParameter Name="empId" QueryStringField="empId" /> </SelectParameters> </asp:SqlDataSource> <asp:GridView id ="GridView1" runat="server" DataSourceID="MyAccessDataSource" AllowPaging="True" PageSize="10" AutoGenerateColumns="False"> <columns> <asp:TemplateField HeaderText=""> <ItemTemplate> <asp:CheckBox runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Order" DataField="OrderID" /> <asp:BoundField HeaderText="Order Date" DataField="OrderDate" /> <asp:BoundField HeaderText="Required Date" DataField="RequiredDate" /> <asp:BoundField HeaderText="Shipped Date" DataField="ShippedDate" /> </columns> </asp:GridView> <asp:Button id="Button1" runat="server" Text="Update the Selected Records As Shipped" OnClick="UpdateRecords" /> <asp:Label id="Label1" runat="server" /> </FORM> </BODY> </HTML>
<%@Page Language="VJ#" %> <%@Import Namespace="System.Data" %> <%@Import Namespace="System.Data.Common" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <SCRIPT runat="server"> private void UpdateRecords(Object source, System.EventArgs e) { // This method is an example of batch updating using a // data source control. The method iterates through the rows // of the GridView, extracts each CheckBox from the row and, if // the CheckBox is checked, updates data by calling the Update // method of the data source control, adding required parameters // to the UpdateParameters collection. CheckBox cb; for (int iCtr = 0; iCtr < this.GridView1.get_Rows().get_Count(); iCtr++) { GridViewRow row = this.GridView1.get_Rows().get_Item(iCtr); cb = (CheckBox)row.get_Cells().get_Item(0).get_Controls().get_Item(1); if (cb.get_Checked()) { String oid = (String)(row.get_Cells().get_Item(1).get_Text()); MyAccessDataSource.get_UpdateParameters().Add(new Parameter( "date", System.TypeCode.DateTime, System.DateTime.get_Now().ToString())); MyAccessDataSource.get_UpdateParameters().Add(new Parameter( "orderid", System.TypeCode.String, oid)); MyAccessDataSource.Update(); MyAccessDataSource.get_UpdateParameters().Clear(); } } } //UpdateRecords </SCRIPT> <HTML> <BODY> <FORM runat="server"> <!-- Security Note: The SqlDataSource uses a QueryStringParameter, Security Note: which does not perform validation of input from the client. Security Note: To validate the value of the QueryStringParameter, handle the Selecting event. --> <asp:SqlDataSource id="MyAccessDataSource" runat="server" ProviderName="System.Data.OleDb" ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\uncpath\Northwind_PasswordProtected.mdb;Mode=3;Jet OLEDB:Database Password=myPassword;" SelectCommand="SELECT OrderID, OrderDate, RequiredDate, ShippedDate FROM Orders WHERE EmployeeID=?" UpdateCommand="UPDATE Orders SET ShippedDate=? WHERE OrderID = ?"> <SelectParameters> <asp:QueryStringParameter Name="empId" QueryStringField="empId" /> </SelectParameters> </asp:SqlDataSource> <asp:GridView id ="GridView1" runat="server" DataSourceID="MyAccessDataSource" AllowPaging="True" PageSize="10" AutoGenerateColumns="False"> <Columns> <asp:TemplateField HeaderText=""> <ItemTemplate> <asp:CheckBox runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Order" DataField="OrderID" /> <asp:BoundField HeaderText="Order Date" DataField="OrderDate" /> <asp:BoundField HeaderText="Required Date" DataField="RequiredDate" /> <asp:BoundField HeaderText="Shipped Date" DataField="ShippedDate" /> </Columns> </asp:GridView> <asp:Button id="Button1" runat="server" Text="Update the Selected Records As Shipped" OnClick="UpdateRecords" /> <asp:Label id="Label1" runat="server" /> </FORM> </BODY> </HTML>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- SqlDataSource.ConnectionString プロパティのページへのリンク