求助C#连接SQL SERVER 2008
各位高手前辈,新年好写贴求助一事关于visual c#2008 链接sqlserver2008 一事求助各位。我练习写程序的时候,尝试连接sql,一切都好,就是运行的时候发现无法链接数据库,报错为:在与sql建立连接时出现与网络相关或特定于实例的错误,无法找到或访问服务器,请验证实例名称是否正确且sql server已经配置为允许远程链接。而我用的SQL server 2008已经开启了远程登录。不知道为什么会这样,特请教各位,我的联系代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace Consolesql
{
class Program
{
static void Main(string[] args)
{
string connstring = @"server=.\CHINA-4D777C59C;integrated security=true;database=northwind";
//Queryable 1
string sql1 = @"select * from customers";
//query 2
string sql2=@"select * from products where unitprice<10";
string sql=sql1+sql2;
SqlConnection conn = new SqlConnection(connstring);
try {
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sql, conn);
DataSet ds=new DataSet();
da.Fill (ds,"customers");
DataTableCollection dtc=ds.Tables;
Console.WriteLine("results from customers table:");
Console.WriteLine ("companyname".PadRight(2)+"contacname".PadLeft(23)+"\n");
string fl="country='Germany'";
string srt="companyname asc";
foreach (DataRow row in dtc["coustomers"].Select(fl, srt))
{
Console.WriteLine("{0}\t{1}", row["companyname"].ToString().PadRight(25),
row["contactname"]);
}
Console.WriteLine("\n---------------------------------");
Console.WriteLine("results from products table:");
Console.WriteLine("productname".PadRight(20) + "unitprice".PadLeft(21) + "\n");
foreach (DataRow row in dtc[1].Rows)
{
Console.WriteLine("{0}\t{1}", row["productname"].ToString().PadRight(25), row["unitprice"]);
}
}
catch(Exception e)
{
Console.WriteLine("error:"+e);
}
finally
{
conn.Close();
}
}
}
}
--------------------编程问答-------------------- 检查sql server 2008是否开启了tcp/ip协议,还有你的客户端sql client是否开启tcp/ip协议, --------------------编程问答-------------------- 在程序中连接之前,可以先在vs中用数据库工具先连接看看,数据库工具可以帮你构造连接字符串, --------------------编程问答-------------------- http://download.csdn.net/detail/xianfajushi/4028787 --------------------编程问答-------------------- 检查sql server 2008是否开启了tcp/ip协议,还有你的客户端sql client是否开启tcp/ip协议 --------------------编程问答-------------------- 连接方法有很多种,根据你的配置选择
连接方法
http://connectionstrings.com/sql-server-2008 --------------------编程问答-------------------- string connstring = @"server=.\CHINA-4D777C59C;integrated security=true;database=northwind";
uid?psw? --------------------编程问答-------------------- 谢谢各位给出解决建议,问题还有一个,我是在服务器这边做联系,为什么还要远程登录呢? --------------------编程问答--------------------
UID --------------------编程问答-------------------- 机器上的TCP/IP协议都开启了,但是不知道怎么开启sql server 2008的协议
--------------------编程问答-------------------- 运行sql server的配置管理器,里面可以启动和禁用协议,
--------------------编程问答-------------------- 选择数据库,右键-》属性,找到安全性选项,服务器身份验证为:Sql Server和windows身份认证模式,
找到连接选项:勾选允许远程连接到此服务器。试试。
补充:.NET技术 , C#