0

I am writing a report manager program. I created a database from project -> new item -> service-based database. When I attempt to insert I am not getting an error. When I attempt to view the data in the database with Server Explorer -> Reports -> Show Table Data I'm getting an empty table. Upon refresh, I get this error

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

My code

private void btnSubmit_Click(object sender, EventArgs e)
        {
            string span = cbSpan.Text;
            string reportData = tbReportInput.Text;
            DateTime timeStamp = DateTime.Now;

            SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\App_Data\\ReportsDB.mdf;Integrated Security=True");

            if (tbReportInput.Text == string.Empty)
            {
                MessageBox.Show("Report must have text!!!!");
            }
            else
            {
                try
                {
                    string sqlQuery = "INSERT INTO dbo.Reports(TimeSpan, ReportData, TimeStamp)  " +
                                         "VALUES (@TimeSpan, @ReportData, @Date)";
                    SqlCommand cmd = new SqlCommand(sqlQuery, con);
                    cmd.Parameters.AddWithValue("@TimeSpan", span);
                    cmd.Parameters.AddWithValue("@ReportData", reportData);
                    cmd.Parameters.AddWithValue("@Date", timeStamp);
                    con.Open();
                    int i = cmd.ExecuteNonQuery();

                    if (i != 0)
                    {
                        MessageBox.Show(i + " Data Saved");
                    }
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    con.Close();
                }

            }

My connection string from app.config

<connectionStrings>
        <add name="ReportManager.Properties.Settings.ReportsDBConnectionString"
            connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\App_Data\ReportsDB.mdf;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>

enter image description here

enter image description here

Mwheeler91
  • 215
  • 2
  • 8
  • 1
    Does this answer your question? [what's the issue with AttachDbFilename](https://stackoverflow.com/questions/11178720/whats-the-issue-with-attachdbfilename) – Charlieface Mar 08 '21 at 20:17

1 Answers1

0

Try connecting to the database from sql server Managment studio, seems like a version conflict in visual studio.

  • When I open the database from Visual Studio it opens but it's named differently [265FD576395188F9671912C444896DC7_\SPRING 2021\CITC 2190 - CAPSTONE\PROJECTNEW\REPORTMANAGER\REPORTMANAGER\APP_DATA\REPORTSDB.MDF].[dbo].[Reports] – Mwheeler91 Mar 08 '21 at 21:38
  • That's because you have this in your connection string: AttachDbFilename=|DataDirectory|\App_Data\ReportsDB.mdf; so it's basically placing the file in your system's DataDirectory – dotnet report Mar 08 '21 at 22:01
  • Is that what is causing my problem? This is only my second time using a database and the first time doing it this way. I'm in uncharted territory. – Mwheeler91 Mar 08 '21 at 22:19
  • The problem is basically exactly what the error message is stating, that you are trying to open a database table that’s not compatible with the tool you are trying to open it with. You are basically trying to visually see if your query worked and added the record you inserted, there are many ways to see it. You can run a select query right after the insert in your c# code, or you can try opening the database in sql server management studio, another tool is Azure Data Studio. VS is not able to open it because of a version conflict. Hope this helps. – dotnet report Mar 09 '21 at 00:57