#region Using Directives
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
#endregionnamespace ConsoleApplication18
{
class Program
{
static void Main(string[] args)
{
SqlConnection thisConnection = new SqlConnection(
@"Server=(local)\sqlexpress;Integrated Security = True;" +
"Database=Northwind");
thisConnection.Open();
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText =
"select customerid,companyname from customers";
//execute datereader for specified command
SqlDataReader thisReader = thisCommand.ExecuteReader();
while (thisReader.Read())
{
Console.WriteLine("\t{0}\t{1}",
thisReader["customerid"], thisReader["companyname"]);
}
thisReader.Close();
thisConnection.Close();
Console.ReadLine(); }
}
}
以上代码老是连不上SQL 2005 ,提示问题在thisConnection.Open();原因是在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错)
但是我已经开通了 SQL 2005的 TCP/IP 和 named pipes(b)服务
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
#endregionnamespace ConsoleApplication18
{
class Program
{
static void Main(string[] args)
{
SqlConnection thisConnection = new SqlConnection(
@"Server=(local)\sqlexpress;Integrated Security = True;" +
"Database=Northwind");
thisConnection.Open();
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText =
"select customerid,companyname from customers";
//execute datereader for specified command
SqlDataReader thisReader = thisCommand.ExecuteReader();
while (thisReader.Read())
{
Console.WriteLine("\t{0}\t{1}",
thisReader["customerid"], thisReader["companyname"]);
}
thisReader.Close();
thisConnection.Close();
Console.ReadLine(); }
}
}
以上代码老是连不上SQL 2005 ,提示问题在thisConnection.Open();原因是在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错)
但是我已经开通了 SQL 2005的 TCP/IP 和 named pipes(b)服务
解决方案 »
- 急!!!请帮忙看看在定时器上40毫秒插入ORACLE数据库数据的方法是否正确
- 弱弱的问一句:条码扫描枪可以扫描计算机屏幕上的条码吗?
- 请教个c#利用webbrowser控件做的winform程序问题
- Windows API?如何让状态栏上运行的程序,激活显示在当前界面上?
- 求一简单算法
- 求图片放大缩小但图片保持清晰的方法!!
- VS 2005中,怎样实现在关闭一个窗体时,也同时关闭另一个窗体...
- 怎样设置语言识别的精度(已安装sdk5.1与language pack,我想知道的只是那一句改语言的代码)
- 请大家看看,实在看不下去了
- ADO向数据库中添加记录
- Excel的高级操作,求
- 不知道这算不算VS2005 BUG
"server=.;uid=sa;pwd=***;database=Northwind"
考虑下数据库是否是自己创建的还是从别处拷贝过来的。
如果是自己创建的,将连接字符串中的Server改为:@"Server=.\sqlexpress;...试试;
如果是从别处拷贝的,有可能存在版本及权限问题,涉及到转换
如果是本地连接,建议用混合的。sp_password null,'newpassword','sa'。
SQL 外围应用配置器关闭后再 启动即可
正确的应该是@"Server=(local);Integrated Security = True;" +