如果服务器的IP地址不断变化,如何更改连接字符串?

编程入门 行业动态 更新时间:2024-10-23 14:20:55
本文介绍了如果服务器的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地址不断变化,如何更改连接字符串?

本文发布于:2023-10-30 22:43:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544213.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何更改   地址   服务器   IP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!