我的数据库中有一个表Teacher
:
TABLE Teacher
(
ID CHAR (7) NOT NULL ,
name VARCHAR (50) NOT NULL ,
surname VARCHAR (50) NOT NULL ,
email VARCHAR (50) NOT NULL ,
phone CHAR (13) NOT NULL
)
在数据库中,我有一个INSTEAD OF INSERT
触发器,用于创建ID
以及来自姓氏和号码的电子邮件。我在SQL Server中插入时一切正常。我能做到
INSERT INTO Teacher(name, surname, phone)
VALUES('John', 'Doe', '+111111111111')
我在这个数据库上实现了ORM。
public static String SQL_INSERT =
"INSERT INTO \"Teacher\" VALUES (@ID, @name, @surname, @email, @phone)";
public static int Insert(Teacher teacher, Database pDb = null)
{
Database db;
if (pDb == null)
{
db = new Database();
db.Connect();
}
else
{
db = (Database)pDb;
}
SqlCommand command = db.CreateCommand(SQL_INSERT);
PrepareCommand(command, teacher);
int ret = db.ExecuteNonQuery(command);
if (pDb == null)
{
db.Close();
}
return ret;
}
private static void PrepareCommand(SqlCommand command, Teacher teacher)
{
command.Parameters.AddWithValue("@ID", teacher.ID);
command.Parameters.AddWithValue("@name", teacher.Name);
command.Parameters.AddWithValue("@surname", teacher.Surname);
command.Parameters.AddWithValue("@email", teacher.Email);
command.Parameters.AddWithValue("@phone", teacher.Phone);
}
问题是当我尝试从ORM插入时。我必须这样做,因为它不允许我在没有所有强制属性的情况下插入。
Teacher newTeacher = new Teacher ();
newTeacher.ID = "";
newTeacher.Name= "John";
newTeacher.Surname= "Doe";
newTeacher.Email = "";
newTeacher.Phone= "+111111111111";
TeacherTable.Insert(newTeacher, db);
有没有办法插入而无需在ID和电子邮件中分配空字符串? it inserts fine,但代码对我来说很糟糕。谢谢你的帮助。
答案 0 :(得分:2)
如果使用默认约束定义表,则在插入时将值设置为默认值,而无需触发器...
CREATE TABLE Teacher
(
ID CHAR (7) NOT NULL, --don't know what is the content here... In most cases an ID column with INT IDENTITY is perefered
name VARCHAR (50) NOT NULL CONSTRAINT DF_Teacher_name DEFAULT(''),
surname VARCHAR (50) NOT NULL CONSTRAINT DF_Teacher_surname DEFAULT('') ,
email VARCHAR (50) NOT NULL CONSTRAINT DF_Teacher_email DEFAULT('') ,
phone CHAR (13) NOT NULL CONSTRAINT DF_Teacher_phone DEFAULT('')
);
顺便说一句:我不会强迫电话号码适合13个字符......
答案 1 :(得分:1)
ORM正在做它应该做的事情。如果表字段可以为空,并且您的变量是未设置的引用类型,那么它应该插入NULL。顺便说一句,字符串是引用类型。
处理此问题最简单的方法之一是向教师类添加一个构造函数,为所有值设置默认值,这样,每次启动新对象时都不必设置它们。
E.g。
public class Teacher
{
public Teacher()
{
ID = "";
Name= "";
Surname= "";
Email = "";
Phone= "";
}
....
}
然后您可以执行以下操作:
Teacher newTeacher = new Teacher ();
newTeacher.Name = "Johnny";
TeacherTable.Insert(newTeacher, db);
只设置您需要的变量,所有其他变量将是构造函数中的默认设置。
答案 2 :(得分:0)
您可以将PrepareCommand默认为空白。
private static void PrepareCommand(SqlCommand command, Teacher teacher)
{
command.Parameters.AddWithValue("@ID", teacher.ID ?? "");
command.Parameters.AddWithValue("@name", teacher.Name);
command.Parameters.AddWithValue("@surname", teacher.Surname);
command.Parameters.AddWithValue("@email", teacher.Email ?? "");
command.Parameters.AddWithValue("@phone", teacher.Phone);
}
另外,如果它们为null,则根本无法传递它们,然后在触发器中提供默认值。
private static void PrepareCommand(SqlCommand command, Teacher teacher)
{
if(string.IsNullOrEmpty(teacher.ID))
command.Parameters.AddWithValue("@ID", teacher.ID);
command.Parameters.AddWithValue("@name", teacher.Name);
command.Parameters.AddWithValue("@surname", teacher.Surname);
if(string.IsNullOrEmpty(teacher.Email))
command.Parameters.AddWithValue("@email", teacher.Email);
command.Parameters.AddWithValue("@phone", teacher.Phone);
}
这两个都可以让你创建教师对象,如...
Teacher newTeacher = new Teacher ();
newTeacher.Name= "John";
newTeacher.Surname= "Doe";
newTeacher.Phone= "+111111111111";
TeacherTable.Insert(newTeacher, db);