简体   繁体   中英

Visual Studio - This Database Cannot Be Imported Error

When I test my app, it appears to work fine. I get the "success" message when I hit the submit button. However, no data is actually inserted, and when I attempt to refresh the table to view the data I get the following message:

This database cannot be imported. It is either an unsupported SQL Server version or an unsupported database compatibility.

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />

    </startup>
  <connectionStrings>
    <remove name="LocalSqlServer" />
    <add name="LocalSqlServer" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|database.mdf;Integrated Security=True;Connect Timeout=30;Initial Catalog=database.mdf"
      providerName="System.Data.SqlClient" />
    <add name="hotelApp.Properties.Settings.databaseConnectionString"
      connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|database.mdf;Integrated Security=True;Connect Timeout=30;Initial Catalog=database.mdf"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

And some code snippets:

     // called when my main window loads
private void initializeData()
            {




                try { con.Open(); }
                catch (SqlException er) { Console.Write(er); }

                String query = "SELECT * from dbo.locations";
               locAdapter = new SqlDataAdapter(query, con);
                locAdapter.Fill(ds, "Locations");



                query = "SELECT * from dbo.report";
                reportAdapter = new SqlDataAdapter(query, con);
                reportAdapter.Fill(ds, "Reports");
                SqlCommand insert = new SqlCommand("INSERT into dbo.report (report_id, inspector, employee, room, date, score) " + " VALUES (@report_id, @inspector, @employee, @room, @date, @score)", con);
                insert.Parameters.Add("@report_id", SqlDbType.Int, 5, "report_id");
                insert.Parameters.Add("@room", SqlDbType.Int, 4, "room");
                insert.Parameters.Add("@inspector", SqlDbType.Int, 5, "inspector");
                insert.Parameters.Add("@employee", SqlDbType.Int, 4, "employee");
                insert.Parameters.Add("@date", SqlDbType.Date, 50, "date");
                insert.Parameters.Add("@score", SqlDbType.Int, 4, "score");

                reportAdapter.InsertCommand = insert;


                query = "SELECT * from dbo.report_details";
               detailsAdapter = new SqlDataAdapter(query, con);
                detailsAdapter.Fill(ds, "Details");

             insert = new SqlCommand("INSERT into dbo.report_details (reportID, itemID, points, comments) " + " VALUES (@reportID, @itemID, @points, @comments)", con);
                insert.Parameters.Add("@reportID", SqlDbType.Int, 5, "reportID");
                insert.Parameters.Add("@itemID", SqlDbType.Int, 5, "itemID");
                insert.Parameters.Add("@points", SqlDbType.Int, 4, "points");
                insert.Parameters.Add("@comments", SqlDbType.Text, 150, "comments");

                detailsAdapter.InsertCommand = insert;


                locationComboBox.DataContext = ds.Tables["Locations"];
                locationComboBox.DisplayMemberPath = "locName";



                DataTable grid = new DataTable("Grid");
                grid.Columns.Add("ID", typeof(int));
                grid.Columns.Add("Name", typeof(String));
                grid.Columns.Add("Description", typeof(String));
                grid.Columns.Add("Points", typeof(int));
                grid.Columns.Add("Score", typeof(List<int>));
                grid.Columns.Add("Current", typeof(int));
                grid.Columns.Add("Comments", typeof(String));     



                query = "SELECT itemID, name, description, points, category FROM dbo.items";

                SqlDataReader reader = new SqlCommand(query, con).ExecuteReader();

                while (reader.Read())
                {
                    DataRow row = grid.NewRow();

                    row["ID"] = reader["itemID"];
                    row["Name"] = reader["name"];
                    row["Description"] = reader["description"];
                    row["Points"] = reader["points"];
                    totalPoints += (int)reader["points"];
                    row["Current"] = reader["points"];

                    int pointsPossible = (int)reader["points"];
                    List<int> rowList = new List<int>();
                    for (int i = pointsPossible; i >= 0; i--)
                    {
                        rowList.Add(i);
                    }
                    rowList.Sort();
                    row["Score"] = rowList;


                    grid.Rows.Add(row);



                }
                ds.Tables.Add(grid);

                itemGrid.ItemsSource = ds.Tables["Grid"].DefaultView;

            }


  // called when the submit button is hit, the message in the try block displays successfully

    private void submitData(object sender, RoutedEventArgs e)
            {
                SqlTransaction tran = con.BeginTransaction();

                reportAdapter.InsertCommand.Transaction = tran;
                detailsAdapter.InsertCommand.Transaction = tran;

                DataRow reportRow = ds.Tables["Reports"].NewRow();

                reportRow["report_id"] = reportID;

                DataRowView inspectorSelection = (DataRowView)inspectorBox.SelectedItem;
                reportRow["inspector"] =       Int16.Parse(inspectorSelection["empID"].ToString());

                DataRowView empSelection = (DataRowView)employeeBox.SelectedItem;
                reportRow["employee"] = Int16.Parse(inspectorSelection["empID"].ToString());

                reportRow["room"] = Int16.Parse(roomTextBox.Text);

                reportRow["date"] = DateTime.Now.ToString("yyy-MM-dd");



                reportRow["score"] = currentPoints;

                ds.Tables["Reports"].Rows.Add(reportRow);

                // update report_details dataset

                foreach (DataRow row in ds.Tables["Grid"].Rows)
                {

                    DataRow reportDetailsRow = ds.Tables["Details"].NewRow();

                    reportDetailsRow["reportID"] = reportID;
                    reportDetailsRow["itemID"] = row["ID"];
                    reportDetailsRow["points"] = currentPoints;
                    reportDetailsRow["comments"] = row["Comments"];

                }

                // update tables as single transaction
                try
                {

                    reportAdapter.Update(ds, "Reports");
                    detailsAdapter.Update(ds, "Details");
                    tran.Commit();
                    MessageBox.Show("Date Inserted");
                }
                catch (SqlException sqlEr)
                {
                    MessageBox.Show(sqlEr.Message);
                }
            }

To be honest, I had trouble earlier with this database (that is why the connection string section is the way it is). If I cannot find a simple solution to this problem, is there an easy way to copy the schema and data from the tables and transport it to a new database that might actually work properly?

Thanks much!

Yes, you need to change the connection string. Try something like this:

    <add name="LocalSqlServer" connectionString="DataBaseServerName;database=NameOfDB;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