C#列出局域网中可用SQL Server服务器

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
public partial class ConnectDataBase_ConfigConnection : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            System.Data.DataTable table = null;
            SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
            table = instance.GetDataSources();
            ViewState["table"] = table;
            dropdownLoaddata(table);
        }
    }

    private void dropdownLoaddata(System.Data.DataTable table)
    {
        this.ddlSQLServer.DataSource = table;
        this.ddlSQLServer.DataTextField = "ServerName";
        this.ddlSQLServer.DataValueField = "InstanceName";
        this.ddlSQLServer.DataBind();
        LoadData(table);
    }
    protected void ddlSQLServer_SelectedIndexChanged(object sender, EventArgs e)
    {
        System.Data.DataTable table = (System.Data.DataTable)ViewState["table"];
        LoadData(table);
    }

    private void LoadData(System.Data.DataTable table)
    {
        foreach(System.Data.DataRow row in table.Rows)
        {
            if (row["ServerName"].Equals(this.ddlSQLServer.SelectedItem.Text))
            {
                this.lblServerName.Text = row["ServerName"].ToString();
                this.lblInstance.Text = row["InstanceName"].ToString();
                this.lblIsClustered.Text = row["IsClustered"].ToString();
                if (row["Version"].ToString().Substring(0, 1) == "8")
                {
                    this.lblVersion.Text = "SQL 2000";
                }
                else if (row["Version"].ToString().Substring(0, 1) == "9")
                {
                    this.lblVersion.Text = "SQL 2005";
                }
            }
        }
    }
    protected void btnConnect_Click(object sender, EventArgs e)
    {
        string connectString = "server=";
        string serverName = this.ddlSQLServer.SelectedItem.Text;
        connectString += serverName + ";" + "database=";
        string database = "master";
        connectString += database + ";" + "user=";
        string userName = txtUserName.Text;
        connectString += userName + ";" + "pwd=";
        string pwd = txtPwd.Text;
        connectString += pwd;
        SqlConnection con = new SqlConnection(connectString);
        try
        {
            con.Open();
            con.Close();
            this.lblMessage.Text = "连接成功";
            ScriptManager.RegisterStartupScript(this.btnConnect, this.GetType(), "alertscript", "alert('连接成功');", true);
            //Response.Write("<script>alert('连接成功');</script>");
            //ScriptManager.RegisterStartupScript(this.Button1, this.GetType(), "alertScript", "window.open('default2.aspx');", true);
            //其中第一个参数为要注册脚本的控件ID,试了一下,只要是本页面的就行。
            //第二个参数为注册脚本控件类型,是控件还是this的GetType()都可以,typeOf(string)也没问题.
            //第三个脚本函数的名字,随便起。
            //第四个是脚本内容。
            //第五个是标明是否再添加脚本标签,如果第四个参数里包含了 <script> </script>标签,此处则为false,否则为true。
        }
        catch (Exception ee)
        {
            this.lblMessage.Text = ee.Message;
            ScriptManager.RegisterStartupScript(this.btnConnect, this.GetType(), "alertscript", "alert('连接失败');", true);
        }
    }
   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值