连接SQL代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;namespace DateReading
{
class Program
{
static void Main(string[] args)
{
SqlConnection thisConnection = new SqlConnection(@"Server=(local)\administrator;Integrated Security=True;Database=northwind");
thisConnection.Open();
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "Select CustomerID,CompanyName from Customes";
SqlDataReader thisReader = thisCommand.ExecuteReader();
while (thisReader.Read())
{
Console.WriteLine("\t{0}\t{1}",thisReader["CustomerID"],thisReader["CompanyName"]);
}
thisReader.Close();
thisConnection.Close();
Console.ReadKey();
}
}
}这样连接会提示
在建立与服务器的连接时出错。连接到SQL Server 2005时,在默认的设置下Sql server不允许进行远程连接可能会导致此失败。
哪里有问题?
new SqlConnection(@"Server=(local)\administrator;Integrated Security=True;Database=northwind");这里的Server=???,是不是计算机名/实例名,那么我怎么知道这个实例名呢?
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;namespace DateReading
{
class Program
{
static void Main(string[] args)
{
SqlConnection thisConnection = new SqlConnection(@"Server=(local)\administrator;Integrated Security=True;Database=northwind");
thisConnection.Open();
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "Select CustomerID,CompanyName from Customes";
SqlDataReader thisReader = thisCommand.ExecuteReader();
while (thisReader.Read())
{
Console.WriteLine("\t{0}\t{1}",thisReader["CustomerID"],thisReader["CompanyName"]);
}
thisReader.Close();
thisConnection.Close();
Console.ReadKey();
}
}
}这样连接会提示
在建立与服务器的连接时出错。连接到SQL Server 2005时,在默认的设置下Sql server不允许进行远程连接可能会导致此失败。
哪里有问题?
new SqlConnection(@"Server=(local)\administrator;Integrated Security=True;Database=northwind");这里的Server=???,是不是计算机名/实例名,那么我怎么知道这个实例名呢?
第一次弄SQL,不好意思@——!
SqlConnection thisConnection = new SqlConnection("Server=127.0.0.1;Database=northwind;Uid=sa;Pwd=sa");Pwd=sa sa改为你sql登陆密码
这个在 .net2003里没问题,在 2005好象就不能这样写了
是你数据库服务器名,一般是你计算机名
也可以 用 127.0.0.1来代替
一般都是这样写的
Integrated Security=SSPI
你的怎么是true?
我没见过,汗自己一下~~~
一般都是这样写的
Integrated Security=SSPI
你的怎么是true?
我没见过,汗自己一下~~~我是按书上这么写的。
SA帐号的密码为空时,黑客可以利用这个帐号得到系统的管理员吗?
你去掉Integrated Security=True 加Uid=sa;Pwd=sa" 这个就行了不然改sql的登陆方式也行搂主好快啊 一下都学到C#入门经典24章了