首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

VS2005 C#连接一个SQL SERVER数据库,程序运行报错 无法建立数据连接:实例失败。这是小弟我照书编写的第一个程序,卡壳4天了,求各位帮帮小弟我,不

2012-03-07 
VS2005 C#连接一个SQL SERVER数据库,程序运行报错 无法建立数据连接:实例失败。这是我照书编写的第一个程序

VS2005 C#连接一个SQL SERVER数据库,程序运行报错 无法建立数据连接:实例失败。这是我照书编写的第一个程序,卡壳4天了,求各位帮帮我,不胜感激!
奇怪的是我的程序在别人的电脑里运行没问题,可到我这就报错,点击登陆界面的前三个按钮想跳到其他三个界面,都报这样的错误。第四个按钮能跳转到另一个界面,我的第四个界面是用报表实现的,我的app.configure
<connectionStrings>
  <add name="Explore700Store.Properties.Settings.Explore700StoreConnectionString"  
  connectionString="Data Source=.\\SQLEXPRESS;Initial Catalog=Explore700Store;Integrated Security=True;
  AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA;" providerName="System.Data.SqlClient" />
  </connectionStrings>
第一个界面(登陆界面)的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace WindowsApplication1
{
  public partial class Form1 : Form
  {
  protected SqlConnection conn;
  protected SqlCommand cmd;
  protected Explore700StoreDataSetTableAdapters.Explore700TableAdapter adapter;
  public Form1()
  {
  InitializeComponent();
  }
   
  private void button1_Click(object sender, EventArgs e)
  {
  Explore700Form frm1=new Explore700Form();
  frm1.Show();
  this.Visible = false;
  }

  private void button2_Click(object sender, EventArgs e)
  {
  CustomerForm frm1 = new CustomerForm();
  frm1.Show();
  this.Visible = false;
  }

  private void Form1_Load(object sender, EventArgs e)
  {

  }

  private void button3_Click(object sender, EventArgs e)
  {
  SellForm frm1 = new SellForm();
  frm1.Show();
  this.Visible = false;
  }

  private void button4_Click(object sender, EventArgs e)
  {
  ReportForm frm1 = new ReportForm();
  frm1.Show();
  this.Visible = false;
  }
  }
}
第二个界面的部分代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;


namespace WindowsApplication1
{
  public partial class CustomerForm : Form
  {
  protected SqlConnection conn;
  protected SqlCommand cmd;
  public CustomerForm()
  {
  InitializeComponent();
  }

  private void CustomerForm_Load(object sender, EventArgs e)
  {
  try
  {
  SqlConnectionStringBuilder sb1=new SqlConnectionStringBuilder ();
  sb1 .DataSource ="(Local)";
  sb1 .InitialCatalog ="Explore700Store";
  sb1 .IntegratedSecurity =true ;
  conn = new SqlConnection(sb1 .ConnectionString);
  cmd = new SqlCommand();
  cmd.Connection = conn;
  conn.Open();
  this.InitData();
  }
  catch (Exception exp)
  {
  MessageBox.Show("无法建立数据连接:" + exp.Message);


  this.Close();
  }
   
  }

[解决办法]
应该还是你的数据库连接字符串有点问题,连接connectionString="Data Source=.\\SQLEXPRESS;Initial Catalog=Explore700Store;Integrated Security=True;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA;"

可能一:你的Data Source名字有问题,因为VS2005自带一个SQL Server2005 Express版,它的数据源名字一般是.\\SQLEXPRESS这样的,而你如果自己安装了SQL Server2005的话,数据源一般是DataSource=.就行了。至于你说的那个企业管理器,其实就是数据库的可视化管理工具,可以有一个登陆框登录到数据库,你观察一下你的登陆框里面数据源的名称,用.\\SQLEXPRESS作为服务器名,看通过Windows方式登录,能否登陆进去。如果行的话,数据源设定是没有问题的。

可能二:你的数据库存不存在,具体说就是你建立的那个库:Explore700Store,在不在SQL Server2005 Express里,如果没有,你就得把那个数据附加上(你照着书敲得,应该有数据文件)。

可能三:就是数据库的存放路径问题,就是我第三个地方红色地方标记的,看看你的路径是否一致。

另外:Sql Server Express版的一般是为了方便用户学习测试,甚至前期的开发的方便,绑定在VS2005里,它能拥有SQL Server2005其他版(如个人版,企业版)的大部分功能,能应付一般的开发。企业版的话,就是用于实际的项目中,性能上好一些,功能更强大一些,一般个人学习的话,电脑配置一般的话,用Express版的就可以了。

呵呵,我的一点愚见,祝顺利!

热点排行