SqlDataSource.ConnectionString プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
基になるデータベースに接続するために SqlDataSource コントロールが使用する ADO.NET プロバイダー固有の接続文字列を取得または設定します。
public:
virtual property System::String ^ ConnectionString { System::String ^ get(); void set(System::String ^ value); };
public virtual string ConnectionString { get; set; }
member this.ConnectionString : string with get, set
Public Overridable Property ConnectionString As String
プロパティ値
が表す SQL データベースへの接続に使用するSqlDataSource、.NET Frameworkデータ プロバイダー固有の文字列。 既定値は、空の文字列 ("") です。
例
このセクションには、2 つのコード例が含まれています。 最初のコード例では、 プロパティを設定ConnectionStringして Microsoft SQL Server データベースに接続し、プロパティの結果をSelectCommandコントロールに表示する方法をGridView示します。 2 番目のコード例は、 SqlDataSource パスワードで保護された Microsoft Access データベースのデータを表示および更新するためにコントロールを使用する、より複雑なシナリオを示しています。 いずれの場合も、 connectionStrings
Web.config ファイルの要素が最初に表示され、その後にコントロールを含む ASP.NET ページが SqlDataSource 表示されます。
次のコード例では、 プロパティを設定ConnectionStringしてSQL Server データベースに接続し、プロパティの結果をコントロールに表示するSelectCommand方法をGridView示します。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" 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>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" 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>
次のコード例は、前のコード例よりも複雑なシナリオを示しています。このシナリオでは、 SqlDataSource コントロールを使用して、パスワードで保護された Access データベースのデータを表示および更新します。 SqlDataSourceは Access で使用されるため、 ProviderName プロパティはプロバイダーにSystem.Data.OleDb設定されConnectionString、プロパティは UNC 共有 Access データベースに適した接続文字列に設定されます。 コントロールには GridView 、出荷日を含む注文が表示されます。 適切なチェック ボックスをオンにして、[更新] ボタンをクリックすると、注文を更新できます。
重要
この例では、説明のためにのみプレーン テキストでパスワードを含めます。 運用アプリケーションでは、パスワードを含む接続文字列を暗号化する必要があります。 詳細については、「接続情報の保護」を参照してください。
<%@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 xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" 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="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 xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" 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>
<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>
<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>
注釈
コントロールは SqlDataSource さまざまな ADO.NET プロバイダーと共に使用でき、基になるデータ ソースへの接続に使用される接続文字列の構文はプロバイダーに固有です。
コントロールを SqlDataSource 構成するときは、 プロパティを ProviderName データベースの種類 (既定値は System.Data.SqlClient) に設定し、データベースに接続するために必要な情報を含む接続文字列に プロパティを設定 ConnectionString します。 接続文字列の内容は、データ ソース管理がアクセスしているデータベースの種類によって異なります。 たとえば、コントロールにはSqlDataSource、サーバー名、データベース (カタログ) 名、およびSQL Serverに接続するときにユーザーを認証する方法に関する情報が必要です。 接続文字列の内容については、クラスの プロパティ、クラスのConnectionStringプロパティSqlConnection、ConnectionStringクラスのOracleConnectionプロパティOleDbConnection、ConnectionStringまたは クラスの プロパティをConnectionStringOdbcConnection参照してください。
プロパティを ConnectionString 変更すると、 DataSourceChanged イベントが発生し、コントロールに SqlDataSource バインドされているコントロールが再バインドされます。
重要
接続文字列の格納の詳細については、「 方法: データ ソース管理を使用するときに接続文字列をセキュリティで保護する」を参照してください。
適用対象
こちらもご覧ください
.NET
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示