asp.net with c#连接sql 2000数据库

该博客展示了使用ASP.NET和C#进行SQL数据库连接测试的代码。通过定义连接字符串,创建SqlConnection对象,尝试打开和关闭数据库连接,并对可能出现的异常进行捕获处理,以验证连接是否成功。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<%@ Page Language="c#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

void Page_Load()
{
string strConnection = "user id=netboyc;password=wd111l;";
strConnection += "initial catalog=aspx;data source=localhost1;";
strConnection += "Connect Timeout=30";
data_src.Text = strConnection;
SqlConnection objConnection = new SqlConnection(strConnection);

try
{
objConnection.Open();
con_open.Text="Connection opened successfully.<br />";
objConnection.Close();
con_close.Text="Connection closed.<br />";
}
catch (Exception e)
{
con_open.Text="Connection failed to open.<br />";
con_close.Text=e.ToString();
}
}

</script>
<html>
<head>
</head>
<body>
<h4>Testing the data connection <asp:Label id="data_src" runat="server"></asp:Label>
</h4>
<asp:Label id="con_open" runat="server"></asp:Label>
<br />
<asp:Label id="con_close" runat="server"></asp:Label>
<br />
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值