Home > other >  SQL service based data base stopped working
SQL service based data base stopped working

Time:06-17

I was doing this project in C# and made a simple service based database and it was working fine until i changed smthing it crashed and i accidentally restarted the system so it saved my changes and now it wont save information whenever i change smthing. It had more problems until i installed the update for visual studio 2019. As soon as i change smthing and restart the program all the info is back to whatever it was before i changed it.

This is for example an update i have

 string sql = @"UPDATE [Table] 
   SET score1 = @score , problem1 = @problem1 , problem2 = @problem2 , problem3 = @problem3 
 WHERE Id = @id";
            using (var con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\main_database.mdf;Integrated Security=True"))
            {
                con.Open();
                using (var cmd = new SqlCommand(sql, con))
                {
                    cmd.Parameters.AddWithValue("@Id", id);
                    cmd.Parameters.AddWithValue("@score", score);
                    cmd.Parameters.AddWithValue("@problem1", problem1);
                    cmd.Parameters.AddWithValue("@problem2", problem2);
                    cmd.Parameters.AddWithValue("@problem3", problem3);
                    cmd.ExecuteNonQuery();
                }
            }

Any info would be greatly appreciated.

CodePudding user response:

if your project is asp.net, I simply reproduce your update database operation and succeed.

Updated UI page:

enter image description here

Code logic to update database:

    protected void Button2_Click(object sender, EventArgs e)
    {
        // get student data
        Student student = new Student();
        student.ID = TextBox1.Text.Trim();
        student.Name = TextBox2.Text.Trim();
        student.Age = Convert.ToInt32(DropDownList1.SelectedValue.Trim());
        student.Department = DropDownList2.SelectedValue.Trim();
        student.ClassName = DropDownList3.SelectedValue.Trim();

      
        string sql = @"UPDATE students SET Name = @name , Age = @age , Department = @Department , className = @className WHERE ID = @id";
        using (var con = new SqlConnection(@"Data Source=localhost;Initial Catalog=student;Integrated Security=SSPI;"))
        {
            con.Open();
            using (var cmd = new SqlCommand(sql, con))
            {
                cmd.Parameters.AddWithValue("@name", student.Name);
                cmd.Parameters.AddWithValue("@age", student.Age);
                cmd.Parameters.AddWithValue("@Department", student.Department);
                cmd.Parameters.AddWithValue("@className", student.ClassName);
                cmd.Parameters.AddWithValue("@id", student.ID);
                var f = cmd.ExecuteNonQuery();
                if (f != 0)
                {
                    Response.Write("<script>alert('Modified successfully!')</script>");
                }
            }
        }

    }

Click the "Edit student information" button:

enter image description here

enter image description here

If there is no problem with your connection string, the database will work fine. Through my simple test, the code to update the database is no problem. Hope that helps you.

CodePudding user response:

Thanks for all the answers i ended up creating a new database on microsoft sql express and it works now i guess visual studio created databases are just buggy since noone explained any reason why mine would stop working specifically. Anyway, does this page have any use for anyone or should i delete this?

  • Related