简体   繁体   中英

This database cannot be imported when using |DataDirectory|

I try to add my value in local database (created by Add -> Service-based database).

My code:

//string connectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\VS_MyProjects\DBAppliaction\DBAppliaction\Database1.mdf;Integrated Security=True";
            string connectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;";

            string query = "INSERT INTO [dbo].[Table] (Test) " +
                   "VALUES (@Test) ";


            // create connection and command
            using (SqlConnection cn = new SqlConnection(connectionString))
            using (SqlCommand cmd = new SqlCommand(query, cn))
            {
                // define parameters and their values
                cmd.Parameters.Add("@Test", SqlDbType.NChar, 10).Value = "test";

                // open connection, execute INSERT, close connection
                cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();
            }

When I use full path to my db it's all okay. In "Show Data Table" appear my added value. But when I use in path |DataDirectory| and press button "Update" in "Show Data Table" for verify added my value or no I have seen the error: "This database cannot be imported. It is either an unsupported SQL Server verison or an unsopported database compatibility". Why?

In your connection string the Initial Catalog key/value pair is missing. This means that when the database file MDF is attached by LocalDB its full path name is used and in subsequent runs of your program the attach fails. Instead if you provide an Initial Catalog value the subsequent runs simply use that catalog

So change your connection string to

string connectionString = @"Data Source=(LocalDB)\v11.0;
                            AttachDbFilename=|DataDirectory|\Database1.mdf;
                            Initial Catalog=LogicalNameOfYourDatabase
                            Integrated Security=True;";

Of course the first problem (data apparently not saved) is caused by the behavior of DataDirectory substitution string coupled with the way in which Visual Studio handles the MDF files attached to your project (as explained in another answer here )

I ran into a similar problem with VS2012 and SQL2014. Essentially, your SQL tools in VS are not current with the SQL server. The fix was to install SQL2014 management tools locally on my development PC.

Try this:

string connectionString = @"Data Source=(LocalDB)\v11.0;
                        AttachDbFilename="+System.IO.Path.GetFullPath("Database1.mdf")+";
                        Initial Catalog=LogicalNameOfYourDatabase
                        Integrated Security=True;";

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