微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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)
);

 

原文地址:https://blog.csdn.net/modern358/article/details/114526777

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐