当前位置:编程学习 > C#/ASP.NET >>

菜鸟问c#数据库连接问题

请问:在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,但试过不行 

我需要详细点,我是菜鸟,谢谢@! --------------------编程问答-------------------- server=(local);database=db_News;Trusted_Connection=True; --------------------编程问答-------------------- 楼主去下载一个PDF.NET框架示例源码,对你学习数据开发肯定有帮助。
http://blog.csdn.net/bluedoctor/archive/2010/01/24/5251913.aspx --------------------编程问答-------------------- http://connectionstrings.com --------------------编程问答-------------------- 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
这几种套你的看看 --------------------编程问答-------------------- 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( );   
              }      
        } --------------------编程问答--------------------  在Visual Studio里面新建一个数据连接。
然后将连接字符串拷贝出来即可。 --------------------编程问答-------------------- 其實你只要寫Data Source=.;Intial Catalog=數據庫名;User ID=sa;Password=密碼;
其他的都是自動生成的,不信你試試!! --------------------编程问答--------------------
引用 6 楼 caozhy 的回复:
在Visual Studio里面新建一个数据连接。
然后将连接字符串拷贝出来即可。


这个好  对初学者来说,不明白的话,先用系统自动生成一个,然后拷贝代码过去就可以了 --------------------编程问答-------------------- Integrated Security=SSPI;(Windows身份登录)Data Source=(你的本地计算机名称或者'.'代替);Initial Catalog=(数据库名称);Max Pool Size=120;Min Pool Size=10;

--------------------编程问答-------------------- 楼主 拖一个GridView 并连接到具体表上,可以查看到
数据库连接的配置信息。。。 --------------------编程问答--------------------
引用 8 楼 xrongzhen 的回复:
引用 6 楼 caozhy 的回复:
在Visual Studio里面新建一个数据连接。
然后将连接字符串拷贝出来即可。


这个好 对初学者来说,不明白的话,先用系统自动生成一个,然后拷贝代码过去就可以了


我还不明白,怎样新建一个数据连接?能详细步骤吗? --------------------编程问答-------------------- 菜单栏工具下有连接数据库选项 --------------------编程问答--------------------
引用 12 楼 lanfengsese 的回复:
菜单栏工具下有连接数据库选项

我用VS的服务器资源管理器建立连接,出现问题
“/demo_News”应用程序中的服务器错误。
--------------------------------------------------------------------------------

在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接 --------------------编程问答--------------------
引用 13 楼 wtoscn 的回复:
引用 12 楼 lanfengsese 的回复:
菜单栏工具下有连接数据库选项

我用VS的服务器资源管理器建立连接,出现问题
“/demo_News”应用程序中的服务器错误。
--------------------------------------------------------------------------------

在建立与服务器的连接时出错。在连接到 SQL……




有以上连接问题!! --------------------编程问答-------------------- Data Source=127.0.0.1;Initial Catalog=Test;User Id=sa;Password=XXX; --------------------编程问答-------------------- 补充楼上
Data Source=localhost;Initial Catalog=Test;User Id=sa;Password=XXX --------------------编程问答-------------------- 修改webconfig中的sever,把king换成你自己的主机名 --------------------编程问答-------------------- 在管理器中看看你的server名字是什么。推荐几个对你有帮助的网页:
http://blog.csdn.net/wyodyia/archive/2009/11/10/4795101.aspx
http://blog.csdn.net/zyq5945/archive/2010/05/13/5586423.aspx

--------------------编程问答-------------------- 别着急,慢慢来 --------------------编程问答--------------------  你拖一个sqlconnection,会有向导的 --------------------编程问答--------------------
引用 14 楼 wtoscn 的回复:
引用 13 楼 wtoscn 的回复:
引用 12 楼 lanfengsese 的回复:
菜单栏工具下有连接数据库选项

我用VS的服务器资源管理器建立连接,出现问题
“/demo_News”应用程序中的服务器错误。
--------------------------------------------------------------------------------

……
外围应用配置器设置下 --------------------编程问答--------------------

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

            return builder.ConnectionString;//连接字符串
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,