在连接到SQL Server2005时,在默认的设置下SQL Server不允许进行远程连接可能会导致此失败(provider:命名管道提供程序,error:40-无法打开到sqlserver的连接)提示的错误就是这个我用的C#,连接语句是
SqlConnection con = new SqlConnection("server = loaclhost;DataBase=DB_Bus;user Id=sa;password=sa");提示在58行出错:
try
{
58: con.Open();
SqlCommand cmd = new SqlCommand("select Id from Line", con);
SqlDataReader dr = cmd.ExecuteReader();
this.comboBox1.Items.Clear();
this.comboBox2.Items.Clear();
this.comboBox3.Items.Clear();
this.comboBox4.Items.Clear();
while (dr.Read())
{
this.comboBox1.Items.Add(dr[0].ToString());
this.comboBox2.Items.Add(dr[0].ToString());
this.comboBox3.Items.Add(dr[0].ToString());
this.comboBox4.Items.Add(dr[0].ToString());
}
dr.Close();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}很奇怪,我的另外一个程序就能和数据库连接上,用的连接语句也是一样,就没有问题另外一个程序:
try
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT id,linename FROM line", con);
SqlDataReader sdr = cmd.ExecuteReader();
this.cbb_line.Items.Clear();
//i是作为string[]的序号
while (sdr.Read())
{
this.cbb_line.Items.Add(sdr["id"].ToString());
linename[int.Parse(sdr["id"].ToString())] = sdr["linename"].ToString().Trim();
}
sdr.Close();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
SqlConnection con = new SqlConnection("server = loaclhost;DataBase=DB_Bus;user Id=sa;password=sa");提示在58行出错:
try
{
58: con.Open();
SqlCommand cmd = new SqlCommand("select Id from Line", con);
SqlDataReader dr = cmd.ExecuteReader();
this.comboBox1.Items.Clear();
this.comboBox2.Items.Clear();
this.comboBox3.Items.Clear();
this.comboBox4.Items.Clear();
while (dr.Read())
{
this.comboBox1.Items.Add(dr[0].ToString());
this.comboBox2.Items.Add(dr[0].ToString());
this.comboBox3.Items.Add(dr[0].ToString());
this.comboBox4.Items.Add(dr[0].ToString());
}
dr.Close();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}很奇怪,我的另外一个程序就能和数据库连接上,用的连接语句也是一样,就没有问题另外一个程序:
try
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT id,linename FROM line", con);
SqlDataReader sdr = cmd.ExecuteReader();
this.cbb_line.Items.Clear();
//i是作为string[]的序号
while (sdr.Read())
{
this.cbb_line.Items.Add(sdr["id"].ToString());
linename[int.Parse(sdr["id"].ToString())] = sdr["linename"].ToString().Trim();
}
sdr.Close();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
如何打开sql server 2005 的1433端口配置工具->Sql Server Configuration Manager->MSSQLSERVER的协议看看TCP/IP协议是否启动,如果启动,右键菜单点"属性" ,在分页菜单中选"IP地址",把"IP1"和"IP2"中"TCP端口"为1433,"已启用"改为"是"配置工具->Sql Server Configuration Manager->SQL Native Client 配置->客户端协议->TCP/IP选择TCP/IP右键菜单中"属性",确认"默认端口"是1433,"已启用"为"是"
SQL Server 2005 远程连接配置TCP/IP属性.
Surface Area Configuration --> Database Engine --> Remote Connections --->Using TCP/IT SQL Server 外围应用配置器—服务和连接外围配置—database englie—远程连接—启用(远程连接的TCP/IP和named pipes)
SQL Server Configuration Manager—SQL2005网络配置—启用TCP/IP和named pipes
-----------------------------------------------------------------------------------------
一般按如上即可. 其他说明见下:
sqlserver2005(Express版),为了便于管理,你还需要去下一个manage管理器,微软的官方网站有
下,安装好manage管理器后,在程序中连接sqlserver2005,下面几点是要注意的。
1. 开启sql2005远程连接功能,开启办法如下,
配置工具->sql
server外围应用配置器->服务和连接的外围应用配置器->打开MSSQLSERVER节点下的Database Engine
节点,先择"远程连接",接下建议选择"同时使用TCP/IP和named pipes",确定后,重启数据库服务就可以了.
2.登陆设置改为,Sql server and windows Authentication方式同时选中,具体设置如下:
manage管理器->windows
Authentication(第一次用windows方式进去),->对象资源管理器中选择你的数据服务器--右键>属
性>security>Sql server and windows Authentication方式同时选中.
3:设置一个Sql server方式的用户名和密码,具体设置如下:
manage管理器->windows Authentication>new query>sp_password null,'sa123456','sa'
这样就设置了一个用户名为sa ,密码为:sa123456的用户,下次在登陆时,可以用Sql server方式,
用户名为sa ,密码为:sa123456的用户进数据库了.
4: 做完上面三步后,这样写连接字符串就可以顺利进入数据库了,
(server=.\\sqlexpress;uid=sa;pwd=sa123456;database=master";
其它版本,做如下設置 程序—Microsoft SQL Server 2005—配置工具—SQL Server 外围应用配置器—服务和连接的外围应用配置器—
1、MSSQLserver(实例名)—DataBase Engine—远程连接—(设置选项)
在下方选项
2、SQL Server Browser—服务(停止时只本机访问)
3、程序—Microsoft SQL Server 2005—配置工具—SQL Server Configuration Manager—网络连接协议-选择实例对应协议—启用(shard memory、Named Pipes、TCP\IP)
注: SQL Server 浏览器程序以服务的形式在服务器上运行。SQL Server 浏览器侦听对 Microsoft SQL Server 资源的传入请求,
并提供计算机上安装的 SQL Server 实例的相关信息。
SQL Server 浏览器可用于执行下列三种操作:
浏览可用服务器
连接到正确的服务器实例
连接到专用管理员连接 (DAC) 端点
SQL Server Browser 服务 (sqlbrowser) 为数据库引擎 和 SSAS 的每个实例提供实例名称和版本号。
SQL Server 浏览器随 Microsoft SQL Server 2005 一起安装,
为该计算机上运行的早期版本的 SQL Server(从 Microsoft SQL Server 7.0 开始)提供此服务。
先不要用程序
你打开VS2005或者其他版本的
点击视图-->服务器资源管理器-->添加数据库连接
用你的用户名称密码登陆
看下是否能够登陆在说
sql连服务都不能启动
VS又怎么能和它通信了你先把sql弄得能够正常访问
然后使用我的3楼的方法
改成
SqlConnection con = new SqlConnection("server = (loaclhost);DataBase=DB_Bus;user Id=sa;password=sa");