在visual studio用command物件呼叫預存程序

我想說在visual studio用command物件呼叫預存程序的資料庫,可以執行,但在輸入查尋資料後
會在這行cn.Open();

顯示其他資訊: An attempt to attach an auto-named database for file D:\c#上課檔\查詢產品2\查詢產品2\遊戲王產品詳.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

請問是其它尃案用到相同路徑的關係嗎,因為 還有一個專案也用到遊戲王產品詳.mdf這檔案,請問如何修正?

程式碼
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 查詢產品2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnOk_Click(object sender, EventArgs e)
{
if (txtName.Text == "")
{
txtResult.Text = "請輸入欲查詢產品名稱";
return;
}
string Pathstr = System.Windows.Forms.Application.StartupPath.Replace(@"\bin\Debug", "");
string connstr = @"Data Source = (LocalDB)\MSSQLLocalDB;"
+ "AttachDbFilename =" + Pathstr + @"\遊戲王產品詳.mdf; Integrated Security = True";
using (SqlConnection cn = new SqlConnection())
{
cn.ConnectionString = connstr;
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandText = "GetEmployeeByName";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@EmpName", SqlDbType.NVarChar));
cmd.Parameters["@EmpName"].Value = txtName.Text;
txtResult.Text = "";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
for (int i = 0; i < dr.FieldCount; i++)
{
txtResult.Text += dr.GetName(i) + ":" + dr[i].ToString() + Environment.NewLine;
}

}
else
{
txtResult.Text = "找不到" + txtName.Text + "這個產品";
}
}
}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}
文章關鍵字
文章分享
評分
評分
複製連結

今日熱門文章 網友點擊推薦!