asp.net得到本机数据库实例的两种方法代码
程序员文章站
2024-02-28 19:38:46
复制代码 代码如下:<%@ page language="c#" %>
<%@ page language="c#" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<script runat="server">
protected void page_load(object sender, eventargs e)
{
//方法1
system.data.datatable table = system.data.sqlclient.sqlclientfactory.instance.createdatasourceenumerator().getdatasources();
gridview1.datasource = table;
gridview1.databind();
//得到本机的
foreach (system.data.datarow row in table.rows)
{
if (row[0].tostring().toupper().equals(system.net.dns.gethostname().toupper()))
{
response.write("<li>" + row[1]);
}
}
//方法2
//需要引用microsoft.sqlserver.smo.dll
gridview2.datasource = microsoft.sqlserver.management.smo.smoapplication.enumavailablesqlservers(true);
gridview2.databind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:gridview id="gridview1" runat="server">
</asp:gridview>
<asp:gridview id="gridview2" runat="server">
</asp:gridview>
</form>
</body>
</html>
复制代码 代码如下:
<%@ page language="c#" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<script runat="server">
protected void page_load(object sender, eventargs e)
{
//方法1
system.data.datatable table = system.data.sqlclient.sqlclientfactory.instance.createdatasourceenumerator().getdatasources();
gridview1.datasource = table;
gridview1.databind();
//得到本机的
foreach (system.data.datarow row in table.rows)
{
if (row[0].tostring().toupper().equals(system.net.dns.gethostname().toupper()))
{
response.write("<li>" + row[1]);
}
}
//方法2
//需要引用microsoft.sqlserver.smo.dll
gridview2.datasource = microsoft.sqlserver.management.smo.smoapplication.enumavailablesqlservers(true);
gridview2.databind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:gridview id="gridview1" runat="server">
</asp:gridview>
<asp:gridview id="gridview2" runat="server">
</asp:gridview>
</form>
</body>
</html>
上一篇: 判断一个数是否为素数