代码如下:
App.config文件<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="provider" value ="System.Data.SqlClient"/>
        <add key="cnStr" value ="Data Source=.\SQLEXPRESS;Initial Catalog=AutoLot;Integrated Security=SSPI"/>        
    </appSettings>
</configuration>
Program.cs文件using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.Common;namespace DataProviderFactory
{
    class Program
    {
        static void Main(string[] args)
        {
            //从config文件获取连接字符串和提供程序
            string dp = ConfigurationManager.AppSettings["provider"];
            string cnStr = ConfigurationManager.AppSettings["cnStr"];            //得到工厂提供程序
            DbProviderFactory df = DbProviderFactories.GetFactory(dp);            //得到连接对象
            DbConnection cn = df.CreateConnection();
            Console.WriteLine("Your connection object is a :{0}" , cn.GetType().FullName);
            cn.ConnectionString = cnStr;
            cn.Open();            //得到命令对象
            DbCommand cmd = df.CreateCommand();
            Console.WriteLine("Your command object is a :{0}", cmd.GetType().FullName);
            cmd.Connection = cn;
            cmd.CommandText = "Select * From Inventory";            //从数据读取器输出数据
            DbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            Console.WriteLine("Your reader object is a :{0}", dr.GetType().FullName);            Console.WriteLine( "\n****Current Inventory****" );
            while (dr.Read())
                Console.WriteLine("->Car #{0} is a {1}." , dr["CarID"] , dr["Make"].ToString().Trim() );
            dr.Close();
            Console.ReadLine();
        }
    }
}现在的问题是,程序执行到cn.Open();就报错了,错语信息是:无法打开登录所请求的数据库 "AutoLot"。登录失败。但是我用VS2008自带的“服务器资源管理器”和SSMSE是以正常访问数据库的。

解决方案 »

  1.   


      <add key="ConnectionString" value="Database=ZLLOA0926;Server=192.168.0.222;uid=sa;pwd=sa;"/>
    看看 用户名 和密码
      

  2.   

    Data Source=a\\a;Initial Catalog=db_CSManage;Integrated Security=SSPI;   
    或者Server=a\\a;Database=db_CSManage;Trusted_Connection=True;  
    WINDOWS身份登录
      

  3.   

    Data Source=Server;Initial Catalog=Database;Integated Security=Trusted_Connection;SSPI=True
      

  4.   

    无法打开登录所请求的数据库 "AutoLot"。登录失败。
    它提示的是AutoLot这个数据库没找到,你的连接字符串里这样写来看看:
    server=.;database=AutoLot;integrated security=sspi;
      

  5.   

    楼上这么多人都指出了你的关键性错误:
     value ="Data Source=.\SQLEXPRESS;Initial Catalog=AutoLot;Integrated Security=SSPI"
    这里哪一句说你是用windows登陆?只指定了数据库是本地的
      

  6.   

    value ="Data Source=.\SQLEXPRESS;Initial Catalog=AutoLot;Integrated Security=SSPI"
    这个错了
      

  7.   

    用户不存在或密码问题
    在"服务器-安全性-登录名"中建立" "用户,
    然后再在 "服务器-数据库-安全性-用户"中添加即可 
    Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;
      

  8.   


    value ="Data Source=.\SQLEXPRESS;Initial Catalog=AutoLot;Integrated Security=SSPI"
    这句我改成这样了
    value ="Data Source=.\SQLEXPRESS;SERVER=.;Initial Catalog=AutoLot;Integrated Security=SSPI" 这次报这个错误了:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接)
      

  9.   

    还是cn.Open();这句出错,怎么弄啊T_T
      

  10.   

    把AutoLot数据库附加到以Windows登陆方式的数据库资源管理器里试试
      

  11.   

    搞定了,把VS2008“服务器资源管理器”-数据库属性下的连接字符串拷到文件中就可以了,如下所示<add key="cnStr" value ="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AutoLot.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"/>