using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class stuinfo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnQuery_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
scsb.DataSource = ".\\SQLEXPRESS";
scsb.InitialCatalog = "OnlineApplyDatabase";
scsb.IntegratedSecurity = true;
using (SqlConnection con = new SqlConnection(scsb.ToString()))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandText = "select 学号 from Apply where 学号 = @学号";
cmd.Parameters.Add(new SqlParameter("学号", txtNo.Text));
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
if (con.State != ConnectionState.Open) con.Open();
adapter.Fill(dt);
}
}
}
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
GridView1.DataSource = null;
GridView1.DataBind();
ClientScript.RegisterStartupScript(this.GetType(), "NotFound", "alert('not found');",true);
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="stuinfo.aspx.cs" Inherits="stuinfo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<br> .style1<br> {<br> font-family: 楷体;<br> color: #FFFFFF;<br> }<br> .style2<br> {<br> font-size: 40pt;<br> background-color: #009933;<br> }<br>
<span class="style1"><strong><span class="style2">请选择需要查询的学号:</span></strong></span><br />
<br />
<asp:TextBox ID="txtNo" runat="server"></asp:TextBox>
<asp:Button ID="btnQuery" runat="server" onclick="btnQuery_Click"
Text="查询" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
web config:
<?xml version="1.0"?>
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\OnlineApplyDatabase.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
</providers>
</profile>
<roleManager enabled="false">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>