Visual Studio - 此数据库无法导入错误

时间:2016-05-13 14:23:34

标签: c# sql sql-server

当我测试我的应用时,它似乎工作正常。当我点击提交按钮时,我收到“成功”消息。但是,实际上没有插入数据,当我尝试刷新表以查看数据时,我得到以下消息:

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>

还有一些代码片段:

     // 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);
                }
            }

老实说,我之前遇到过这个数据库的麻烦(这就是连接字符串部分就是这样的原因)。如果我找不到这个问题的简单解决方案,是否有一种简单的方法可以从表中复制模式和数据并将其传输到可能实际正常工作的新数据库?

非常感谢!

1 个答案:

答案 0 :(得分:0)

是的,您需要更改连接字符串。尝试这样的事情:

    <add name="LocalSqlServer" connectionString="DataBaseServerName;database=NameOfDB;Integrated Security=True;" />