以下代码连接本地数据库是正常的
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace CShar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
scsb.DataSource = @"localhost";
scsb.IntegratedSecurity = true;
scsb.InitialCatalog = "XA_IVR";
SqlConnection myConnection = new SqlConnection(scsb.ConnectionString);
myConnection.Open();
if (myConnection.State == ConnectionState.Open)
{
MessageBox.Show(this,"连接成功","OK");
}
else
MessageBox.Show(this,"连接失败","Failed");
myConnection.Close();
}
}
}
如果数据库在远程比如10.2.20.20使用
scsb.DataSource = @"10.2.20.20:1433";
代替
scsb.DataSource = @"localhost";
为何连接会失败?
应该如何修改呢 谢谢
已解决 原来是域不同
10.2.20.20是远程服务器还是你本地的电脑ip地址?请先确认你自己的电脑有没有安装SQL Server,然后再确认开启了SQL Server服务,并配置SQL Server网络配置,最后再连接试试。
10.2.20.20看上去像是一个虚拟机的ip呀,是不是网络连接模式不对啊