如果服务器的IP地址不断变化,如何更改连接字符串? [英] how to change the connection string if server's ip address keeps changing?

查看:151
本文介绍了如果服务器的IP地址不断变化,如何更改连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在开发一个在LAN中运行的应用程序。应用程序准备就绪并且正在运行但不知何故服务器的IP地址发生变化并且应用程序崩溃。



这是我的program.cs,其中我定义了连接到服务器的函数。

Hello,
I am developing an application which runs in LAN.Application is ready and is working but somehow server's ip address changes and application crashes.

this is my program.cs in which i have defined the function which connects to the server.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Vikrant_Studio
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        public static MySqlConnection con;
        
        public static void connect2server()
        {
            try
            {
                string connection = "SERVER=192.168.1.3; Database=vikrant_studio; Uid=root;"; //for main deployment
              
                con = new MySqlConnection(connection);
                con.Open();
            }
            catch(Exception ex)
            {
                MessageBox.Show("You may not be a member of this Network,Please Contact the Administrator to get access.");

            }


        }


        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Login());
        }
    }
}









我需要在服务器的IP地址改变时更改我的连接字符串。

或者我可以做的是让ip地址静态,因为我已经知道在局域网中ipaddresses改变了吗?





I need to change my connection string when server's ip address changes.
Or what i can do is to make ip addresses static as i have learned that ipaddresses change while in LAN?

推荐答案

您可以通过路由器设置为数据库服务器提供静态本地IP。如何访问这些设置取决于路由器,但您可以尝试 192.168.1.1 192.168.0.1 。如何为设备提供静态IP也取决于路由器,但您可以尝试查找地址保留之类的部分并将其设置在那里。然后,您可以在连接字符串中使用静态IP。
You can give the database server a static local IP through your router settings. How to access these settings depends on the router, but you can try 192.168.1.1 or 192.168.0.1. How to give a device a static IP also depends on the router, but you can try to look for a section like "Address Reservation" and set it there. Then you can use the static IP in your connection string.


首先,不要对连接字符串进行硬编码。在app.config或web.config中使用连接字符串部分。其次,您可以使用DNS名称而不是IP地址。顺便说一句,如果是服务器,你应该使用固定IP地址(如果你有dhcp,则为reserver地址)。如果从isp获取ip,请使用ddns服务。
First of all, don't hardcode connection string. Use connection string section in app.config or web.config. Secondly, you can use dns name instead of ip address. By the way, in case of servers you should use fixed ip address (reserver address if you have dhcp). If you get your ip from isp, use a ddns service.


这篇关于如果服务器的IP地址不断变化,如何更改连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆