C#数据库连接出错 未处理的SqlException
在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接)代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace deletedata
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
// string connectionString = "server = localhost; uid = sa; pwd = sa; database = data;";
SqlConnection conn = new SqlConnection("server =localhost; uid =sa; pwd =sa; database =data;"); conn.Open(); MessageBox.Show("连接成功"); conn.Close(); }
}
}求求各位了!在线等啊
在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接)代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace deletedata
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
// string connectionString = "server = localhost; uid = sa; pwd = sa; database = data;";
SqlConnection conn = new SqlConnection("server =localhost; uid =sa; pwd =sa; database =data;"); conn.Open(); MessageBox.Show("连接成功"); conn.Close(); }
}
}求求各位了!在线等啊
http://www.google.com/search?q=SQL+Server+%E4%B8%8D%E5%85%81%E8%AE%B8%E8%BF%9B%E8%A1%8C%E8%BF%9C%E7%A8%8B&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:zh-CN:official&client=firefox-a
写成这样试试:
server =.; uid =sa; pwd=; database =data
2)打开数据库企业管理器,Server:localhost 用sa能否连接数据库
3)以上两步能连接,基本上程序也是没问题
"Data Source=(local);Initial Catalog=utscadadb;Integrated Security=SSPI; User Id=sa;Password=ut2001;"
楼上已经基本都说清楚了
少了这个Persist Security Info=True
"server =localhost;Persist Security Info=True; uid =sa; pwd =sa; database =data;"
SqlConnection gy = new SqlConnection(ConStr);
gy.Open();
我用的是SQL2008 那个TOPGY是服务器名称
应该是sqlserver服务关闭着。