0
當我測試我的應用程序時,它似乎工作正常。當我點擊提交按鈕時,我收到了「成功」的消息。但是,沒有數據實際上是插入,當我試圖刷新表查看我碰到下面的信息數據:Visual Studio - 此數據庫無法導入錯誤
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);
}
}
說實話,我之前對這個數據庫有麻煩(這就是爲什麼連接字符串部分是這樣)。如果我找不到這個問題的簡單解決方案,是否有一種簡單的方法可以從表中複製架構和數據並將其傳輸到實際可能正常工作的新數據庫?
非常感謝!