请问:在21日学C#书中,最后的案例讲解中,要用Web.config文件编写代码连接数据库,书中源代码如下:<add key="ConnString" value="server=king;uid=sa;pwd=sa2005;database=db_News;Enlist=true;Pooling=true;Max Pool Size=3000;Min Pool Size=0;Connection Lifetime=3000;packet size=3000"/> 
问题是:我的数据库用WINDOW用户登录的,怎样修改上面的代码才能连接到数据库?听说要uid=sa;pwd=sa2005;  换成integraged   Security=SSPI,但试过不行 我需要详细点,我是菜鸟,谢谢@!

解决方案 »

  1.   

    server=(local);database=db_News;Trusted_Connection=True;
      

  2.   

    楼主去下载一个PDF.NET框架示例源码,对你学习数据开发肯定有帮助。
    http://blog.csdn.net/bluedoctor/archive/2010/01/24/5251913.aspx
      

  3.   

    http://connectionstrings.com
      

  4.   

    Persist Security Info=False; User Id=sa ; Password=123456; Initial Catalog=test ; Data Source = SEVEN\SQLEXPRESS
    Integrated Security=SSPI; Initial Catalog=test ; Data Source = SEVEN\SQLEXPRESS
    Integrated Security=SSPI; Initial Catalog=test ; Server = SEVEN\SQLEXPRESS
    Integrated Security=SSPI; Database=test ; Server = SEVEN\SQLEXPRESS
    这几种套你的看看
      

  5.   

    private static string _ConString = "Data Source=" + _ServerName + ";Initial Catalog=goldidea;Integrated Security=True";    public static List<UserInfo> LoadUserList()
            {
                SqlConnection con = new SqlConnection(_ConString);
                                                                                                                                   
                  try
                  {
                      //连接数据库中                            
                      SqlCommand cmd = new SqlCommand();
                      cmd.Connection = con;
                      con.Open();
                      //查询数据库中 
                      cmd.CommandText = "SELECT * FROM User1";       
                      SqlDataReader dr = cmd.ExecuteReader();                  List<UserInfo> userLst = new List<UserInfo>();
                      while (dr.Read( ))
                      {
                          List<object> ReadUserDateBase = new List<object>();
                          for (int index = 0; index < dr.FieldCount;index++ )
                          {   
                              ReadUserDateBase.Add(dr[index]);
                          }
                          UserInfo user = new UserInfo(ReadUserDateBase);
                          userLst.Add(user);             
                      }
                      dr.Close();
                      return userLst;
                  }
                  catch (Exception)
                  {
                      return null;
                  }
                  finally
                  {
                      //关闭连接
                      con.Close( );   
                  }      
            }
      

  6.   

     在Visual Studio里面新建一个数据连接。
    然后将连接字符串拷贝出来即可。
      

  7.   

    其實你只要寫Data Source=.;Intial Catalog=數據庫名;User ID=sa;Password=密碼;
    其他的都是自動生成的,不信你試試!!
      

  8.   

    Integrated Security=SSPI;(Windows身份登录)Data Source=(你的本地计算机名称或者'.'代替);Initial Catalog=(数据库名称);Max Pool Size=120;Min Pool Size=10;
      

  9.   

    楼主 拖一个GridView 并连接到具体表上,可以查看到
    数据库连接的配置信息
      

  10.   

    我用VS的服务器资源管理器建立连接,出现问题
    “/demo_News”应用程序中的服务器错误。
    --------------------------------------------------------------------------------在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接
      

  11.   

    Data Source=127.0.0.1;Initial Catalog=Test;User Id=sa;Password=XXX;
      

  12.   

    补充楼上
    Data Source=localhost;Initial Catalog=Test;User Id=sa;Password=XXX
      

  13.   

    修改webconfig中的sever,把king换成你自己的主机名
      

  14.   

    在管理器中看看你的server名字是什么。推荐几个对你有帮助的网页:
    http://blog.csdn.net/wyodyia/archive/2009/11/10/4795101.aspx
    http://blog.csdn.net/zyq5945/archive/2010/05/13/5586423.aspx
      

  15.   

     你拖一个sqlconnection,会有向导的
      

  16.   


    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                builder.DataSource = "";//数据库服务器
                builder.InitialCatalog = "";//数据库名称
                builder.IntegratedSecurity = "";//获取或设置一个布尔值,该值指示是否在连接中指定用户 ID 和密码(值为 false 时),或者是否使用当前的 Windows 帐户凭据进行身份验证(值为true 时)。
                builder.UserID = "";//用户名
                builder.Password = "";//密码            return builder.ConnectionString;//连接字符串