c# - Visual Studio 说我试图将 NULL 插入“Cars_details”表的“id”记录中
问题描述
我正在为大学课程编写程序,但我被卡住了。我有 2 个表,我想用一个文本向其中插入数据。
汽车
- id INT NOT NULL 主键,IDENTITY(1,1)
- userId INT NOT NULL FOREIGN KEY Users(id)
Car_Details
- id INT NOT NULL PRIMARY KEY, FOREIGN KEY Cars(id)
- 名称 varchar(250) NOT NULL
- body_type VARCHAR(250) 非空,
- 燃料类型 VARCHAR(250) 非空,
- motor_power VARCHAR(250) 非空,
- 制造年 INT NOT NULL
这就是说我正在尝试为Car_Details
表中的 id 记录插入 NULL 值。下面我写了我的方法,我是如何尝试的。
尝试使用事务进行此操作,但这也不起作用。
public int Insert(Record record)
{
SqlCommand command = new SqlCommand();
command.CommandType = System.Data.CommandType.Text;
//command.CommandText = @"INSERT INTO Cars(userId)
// VALUES ((SELECT id FROM Users WHERE id = Users.id));
// INSERT INTO Car_Details(id, name, body_type, fuel_type, motor_power, manufacture_year)
// VALUES ((SELECT TOP 1 id FROM Cars),@name, @body_type, @fuel_type, @motor_power, @manufacture_year);";
command.CommandText = "SET XACT_ABORT ON BEGIN TRANSACTION DECLARE @CarID int
INSERT INTO Cars(userID) VALUES((SELECT id FROM Users WHERE id = Users.id))" +
"SELECT @CarID = scope_identity()" +
"INSERT INTO Car_Details VALUES(@CarID, @name, @body_type" +
"@fuel_type, @motor_power, @manufacture_year)" +
"COMMIT";
//SqlParameter p_id = new SqlParameter();
//p_id.ParameterName = "@id";
//p_id.SqlDbType = System.Data.SqlDbType.Int;
//p_id.Direction = System.Data.ParameterDirection.Input;
//p_id.Value = (record as CarRecord).Id;
//command.Parameters.Add(p_id);
SqlParameter p_name = new SqlParameter();
p_name.ParameterName = "@name";
p_name.SqlDbType = System.Data.SqlDbType.Char;
p_name.Direction = System.Data.ParameterDirection.Input;
p_name.Value = (record as CarRecord).Name;
command.Parameters.Add(p_name);
SqlParameter p_body_type = new SqlParameter();
p_body_type.ParameterName = "@body_type";
p_body_type.SqlDbType = System.Data.SqlDbType.VarChar;
p_body_type.Direction = System.Data.ParameterDirection.Input;
p_body_type.Value = (record as CarRecord).Body_type;
command.Parameters.Add(p_body_type);
SqlParameter p_fuel_type = new SqlParameter();
p_fuel_type.ParameterName = "@fuel_type";
p_fuel_type.SqlDbType = System.Data.SqlDbType.VarChar;
p_fuel_type.Direction = System.Data.ParameterDirection.Input;
p_fuel_type.Value = (record as CarRecord).Fuel_type;
command.Parameters.Add(p_fuel_type);
SqlParameter p_motor_power = new SqlParameter();
p_motor_power.ParameterName = "@motor_power";
p_motor_power.SqlDbType = System.Data.SqlDbType.Int;
p_motor_power.Direction = System.Data.ParameterDirection.Input;
p_motor_power.Value = (record as CarRecord).Motor_power;
command.Parameters.Add(p_motor_power);
SqlParameter p_manufactrue_year = new SqlParameter();
p_manufactrue_year.ParameterName = "@manufacture_year";
p_manufactrue_year.SqlDbType = System.Data.SqlDbType.Int;
p_manufactrue_year.Direction = System.Data.ParameterDirection.Input;
p_manufactrue_year.Value = (record as CarRecord).Manufacture_year;
command.Parameters.Add(p_manufactrue_year);
command.Connection = getConnection();
int affectedRows = command.ExecuteNonQuery();
command.Connection.Close();
return affectedRows;
}
解决方案
主键字段上缺少 IDENTITY (1,1) 子句。在您当前的场景中,您需要使用 IDENTITY 定义或在代码中指定主键值,然后将其传递给 SQL 命令。
还有 OUTPUT INSERTED.[PK 列名] 子句: https ://www.sqlservercentral.com/articles/the-output-clause-for-insert-and-delete-statements
使用 OUTPUT 子句,您需要使用 reader 从中获取回复。
如果您在待处理(未提交)事务中工作,您可能需要使用这种方法。
推荐阅读
- c# - 无法从 ASP.NET Web API 获得响应
- git - jenkins 没有从管道运行我的 python 脚本
- typescript - 为什么 Babel 不为 IE11 提供 URLSearchParams polyfill
- excel - Excel vba 翻译 IE.Document 为空
- flutter - 来自 JsonPalceHolder 的 Rx Flutter 请求列表
- architecture - 部署在云上与本地部署时应用程序级别的变化是什么
- c# - 类是继承接口还是实现接口?
- python - 用matplotlib绘制pandas数据框的分段折线图
- amazon-web-services - 当响应状态不是 200 时,响应标头中不存在 X-Amzn-Trace-Id
- sql - 在插入触发器之前创建以在 sql 中添加 sysdate + 30 分钟