C#连接SQLServer时报错

sparadise1003 2010-09-30 11:21:28
代码如下:
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是以正常访问数据库的。
...全文
302 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
sparadise1003 2010-09-30
  • 打赏
  • 举报
回复
还是cn.Open();这句出错,怎么弄啊T_T
sparadise1003 2010-09-30
  • 打赏
  • 举报
回复

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 的连接)
wuyq11 2010-09-30
  • 打赏
  • 举报
回复
用户不存在或密码问题
在"服务器-安全性-登录名"中建立" "用户,
然后再在 "服务器-数据库-安全性-用户"中添加即可
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;
zhumingfang 2010-09-30
  • 打赏
  • 举报
回复
value ="Data Source=.\SQLEXPRESS;Initial Catalog=AutoLot;Integrated Security=SSPI"
这个错了
iey000 2010-09-30
  • 打赏
  • 举报
回复
楼上这么多人都指出了你的关键性错误:
value ="Data Source=.\SQLEXPRESS;Initial Catalog=AutoLot;Integrated Security=SSPI"
这里哪一句说你是用windows登陆?只指定了数据库是本地的
swalp 2010-09-30
  • 打赏
  • 举报
回复
你把server=你的IP,试试
yuhuiwqvb 2010-09-30
  • 打赏
  • 举报
回复
无法打开登录所请求的数据库 "AutoLot"。登录失败。
它提示的是AutoLot这个数据库没找到,你的连接字符串里这样写来看看:
server=.;database=AutoLot;integrated security=sspi;
swalp 2010-09-30
  • 打赏
  • 举报
回复
Data Source=Server;Initial Catalog=Database;Integated Security=Trusted_Connection;SSPI=True
swalp 2010-09-30
  • 打赏
  • 举报
回复
Data Source=a\\a;Initial Catalog=db_CSManage;Integrated Security=SSPI;
或者Server=a\\a;Database=db_CSManage;Trusted_Connection=True;
WINDOWS身份登录
sparadise1003 2010-09-30
  • 打赏
  • 举报
回复
我是通过windows验证登录的
yinrongg 2010-09-30
  • 打赏
  • 举报
回复
连接字符串写错了吧,自己写一个撒
在路上20130607 2010-09-30
  • 打赏
  • 举报
回复

<add key="ConnectionString" value="Database=ZLLOA0926;Server=192.168.0.222;uid=sa;pwd=sa;"/>


看看 用户名 和密码
sparadise1003 2010-09-30
  • 打赏
  • 举报
回复
搞定了,把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"/>
tjw687492 2010-09-30
  • 打赏
  • 举报
回复
防火墙关了
门关 2010-09-30
  • 打赏
  • 举报
回复
把AutoLot数据库附加到以Windows登陆方式的数据库资源管理器里试试。。。

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧