zhangjunjie1107 发表于 2013-1-28 13:04:15

ADO.NET 01

     今天讲一下关于数据库连接字符串,我知道的有三种,估计也就这三种,可能写的形式又不同但基本的思路还是一样的,一种是SqlConnection con = new SqlConnection("server=.;database=vote;uid=sa;pwd=;");
                return con;
     这种的话就是连接字符串比较难记,容易忘记,当然如果你用的是sql2005的话也可以投机取巧,在属性面板上可以找到!
      第二种:   use builder连接
        //SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();  ---建一个builder实例
        //builder.DataSource = ".";   --点代表本机
        //builder.InitialCatalog = "vote";    --这个是数据库的名字
        //builder.IntegratedSecurity = false;    --这个表示非WINDOWS认证
        //builder.UserID = "sa";        --数据库的账号
        //builder.Password = "";         ---数据库的密码
        //SqlConnection con = new SqlConnection(builder.ToString());
        //return con;
     这种的话就比较清晰,容易看懂!
  
    第三种:就是把字符串放入web.config 这个又好处也有坏处,具体的自己可以考虑下,如果不知道可以踩下,留个贴子!
              System.Configuration.ConnectionStringSettings  setting      =System.Configuration.ConfigurationManager.ConnectionStrings["strCon"];          -----接受web.config中的字符串!
<connectionStrings>
    <add name="strCon" connectionString="server=.;database=vote;uid=sa;pwd=;"/>
  </connectionStrings>
        SqlConnection con = new SqlConnection(setting.ConnectionString);
        return con;
 
 我做了个小视频,要的朋友留下QQ邮箱
页: [1]
查看完整版本: ADO.NET 01