c# - 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)
);
解决方案
推荐阅读
- vba - Excel VBA:拆分字符串
- java - 云未安装插件Properties Editor for Eclipse(Kepler)
- java - 如何为 spring 5 websockets 添加安全性
- ios - 使用 uitextfield 停止计时器(swift4)
- javascript - 如何使用 Angular 5 依次运行 2 个 for 循环
- redis - 官方 redis-pubsub 源码
- c# - C# BinaryWriter 内存泄漏
- algorithm - 具有大量集合的优化算法(以功能方式)
- python - 获取每行的第一个非空值
- sql - Oracle SQL:提取两个字符之间的文本