欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

网上购物系统(Task000)——经典的数据库连接

程序员文章站 2022-06-26 08:55:40
using System;  using System.Data;    using System.Data.SqlClient; ...

using System; 
using System.Data; 
 
using System.Data.SqlClient; 
 
namespace WestGarden.Web 

    public partial class Default : System.Web.UI.Page 
    { 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            string connectionString = "Server=.\\SQLEXPRESS;Database=NetShop;Trusted_Connection=SSPI"; 
            string cmdText = "SELECT * FROM Category"; 
 
            SqlConnection conn = new SqlConnection(); 
            conn.ConnectionString = connectionString; 
 
            SqlCommand cmd = new SqlCommand(); 
            cmd.Connection = conn; 
            cmd.CommandType = CommandType.Text; 
            cmd.CommandText = cmdText; 
 
            conn.Open(); 
 
            SqlDataReader rdr = cmd.ExecuteReader(); 
            while (rdr.Read()) 
            { 
                Response.Write(rdr["Name"] + "<br/>"); 
            } 
 
            rdr.Close(); 
            conn.Close();   
        } 
    } 

 

作者 yousuosi