1、获取本机的SQL Serverserver名
private void Form2_Load(object sender, EventArgs e) { listBox1.Items.Clear(); SQLDMO.Application SQLServer = new SQLDMO.Application(); SQLDMO.NameList strServer = SQLServer.ListAvailableSQLServers(); if (strServer.Count > 0) { for (int i = 0; i < strServer.Count; i++) { listBox1.Items.Add(strServer.Item(i + 1)); } } }
2、连接特定的数据库
private void button1_Click(object sender, EventArgs e) { DBUser.sserver = "."; DBUser.DBName = "Goodluck"; DBUser.suser = "sa"; DBUser.spasswd = "abc"; MyDBase DB = new MyDBase(DBUser.sserver, DBUser.DBName, DBUser.suser, DBUser.spasswd); DataSet DS = DB.GetRecordset("use master;select name as Sname from sysdatabases"); comboBox1.DataSource = DS.Tables[0]; comboBox1.DisplayMember = "Sname"; comboBox1.ValueMember = "Sname"; DB.DBClose(); }
版权声明:本文博客原创文章,博客,未经同意,不得转载。