u011097980 2016-07-03 02:51 采纳率: 0%
浏览 1247

C#实现delphi程序中DBConn.udl数据库链接工具,请高手解答疑惑

第1步可以获取所有区域网内的服务器名,但是第三步骤无法从特定的服务器名下,通过用户名和密码,得到数据库

代码:
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条回答 默认 最新

  • devmiao 2016-07-03 06:28
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题