Reputation: 11
using (Connection = new SqlConnection(connectionstring))
{
SqlCommand voegtoe = new SqlCommand("INSERT INTO Speler(Naam, Rugnummer, Leeftijd) VALUES(@Naam, @Rugnummer, @Leeftijd)");
voegtoe.CommandType = CommandType.Text;
voegtoe.Connection = Connection;
voegtoe.Parameters.AddWithValue("@Naam", textBox1.Text);
voegtoe.Parameters.AddWithValue("@Rugnummer", textBox2.Text);
voegtoe.Parameters.AddWithValue("@Leeftijd", textBox3.Text);
Connection.Open();
voegtoe.ExecuteNonQuery();
}
If I open my Database there's nothing added to it. I think it should add the text what the user puts in the textboxes.
The connection string is:
Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Speler.mdf;Integrated Security=True
Upvotes: 1
Views: 128
Reputation: 131180
There's nothing wrong with the code. I suspect that your connection string uses a User instance database, ie it has the AttachDBFilename
and User Instance=true
keywords:
Data Source=.\SQLEXPRESS; AttachDBFilename=|DataDirectory|\db.mdf; User Instance=true;...
That means that each user that tries to attach to the database file gets his own copy. When you try to check the data with SSMS you see a different copy of the database.
Apart from causing confusion, this feature is deprecated and will be removed in the future.
Just create a proper database in your database server and connect to it by specifying its name:
Data Source=.\SQLEXPRESS; Initial Catalog=MyDB;...
You can create the database either from the New Database
menu in SSMS, or with a CREATE DATABASE MyDB
command
You can find more information in Bad habits : Using AttachDBFileName by Aaron Bertrand
Upvotes: 2