当前位置:编程学习 > asp >>

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

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

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,