Reputation: 147
I have created a very simple WPF application which simply reads and displays the contents of a local MDF database. It runs fine on my computer but fails when I publish and install it on another.
I have added a Try/Catch statement around the cm.Open() statement and discovered that it is unable to connect to the database.
I'm using Visual Studio 2022. The application is in C#. The local database is an MDF file.
This is my code...
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//cn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True");
cn = new SqlConnection(@"Data Source=server;Initial Catalog=Database1.mdf;Integrated Security=True");
try
{
cn.Open();
GetAllEmployeeRecocrd();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void GetAllEmployeeRecocrd()
{
cmd = new SqlCommand("SELECT * FROM tblEmployee", cn);
da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGrid1.ItemsSource = dt.DefaultView;
}
The only prerequisite which is automatically included is the .Net Framework 4.7.2.
Can anyone offer any ideas please?
Upvotes: 0
Views: 60