Asp.net C#数据库连接 [英] Asp.net C# database connection

查看:86
本文介绍了Asp.net C#数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手问题..



在Web.config或公共方法中配置连接字符串的最佳方法是什么?



我试图创建连接到数据库的Web项目[MSSQL SERVER 2008 R2] ..



我有什么试过:



newbie question..

What is the best way to configure the connection string, in Web.config or in a public method?

im trying to create web project connected to a database [MSSQL SERVER 2008 R2]..

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace asp
{
    public class connection
    {
        public string constring()
        {
            string str = @"SERVER=MACHUCA-PC\SQLEXPRESS;DATABASE=testdb;TRUSTED_CONNECTION=True";

            return str;
        }
    }
}

推荐答案

Web.config



如果您对字符串进行硬编码,则必须在代码更改时重新编译代码。



每次进行维护时它都会更改,或者升级。因为如果没有,您的代码将对生产数据库进行测试,而您所做的一切都会带来灾难。如果您忘记了UPDATE命令的WHERE子句,例如:并且所有客户都更改了其传递地址。或者您测试降低销售价格,并且每个人都可以在线免费获得产品。这些事情发生在开发中:如果你正在攻击不是生产的开发DB,那就没什么大不了的了。对生产进行测试是一个非常糟糕的举动!



如果你硬编码字符串,你必须在发布之前更改你的(经过测试和完善的)代码 - 你可以' t测试那些变化,除了生产,每个错误都会导致停机和客户。
Web.config

If you hard code the string you have to recompile the code when it changes.

And it changes every time you do maintenance, or upgrades. Because if it doesn't, your code tests against the production database and everything you do risks disaster. If you forget the WHERE clause for an UPDATE command for example: and all customers get their delivery address changed. Or you test a sale price reduction and everybody online gets the product for free perhaps. These things happen in development: and provided you are working against a development DB which is not the production one, it's no biggie. Testing against production is a very bad move!

If you hard code strings, you have to change your (tested, and perfect) code before release - and you can't test those changes except in production, where every mistake costs downtime and customers.


这篇关于Asp.net C#数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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