I have a table Teacher in my database:
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
)
In the database I have an INSTEAD OF INSERT trigger which creates ID and email from surname and numbers. Everything works fine when I insert in SQL Server. I can do
INSERT INTO Teacher(name, surname, phone)
VALUES('John', 'Doe', '+111111111111')
I'm implementing ORM on this database.
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);
}
The problem is when I try to insert from the ORM. I have to do it like this because it doesn't let me insert without all mandatory attributes.
Teacher newTeacher = new Teacher ();
newTeacher.ID = "";
newTeacher.Name= "John";
newTeacher.Surname= "Doe";
newTeacher.Email = "";
newTeacher.Phone= "+111111111111";
TeacherTable.Insert(newTeacher, db);
Is there any way I could insert without having to assign empty strings into ID and Email? it inserts fine, but the code looks bad to me. Thanks for help.
IDandemailfrom surname and numbers." He is storing neither nulls nor blanks for those values.