简体   繁体   中英

Cannot find local database on WPF project in Visual Studio

I am trying to create a WPF application for a client and a SQL server is required. I am currently using Visual Studio to develop everything, but I have never used SQL servers outside of web application before. I add a new SQL database to my WPF project by adding a "Service-based Database", and everything works fine. Until I transfer the project over to a different computer where it crashes and gives me the error "System.Data.SqlClient.SqlException: Database not found". Is there something that I'm missing about setting up a database inside my VS project? Why does it only work on my development PC and not any other that I transfer it to?

The code that it crashes on:

string cn_string = Properties.Settings.Default.connection_String;
//Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\_data\db_local.mdf;Integrated Security=True
string cmdString = "SELECT * FROM tbl_Clients";

using (SqlConnection con = new SqlConnection(cn_string))
{
   SqlCommand cmd = new SqlCommand(cmdString, con);
   SqlDataAdapter sda = new SqlDataAdapter(cmd);
   DataTable dt = new DataTable("tbl_Clients");
   sda.Fill(dt);
   MyDataGrid.ItemsSource = dt.DefaultView;
}

Sorry for my easy questions, I am currently learning programming in high-school. Thanks for all your help.

Using a service-based database requires you to set up a SQL Server service on the machine where you intend to run the WPF client application.

If you want an embedded database that you can xcopy deploy with your application, you should take a look at SQLite . It is, unlike the service-based database that you create in Visual Studio, completely self-contained and doesn't require you to install any service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM