using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using StudentManager.com.huixin.util; using System.Collections; using System.Data.ProviderBase; using SQLDMO; using System.Data.SqlClient; namespace StudentManager { public partial class DataSourceConfig : Form { //连接数据库的类型 private String sqlConType = ""; public DataSourceConfig() { InitializeComponent(); } //配置数据库链接的窗体关闭时,打开相对应登录窗体 private void DataSourceConfig_FormClosed(object sender, FormClosedEventArgs e) { try { //杀死进程 //System.Diagnostics.Process.GetCurrentProcess().Kill(); //关闭当前窗体 //终止当前进程并为基础操作系统提供指定的退出代码 //System.Environment.Exit(System.Environment.ExitCode); this.Dispose(); this.Close(); //打开登录窗体 LoginForm lf = new LoginForm(); lf.Show(); // LoginForm.loginFrm.Show(); } catch (Exception ex) { Console.WriteLine("连接数据的窗体进程出错了:" + ex.Message); } } //显示组合框的下拉部分时发生 private void comboBox1_DropDown(object sender, EventArgs e) { this.comboBox1.DataSource = SqlLocator.GetLocalSqlServerNamesWithSqlClientFactory(); this.comboBox1.DisplayMember = "ServerName"; } //当用户点击刷新按钮时,将局域网内服务器和实例名给comobox private void button1_Click(object sender, EventArgs e) { this.comboBox1.DataSource = SqlLocator.GetLocalSqlServerNamesWithSqlClientFactory(); this.comboBox1.DisplayMember = "ServerName"; } //在窗体加载时,将Sql Server按钮以及和他相关两个文本框禁用 private void DataSourceConfig_Load(object sender, EventArgs e) { //Windows的按钮默认被选中 this.radioButton1.Select(); this.textBox1.Enabled = false; this.textBox2.Enabled = false; } private void radioButton1_Click(object sender, EventArgs e) { if (this.radioButton1.Checked == true) { sqlConType = "0"; } } private void radioButton2_CheckedChanged(object sender, EventArgs e) { if (this.radioButton2.Checked == true) { sqlConType = "1"; this.textBox1.Enabled = true; this.textBox2.Enabled = true; } } //3.点击数据库链接时显示时,组合框的下拉部分时发生 private void comboBox2_DropDown(object sender, EventArgs e) { //通过第一步输入的服务器实例名,从而找到步骤三中的数据库 //通过判断用户点击的属于哪一种数据库连接方式,从而判断采用什么方法 if (sqlConType == "0") { //用户选择的是Windows方式 this.comboBox2.DataSource = GetDataBaseList(this.comboBox1.SelectedItem.ToString()); this.comboBox2.DisplayMember = ""; } else if (sqlConType == "1") { //用户选择了Sql Server方式 this.comboBox2.DataSource = GetDataBaseList(this.comboBox1.SelectedItem.ToString(),this.textBox1.Text.Trim().ToString(),this.textBox2.Text.Trim().ToString()); this.comboBox2.DisplayMember = ""; } } //得到指定SQL服务器所有数据库的列表 //这种输入Sql Server模式,需要用户名和密码 public ArrayList GetDataBaseList(string ServerName, string UserName, string Pwd) { //一般使用SQLDMO,只能用于sql server2005一般的版本 ArrayList list = new ArrayList(); SqlConnection conn = new SqlConnection("Uid=" + UserName + ";Pwd=" + Pwd + ";Data Source=SQL2012;Integrated Security=false;"); ; SqlCommand cmd = new SqlCommand("select name from sys.databases where database_id > 4", conn); ; try { if (conn.State == ConnectionState.Closed){ conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { list.Add(dr[0]); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (conn != null && conn.State == ConnectionState.Open) { conn.Close(); conn.Dispose(); //释放资源 } } /** SQLDMO.Application sqlApp = new SQLDMO.Application(); SQLDMO.SQLServer oServer = new SQLDMO.SQLServer("Data Source="+ServerName+";Persist Security Info=True;UID="+UserName+";PWD="+Pwd+""); oServer.Connect(ServerName,UserName,Pwd); foreach (SQLDMO.Database db in oServer.Databases) { if ((db.Name != null) && (db.SystemObject == false)) { list.Add(db.Name); } } **/ return list; } //这种属于Windows方式 //此时链接数据库时,需要加integrated security=SSPI public ArrayList GetDataBaseList(string ServerName) { //一般使用SQLDMO,只能用于sql server2005一般的版本 ArrayList list = new ArrayList(); SqlConnection conn = new SqlConnection("Data Source=" + ServerName + ";Integrated Security=SSPI;Initial Catalog=mwyqms_2016-03-25"); SqlCommand cmd = new SqlCommand("select name from sys.databases where database_id > 4", conn); ; try { if (conn.State == ConnectionState.Closed) { conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { list.Add(dr[0]); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (conn != null && conn.State == ConnectionState.Open) { conn.Close(); conn.Dispose(); //释放资源 } } return list; } } }
第1步可以获取所有区域网内的服务器名,但是第三步骤无法从特定的服务器名下,通过用户名和密码,得到数据库
代码:
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。