要烂熟于胸的代码(1)-ADO.NET数据查询
2016-03-20 08:34
579 查看
空网站
配置部分:
<?xml version="1.0"?>
<!--
注意: 除了手动编辑此文件以外,您还可以使用
Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
“网站”->“Asp.Net 配置”选项。
设置和注释的完整列表在
machine.config.comments 中,该文件通常位于
\Windows\Microsoft.Net\Framework\v2.x\Config 中
-->
<configuration>
<appSettings>
<add key="ConnectionString" value="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\DataBase\db_09.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"/>
</appSettings>
<connectionStrings/>
<system.web>
<!--
设置 compilation debug="true" 将调试符号插入
已编译的页面中。但由于这会
影响性能,因此只在开发过程中将此值
设置为 true。
-->
<compilation debug="true" targetFramework="4.0"/>
<!--
通过 <authentication> 节可以配置 ASP.NET 使用的
安全身份验证模式,
以标识传入的用户。
-->
<authentication mode="Windows"/>
<!--
如果在执行请求的过程中出现未处理的错误,
则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
开发人员通过该节可以配置
要显示的 html 错误页
以代替错误堆栈跟踪。
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
</configuration>
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>使用Command对象查询数据库中记录</title>
<style >
body:{margin-top:0px}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style=" font-size :9pt">
<tr>
<td>
请输入姓名:<asp:TextBox ID="txtName" runat="server" Width="78px"></asp:TextBox>
<asp:Button ID="btnSelect" runat="server" OnClick="btnSelect_Click" Text="查询" /></td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.bind();
}
}
public SqlConnection GetConnection()
{
string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection myConn = new SqlConnection(myStr);
return myConn;
}
protected void bind()
{
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select * from tb_Student ";
SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
GridView1.DataSource = myDs;
GridView1.DataBind();
myDa.Dispose();
myDs.Dispose();
myConn.Close();
}
protected void btnSelect_Click(object sender, EventArgs e)
{
if (this.txtName.Text != "")
{
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select * from tb_Student where
Name=@Name";
SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
myCmd.Parameters.Add("@Name", SqlDbType.VarChar, 20).Value = this.txtName.Text.Trim();
SqlDataAdapter myDa = new SqlDataAdapter(myCmd);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
if (myDs.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = myDs;
GridView1.DataBind();
}
else
{
Response.Write("<script>alert('没有相关记录')</script>");
}
myDa.Dispose();
myDs.Dispose();
myConn.Close();
}
else
this.bind();
}
}
配置部分:
<?xml version="1.0"?>
<!--
注意: 除了手动编辑此文件以外,您还可以使用
Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
“网站”->“Asp.Net 配置”选项。
设置和注释的完整列表在
machine.config.comments 中,该文件通常位于
\Windows\Microsoft.Net\Framework\v2.x\Config 中
-->
<configuration>
<appSettings>
<add key="ConnectionString" value="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\DataBase\db_09.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"/>
</appSettings>
<connectionStrings/>
<system.web>
<!--
设置 compilation debug="true" 将调试符号插入
已编译的页面中。但由于这会
影响性能,因此只在开发过程中将此值
设置为 true。
-->
<compilation debug="true" targetFramework="4.0"/>
<!--
通过 <authentication> 节可以配置 ASP.NET 使用的
安全身份验证模式,
以标识传入的用户。
-->
<authentication mode="Windows"/>
<!--
如果在执行请求的过程中出现未处理的错误,
则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
开发人员通过该节可以配置
要显示的 html 错误页
以代替错误堆栈跟踪。
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
</configuration>
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>使用Command对象查询数据库中记录</title>
<style >
body:{margin-top:0px}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style=" font-size :9pt">
<tr>
<td>
请输入姓名:<asp:TextBox ID="txtName" runat="server" Width="78px"></asp:TextBox>
<asp:Button ID="btnSelect" runat="server" OnClick="btnSelect_Click" Text="查询" /></td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.bind();
}
}
public SqlConnection GetConnection()
{
string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection myConn = new SqlConnection(myStr);
return myConn;
}
protected void bind()
{
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select * from tb_Student ";
SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
GridView1.DataSource = myDs;
GridView1.DataBind();
myDa.Dispose();
myDs.Dispose();
myConn.Close();
}
protected void btnSelect_Click(object sender, EventArgs e)
{
if (this.txtName.Text != "")
{
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select * from tb_Student where
Name=@Name";
SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
myCmd.Parameters.Add("@Name", SqlDbType.VarChar, 20).Value = this.txtName.Text.Trim();
SqlDataAdapter myDa = new SqlDataAdapter(myCmd);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
if (myDs.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = myDs;
GridView1.DataBind();
}
else
{
Response.Write("<script>alert('没有相关记录')</script>");
}
myDa.Dispose();
myDs.Dispose();
myConn.Close();
}
else
this.bind();
}
}
相关文章推荐
- 以OLE方式读写EXCEL的C++类【转载】
- 使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用----20135334赵阳林
- C++进阶—— helper function 的设计与实现
- C#完整执行存储过程的代码加实例
- ASP.NET web 应用程序项目
- python中dir(), hasattr(), getattr(), setattr()的使用
- python中的装饰器Decorator用法
- C++实验2—标准体重
- C语言实现单链表面试题汇总
- PHP中变量的声明、作用范围和解析规则
- struts2 配置文件示范说明 xml 格式和 properties 格式
- 20145208《Java程序设计》第3周学习总结
- Java开发流程自动化(svn+maven+nexus+jenkins) --自动编译,持续集成,测试,打包,发布/部署
- 第一个python程序
- 手把手教你如何配置Spring JDBC
- C语言32个关键字
- C++有子对象的派生类的构造函数
- 刚刚开始学习编程
- C语言32个关键字
- 函数式编程