VS2005 C#连接一个SQL SERVER数据库,程序运行报错 无法建立数据连接:实例失败。这是小弟我照书编写的第一个程序,卡壳4天了,求各位帮帮小弟我,不胜感激
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);
奇怪的是我的程序在别人的电脑里运行没问题,可到我这就报错,点击登陆界面的前三个按钮想跳到其他三个界面,都报这样的错误。第四个按钮能跳转到另一个界面,我的第四个界面是用报表实现的,我的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);