asp.net 4.5 练习~test7-11

webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test7_11.WebForm1" %>

<!DOCTYPE html>

<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:Button ID="Button1" runat="server" Text="查询含有‘咖啡’的记录" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

webform1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.sqlClient;
using System.Data;

namespace test7_11
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string connectionString = @"Data Source=LAPTOP-AQKEN65V\sqlEXPRESS08;Initial Catalog=db_CoffeeManagement;User ID=sa;Password=123456;";
            sqlConnection myConnection = new sqlConnection(connectionString);
            myConnection.open();

            string selectsql = "select * from tb_shangPin where sp_name like '%咖啡%' ";
            sqlCommand myCmd = new sqlCommand(selectsql, myConnection);
            sqlDataAdapter adapter = new sqlDataAdapter();
            adapter.SelectCommand = myCmd;
            DataSet myDs = new DataSet();
            adapter.Fill(myDs);
            DataTable myDt = myDs.Tables[0];
 
            Response.Write("<h3>查询数据库记录</h3>");
            //输出表格
            Response.Write("<table border='1'>");
            //输出首行
            Response.Write("<tr bgcolor='gray'>");
            foreach(System.Data.DataColumn col in myDt.Columns)
            {
                Response.Write("<td>"+ col.ColumnName +"</td>");
            }
            Response.Write("</tr>");
            //输出其他行
            foreach(System.Data.DaTarow row in myDt.Rows)
            {
                Response.Write("<tr>");
                foreach(System.Data.DataColumn col in myDt.Columns)
                {
                    Response.Write("<td>"+ row[col].ToString() +"</td>");
                }
                Response.Write("</tr>");
            }
            //输出表尾
            Response.Write("</table>");
            //关闭连接
            myConnection.Close();

        }
    }
}

sql

CREATE DATABASE db_CoffeeManagement;
USE db_CoffeeManagement;
GO;

CREATE TABLE tb_shangPin(
sp_id int identity(1,1) not null primary key,
sp_name nvarchar(50) not null,
sp_price money,
sp_type nchar(10)
);

 

相关文章

数组的定义 Dim MyArray MyArray = Array(1‚5‚123‚12‚98...
\'参数: \'code:要检测的代码 \'leixing:html或者ubb \'n...
演示效果: 代码下载: 点击下载
环境:winxp sp2 ,mysql5.0.18,mysql odbc 3.51 driver 表采...
其实说起AJAX的初级应用是非常简单的,通俗的说就是客户端(j...
<% ’判断文件名是否合法 Function isFilename(aFilename...