ASP.NET连接SQLServer简单测试实例


以下是ASP.NET连接SQL Server的简单测试实例:

using System;using System.Data.SqlClient;namespace DemoApp{public partial class Default : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){string connectionString = "Data Source=(local);Initial Catalog=DemoDB;Integrated Security=True";string query = "SELECT * FROM Customers";using (SqlConnection connection = new SqlConnection(connectionString)){using (SqlCommand command = new SqlCommand(query, connection)){connection.Open();using (SqlDataReader reader = command.ExecuteReader()){while (reader.Read()){string customerName = reader["CustomerName"].ToString();string contactName = reader["ContactName"].ToString();string city = reader["City"].ToString();// 做你想做的事情,比如把数据显示在页面上Response.Write($"CustomerName: {customerName}, ContactName: {contactName}, City: {city}<br/>");}}}}}}}

上述代码假设你已经创建了一个名为DemoDB的数据库,并且在该数据库中已经创建了一个名为Customers的表。

在ASP.NET应用程序中,创建一个名为"Default.aspx"的页面,并将上述代码放在该页面的代码文件中。

当访问该页面时,它将连接到SQL Server数据库,从Customers表中检索数据,并将数据显示在页面上。


上一篇:linux服务器端口号怎么看

下一篇:Oracle怎么查看所有job任务


Asp.net SqlServer
Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器