using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;namespace ADO.NET
{
class Program
{
static void Main(string[] args)
{
SqlConnection con = null;
SqlCommand cmd = null;
SqlDataReader Reader = null;
try
{
string strcon = "data Source=.;Initial Catalog=AdventureWorks;Integrated Security=true";
con = new SqlConnection(strcon);
cmd = con.CreateCommand();
cmd.CommandText = "select AddressLine1 from Person.Address";
con.Open();
Reader = cmd.ExecuteReader();
while (Reader.Read())
{
Console.WriteLine(Reader[0]);
}
}
catch (Exception ep)
{
Console.WriteLine(ep.Message);
}
finally
{
Reader.Close();
cmd.Clone();
con.Close();
} }
}
}为什么显示未找到或无法访问服务器。请验证实例名称是否正确并且SQLSever已配置为允许远程连接
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;namespace ADO.NET
{
class Program
{
static void Main(string[] args)
{
SqlConnection con = null;
SqlCommand cmd = null;
SqlDataReader Reader = null;
try
{
string strcon = "data Source=.;Initial Catalog=AdventureWorks;Integrated Security=true";
con = new SqlConnection(strcon);
cmd = con.CreateCommand();
cmd.CommandText = "select AddressLine1 from Person.Address";
con.Open();
Reader = cmd.ExecuteReader();
while (Reader.Read())
{
Console.WriteLine(Reader[0]);
}
}
catch (Exception ep)
{
Console.WriteLine(ep.Message);
}
finally
{
Reader.Close();
cmd.Clone();
con.Close();
} }
}
}为什么显示未找到或无法访问服务器。请验证实例名称是否正确并且SQLSever已配置为允许远程连接
data Source=.;
//改为
Data Source=localhost;
//或改为你的服务器名称或ip地址试下
con = new SqlConnection(strcon);