【问题标题】:C# change to different connection string to SQL Server when can't connect to first one当无法连接到第一个时,C# 更改为与 SQL Server 不同的连接字符串
【发布时间】:2017-04-04 14:03:16
【问题描述】:

我想在家里和外面都连接到 SQL Server 数据库。我可以通过两种方式进行连接,但是每次我在家时都需要将连接字符串从公共 IP 更改为本地。是否可以编写一个函数,当它无法通过远程 ip 连接时在连接字符串中使用其他主机名? 请帮助我是初学者:)

【问题讨论】:

  • 显示你迄今为止尝试过的内容。
  • 当然。您将在配置文件中放置两个连接字符串。当打开与一个的连接失败时,您尝试与另一个连接。

标签: c# sql-server connection-string


【解决方案1】:

试试这个:

//.....
//.....
//..... YOUR CODE

     try
    {

        con = new SqlConnection(check_Active_Connection_String()); // Add this
        con.Open();

        SqlCommand r = new SqlCommand("SELECT Login FROM Users WHERE Login not like '%Wszyscy%'", con);

//.... YOUR CODE
//.... YOUR CODE

希望对您有所帮助。

【讨论】:

  • 我在公共部分类 Form1 之后添加了我的字符串并添加了您的代码:表单是正确的吗?我收到 {WindowsFormsApplication1.exe 中发生“System.NullReferenceException”类型的未处理异常
  • 它还必须在第 1 行显示错误。 ?检查该错误是否在我的代码行或任何其他?
  • 如果您编辑您的代码并通过添加我的代码对其进行修改,我们将不胜感激,以便我检查一下?
  • 在 con.Open() 之前;你还没有初始化 con 对象?请更正您的代码。
  • 有效!谢谢大哥!
【解决方案2】:

这是我添加你的 Dheeraj 后的代码。错误在 con.Open();线

命名空间 WindowsFormsApplication1

{
公共部分类Form1:表格 {

    SqlConnection con;
    private string CS_1 = @"Data Source=99.88.88.156,33400\SQLMINFOR;Initial Catalog=Minfor;Integrated Security=False;Password=****;User ID=sa;";
    private string CS_2 = @"Data Source=BACKUP-MIN\SQLMIN;Initial Catalog=Minfor;Integrated Security=False;Password=****;User ID=sa;";

    public string check_Active_Connection_String()
    {
        string main_CS = "";
        if (try_CS(CS_1))
        {
            main_CS = CS_1;
        }
        else if (try_CS(CS_2))
        {
            main_CS = CS_2;
        }

        return main_CS;// use main_CS for your connection string further
    }

    private bool try_CS(string CS)
    {
        try
        {
            using (con = new SqlConnection(CS))
            {
                con.Open();
                return true;
            }
        }
        catch (Exception exp)
        {
            return false;
        }
    }
    public Form1()

    {
        InitializeComponent();
        textBox2.PasswordChar = '*';
        try
        {
            con.Open();

            SqlCommand r = new SqlCommand("SELECT Login FROM Users WHERE Login not like '%Wszyscy%'", con);
            SqlDataReader dr = r.ExecuteReader(); 

            while (dr.Read())
            {
                comboBox1.Items.Add(dr["Login"]);
            }
            dr.Close();
            dr.Dispose();


        }
        catch(SqlException ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.Exit();
        }


    }

【讨论】:

  • 在打开连接之前写这个:con = new SqlConnection(check_Active_Connection_String());
  • 检查我的答案,我已经修改了。
  • 如果您的 IP\Server 发生变化怎么办?您将不得不更改您的代码。最好将这些连接字符串放在您的应用配置文件中,以便以后更轻松地更改它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 1970-01-01
  • 2011-02-24
  • 1970-01-01
  • 2018-05-30
相关资源
最近更新 更多