简体   繁体   English

asp.net 中的连接字符串问题

[英]problem in connectionstring in asp.net

private string conString = "Data Source=173.56.33.6;Database=musicapp;User ID=guest;Password=guest"; I was working on local database at that time my application was successfully interacting with mysql database.when put the database on server, my application still taking the old connection string and data is stored in local database and not on server.当时我正在处理本地数据库,我的应用程序与 mysql 数据库成功交互。当将数据库放在服务器上时,我的应用程序仍然采用旧的连接字符串,数据存储在本地数据库中而不是服务器上。 what is wrong?怎么了?

I'd remove hard coded connection strings all together.我会一起删除硬编码的连接字符串。 There is a dedicated section of your config file for this very purpose:为此目的,您的配置文件中有一个专用部分:

<connectionStrings>
    <add name="MusicApp" connectionString="Data Source=173.56.33.6;Database=musicapp;User ID=guest;Password=guest;" />
</connectionStrings>

Which you can then read out:然后你可以读出:

string connection = ConfigurationManager.ConnectionStrings["MusicApp"].ConnectionString;

if Data Source=173.56.33.6;如果数据源=173.56.33.6; is the location of your server database try this instead Data Source=\173.56.33.6;是您的服务器数据库的位置 试试这个 Data Source=\173.56.33.6;

I think your problem is that you have the connection string hard-coded in your code (as a private string that you show above).我认为您的问题是您的代码中硬编码了连接字符串(作为您在上面显示的私有字符串)。 A much better way is to store it in the config file, use Settings in VS and select ConnectionString as type.更好的方法是将其存储在配置文件中,使用 VS 中的 Settings 和 select ConnectionString 作为类型。

Make sure whether you updated your connection string when you transfered your DB to server.确保在将数据库传输到服务器时是否更新了连接字符串。 In any case it is best to store connection string in web.config, so that you can modify it when ever your db is changed or transferred to another location.在任何情况下,最好将连接字符串存储在 web.config 中,以便在您的数据库更改或转移到另一个位置时对其进行修改。 This change in connection string in your web.config wouldn't require you to rebuild your application. web.config 中连接字符串的这种更改不需要您重新构建应用程序。 Although if your connection string is hard-coded in code, then you would require to rebuild your application when ever you change the connection string.尽管如果您的连接字符串在代码中是硬编码的,那么当您更改连接字符串时,您将需要重新构建您的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM