Changing a SQL Server Connection String in an ASP.NET Application
I'm a web developer and I work primarily in Django -- I've never used ASP.Net or anything like that before, but I'm tr开发者_StackOverflow中文版oubleshooting a problem for a friend's parents / client so I need some advice!
It's pretty simple: I need to change the parameters of their SQL Server database connection because of some server upgrades that happened at their hosting company -- is it ok for me to go in and just edit a .cs file, or does it need to be compiled or some other business?
Literally just need to change the parameters of
mySqlConn = new SqlConnection("server=hostname;database=dbname;uid=username;pwd=password;");
You will need to recompile the .cs file with its project, and deploy the resulting DLL file.
(Move the connection string out of code, while you're at it. That's bad practice, for just this reason. Read it from an application config file like web.config instead)
C# is compiled to the CLR Runtime so I would think you would need to make the change and recompile the code.
To avoid this in the future, read the hostname, dbname, username and password from a config file.
If, as Mitch said, the connection string is typed in code, then you will need to change the string (please pull it out and put it in a config file or some other reference) and recompile.
If the .cs file is already deployed to the web directory then you can change it there. It is not often recommended but it is possible to run an ASP.NET web application off of uncompiled code, the IIS server compiles and caches it for web requests. However, if the deployed site is compiled already (you won't see any .cs files) then you will have to update it, compile, and deploy the newly compiled project back to the web host.
Changing the connection in a .cs file will change the compile so you will have to recompile and deploy the application .dll back to the server.
Once you compile you can just take the .dll and the .cs file you changed.
Ideally you'd make connection strings in web.config and reference them so you wouldn't have to do this. As an example, in Web.config you'd do...
<connectionStrings>
<add
name="NorthwindConnectionString"
connectionString="Data Source=serverName;Initial
Catalog=Northwind;Persist Security Info=True;User
ID=userName;Password=password"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
in .cs file you'd reference like so
string connString = rootWebConfig.ConnectionStrings.ConnectionStrings["NorthwindConnectionString"];
精彩评论