想問用SQL語法存取資料表發生這樣的意外原因

完整程式碼



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace sql客戶端
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{
using (SqlConnection cn= new SqlConnection())
{
cn.ConnectionString = "Data Source=.\\SQLExpress;" + "AttachDbfilename=|DataDirectory|Database2.mdf;" + "Integrated Security=True;user Instance=True;";
SqlDataAdapter daEmployee = new SqlDataAdapter("SELECT*FROM 姓名 ORDER BY 編號 DESC", cn);
DataSet ds = new DataSet();
daEmployee.Fill(ds, "姓名");
cboId.DataSource = ds;
cboId.DisplayMember = "員工.編號";
txtName.DataBindings.Add("Text", ds, "員工.姓名");
txtTel.DataBindings.Add("Text", ds, "員工.電話");
txtPosition.DataBindings.Add("Text", ds, "員工.職稱");
txtSalary.DataBindings.Add("Text", ds, "員工薪資");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "員工";
}

}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

private void txtSalary_TextChanged(object sender, EventArgs e)
{

}

private void cboId_SelectedIndexChanged(object sender, EventArgs e)
{

}
}
}


程式的功能是查詢員工的基本資料,
其中是這行有問題 daEmployee.Fill(ds, "姓名");
編譯器寫說
建立連接至 SQL Server 時,發生網路相關或執行個體特定的錯誤。找不到或無法存取伺服器。確認執行個名稱是否正確,以及 SQL Server 是否設定為允許遠端連線。 (provider: SQL Network Interfaces, error: 26 -
我有把資料表複製到這尃案的Debug資料夾下,我是一個初學者,請問是什麼原因造成的,謝謝!
2017-10-30 18:44 發佈
AttachDbfilename=|DataDirectory|Database2.mdf; ==> 這段有問題

你改成絕對路徑就可以了 EX:AttachDbFilename=C:\\MyDB\\Database.mdf


自己電腦有沒裝SQL Express都不知道吧?


內文搜尋
X
評分
評分
複製連結
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?