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);
}
}
}