界面连接数据库

界面连接数据库_第1张图片
1、获取本机的SQL Server服务器名
        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();
        }



你可能感兴趣的:(界面连接数据库)