学堂 学堂 学堂公众号手机端

ado.net的连接显示实例

lewis 1年前 (2024-04-04) 阅读数 6 #技术



从昨天晚上开始调试,终于调试成功,下面是代码。




web.config中的部分。

<configuration>

<add name="sqlconn"

connectionString="server=LILIZONG;database=dangwei;uid=sa;" providerName="System.Data.SqlClient"/>

</connectionStrings>

显示页的cs部分。


using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;


public partial class test : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if(!Page.IsPostBack){TestCommandReader();}

}



private void TestCommandReader()

{

String myConnectionString=ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;

SqlConnection myConn=new SqlConnection(myConnectionString);

//SqlConnection myConn=new SqlConnection(myConnectionString);

String cmdText="select * from geren";

SqlCommand myCommand=new SqlCommand(cmdText,myConn);



try

{

myConn.Open();

SqlDataReader mydr=myCommand.ExecuteReader();

while(mydr.Read())

{

Userlist.Items.Add(new ListItem((String)mydr["name"]));}



mydr.Close();

SqlDataReader mydrOther=myCommand.ExecuteReader();

//UserOtherList.DateSource=mydrOther;

UserOtherList.DataSource = mydrOther;


//UserOtherList.datatextfield="name";

//UserOtherList.datavaluefield = "birthday";

//UserOtherList.databind();

//mydrOther.close();

UserOtherList.DataTextField = "name";

UserOtherList.DataValueField = "birthday";

UserOtherList.DataBind();

mydrOther.Close();

}

catch(SqlException sqlex)

{

Response.Write(sqlex.Message+"<br>");

}

finally{

myConn.Close();

}

}

protected void Userlist_SelectedIndexChanged(object sender, EventArgs e)

{


}

}


显示页aspx部分:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="/news/upload/ueditor/image/202208/zwjbrbqfwyy" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<asp:ListBox ID="Userlist" runat="server" Height="400px" Width="300px">

</asp:ListBox>

<asp:ListBox ID="UserOtherList" runat="server" Height="400px" Width="300px" ></asp:ListBox>

</form>

</body>

</html>


版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门