.NETラーニング(四)データソースコントロールSqlDataSourceの使用
7253 ワード
このセクションで使用するデータベースはmysqlです.複雑なため、説明します.それに比べてsql serverを使うのは簡単です.Step 1:確保http://blog.csdn.net/qq_16912257/article/details/49951865前の3歩は全部終わった.付webconfig
Step 2:ListBoxコントロールを追加し、SqlDataSourceデータソースコントロールを追加する.コードは次のとおりです.
注意:SqlDataSourceコントロールデータベースからデータを取得するには、ProviderName、ConnectionsString、SelectCommandの3つのプロパティが必要です.mysqlデータベースを使用する場合は、ProviderNameをMySqlに設定する必要があります.Data.MySqlClient(筆者はここで長い時間を費やした、Q_Q)Step 3:リストボックスにデータを表示する例を表示するためにcsコードを記述する:
筆者のデータベースでは、chromeで実行した結果、
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
assemblies>
compilation>
<httpRuntime targetFramework="4.5"/>
system.web>
<connectionStrings>
<add name="connection" connectionString="Data Source=localhost;Initial Catalog=hotel;Persist Security Info=True;User ID=root;Password=123456" providerName="MySql.Data.MySqlClient"/>
connectionStrings>
configuration>
Step 2:ListBoxコントロールを追加し、SqlDataSourceデータソースコントロールを追加する.コードは次のとおりです.
"C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>title>
head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource1">asp:ListBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ProviderName="MySql.Data.MySqlClient" ConnectionString="" SelectCommand="SELECT * FROM userinfo">asp:SqlDataSource>
div>
form>
body>
html>
注意:SqlDataSourceコントロールデータベースからデータを取得するには、ProviderName、ConnectionsString、SelectCommandの3つのプロパティが必要です.mysqlデータベースを使用する場合は、ProviderNameをMySqlに設定する必要があります.Data.MySqlClient(筆者はここで長い時間を費やした、Q_Q)Step 3:リストボックスにデータを表示する例を表示するためにcsコードを記述する:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data;
using MySql.Data.MySqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ListBox1.DataTextField = "Address";
}
}
筆者のデータベースでは、chromeで実行した結果、