I have already checked other topics regarding the matter and tried some of the standard solutions but nothing seems to work. My DB is local and I'm trying to connect, it is already created and saved in the folder Helpers with the name of localDB.mdf My code is:
public static List<ChatMessages> GetFromDatabase(string query)
{
List<ChatMessages> ImportedFiles = new List<ChatMessages>();
string sql = @"Data Source=|DataDirectory|\Helpers\localDB;AttachDbFilename=\Helpers\localDB.mdf;Integrated Security=True";
SqlConnection connection = new SqlConnection(sql);
try
{
connection.Open();
var check = connection.State;
SqlCommand cmd = new SqlCommand(query, connection);
var rdr = cmd.ExecuteReader();
while (rdr.Read())
{
//stuff
}
connection.Close();
}
catch (Exception e) { MessageBox.Show("error: " + e); }
var x = ImportedFiles;
return ImportedFiles;
}
Thanks for your time, Best regards