SqlDataSource.ConnectionString Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan atau mengatur string koneksi khusus penyedia ADO.NET yang SqlDataSource digunakan kontrol untuk menyambungkan ke database yang mendasar.
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
Nilai Properti
String khusus penyedia data .NET Framework yang SqlDataSource digunakan untuk menyambungkan ke database SQL yang diwakilinya. Defaultnya adalah string kosong ("").
Contoh
Bagian ini berisi dua contoh kode. Contoh kode pertama menunjukkan cara mengatur ConnectionString properti untuk menyambungkan ke database Microsoft SQL Server dan menampilkan hasil SelectCommand properti dalam GridView kontrol. Contoh kode kedua menunjukkan skenario yang SqlDataSource lebih kompleks, di mana kontrol digunakan untuk menampilkan dan memperbarui data dalam database Microsoft Access yang dilindungi kata sandi. Dalam setiap kasus, connectionStrings
elemen file Web.config ditampilkan terlebih dahulu, diikuti oleh halaman ASP.NET yang berisi SqlDataSource kontrol.
Contoh kode berikut menunjukkan cara mengatur ConnectionString properti untuk menyambungkan ke database SQL Server dan menampilkan hasil SelectCommand properti dalam GridView kontrol.
<%@ 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>
Contoh kode berikut menunjukkan skenario yang lebih kompleks daripada contoh kode sebelumnya, di mana SqlDataSource kontrol digunakan untuk menampilkan dan memperbarui data dalam database Access yang dilindungi kata sandi. SqlDataSource Karena digunakan dengan Access, ProviderName properti diatur ke System.Data.OleDb penyedia, dan ConnectionString properti diatur ke string koneksi yang sesuai untuk database Access yang dibagikan UNC. GridView Kontrol menampilkan pesanan dengan tanggal pengiriman. Anda dapat memperbarui pesanan dengan mencentang kotak centang yang sesuai, lalu mengklik tombol Perbarui .
Penting
Contoh ini menyertakan kata sandi dalam teks biasa hanya untuk tujuan ilustrasi. Dalam aplikasi produksi, string koneksi yang menyertakan kata sandi harus dienkripsi. Untuk informasi lebih lengkap, lihat Melindungi Informasi Koneksi.
<%@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>
Keterangan
Kontrol SqlDataSource dapat digunakan dengan berbagai penyedia ADO.NET dan sintaks string koneksi yang digunakan untuk menyambungkan ke sumber data yang mendasar khusus untuk penyedia.
Saat mengonfigurasi SqlDataSource kontrol, Anda mengatur ProviderName properti ke jenis database (defaultnya adalah System.Data.SqlClient), dan Anda mengatur ConnectionString properti ke string koneksi yang menyertakan informasi yang diperlukan untuk menyambungkan ke database. Konten string koneksi berbeda tergantung pada jenis database apa yang diakses kontrol sumber data. Misalnya, SqlDataSource kontrol memerlukan nama server, nama database (katalog), dan informasi tentang cara mengautentikasi pengguna saat menyambungkan ke SQL Server. Untuk informasi tentang konten string koneksi, lihat properti untuk SqlConnection kelas, ConnectionString properti untuk OracleConnection kelas, ConnectionString properti untuk OleDbConnection kelas, atau ConnectionString properti untuk kelas tersebutOdbcConnection.ConnectionString
Jika Anda mengubah ConnectionString properti , DataSourceChanged peristiwa dinaikkan, menyebabkan kontrol apa pun yang terikat ke SqlDataSource kontrol untuk digabungkan kembali.
Penting
Untuk informasi tentang menyimpan string koneksi, lihat Cara: Mengamankan String Koneksi saat Menggunakan Kontrol Sumber Data.