界面连接数据库


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



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。