首页 > 解决方案 > Logical database not INSERTING

问题描述

I have made a service-based database with VS, and added the connection to my project, with VS again. I can edit the database by manually adding the data in the table editor, but when I try to do it programatically, it compiles without errors, and even inserting gives me no errors, but I do not see the data anywhere in the database.

SqlConnection dataConnection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Users.mdf;Integrated Security=True;");
dataConnection.Open();
string query = "INSERT INTO PersonalData(a,b,c,d) VALUES (@a,@b,@c,@d)";
using (SqlCommand dataCommand = new SqlCommand(query, dataConnection))
{
    dataCommand.Parameters.AddWithValue("@a", textBox1.Text);
    dataCommand.Parameters.AddWithValue("@b", textBox2.Text);
    dataCommand.Parameters.AddWithValue("@c", textBox3.Text);
    dataCommand.Parameters.AddWithValue("@d", textBox4.Text);

    dataCommand.ExecuteNonQuery();

}
dataConnection.Close();

I execute the same exact code on another database that I have in my project, which is a SQLite database, and I can insert without problems, so I'm guessing it has something to do with SQL, but I can't figure it out. How can I insert data from a form into my database?

The code for the table creation is as follows:

CREATE TABLE [dbo].[PersonalData] (
[Id]           INT           IDENTITY (1, 1) NOT NULL,
[a] NVARCHAR (50) NULL,
[b] NVARCHAR (50) NULL,
[c] NVARCHAR (50) NULL,
[d] NVARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)

);

标签: c#sqlvisual-studio

解决方案


推荐阅读