私はwinアプリケーションを持っていますが、データベースをアプリエラーに接続したいときにこのエラーが発生します:
ファイルC:\ Users \ Aren \ Desktop \ DB\REGISTRATION.mdfの自動名前付きデータベースをアタッチしようとして失敗しました。同じ名前のデータベースが存在するか、指定されたファイルを開くことができないか、UNC共有にあります。
public class DALBase
{
protected SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\\Users\\Aren\\Desktop\\DB\\REGISTRATION.mdf;integrated Security=true ; User Instance=True");
protected SqlCommand com = new SqlCommand();
protected SqlDataAdapter da = new SqlDataAdapter();
protected DataSet ds = new DataSet();
}
public DataSet GetStudent(string filter)
{
DataSet dset = new DataSet();
using (SqlCommand cmd = new SqlCommand())
{
string cmdstring = string.Format("Select * from {0}"
, Common.Data.Student.Table_Name);
if (!string.IsNullOrEmpty(filter)) cmdstring += " where " + filter;
cmd.CommandText = cmdstring;
cmd.Connection = this.con;
cmd.Connection.Open();
cmd.CommandText = cmdstring;
cmd.Connection = this.con;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dset, Common.Data.Student.Table_Name);
return dset;
}
}
注:また、ソリューションに3つのプロジェクトがあり、DALBASEメソッドとGETSTUDENTメソッドが1つのプロジェクトにあり、他のプロジェクトからそれらを呼び出してデータを取得しています。