[英]How to add row to MS Access table which linked to another tables?
I have MS Access database (I have to use Access) with this structure (names barely traslated for this question): 我有具有以下结构的MS Access数据库(我必须使用Access)(名称几乎未涉及此问题):
My programm allow to add row to each table. 我的程序允许向每个表添加行。 To work with database I use OleDbDataAdapter
. 要使用数据库,我使用OleDbDataAdapter
。 This code successfully add row to "desease": 此代码成功将行添加到“ desease”:
connection.Open();
OleDbCommand command = new OleDbCommand("INSERT INTO "
+ "desease (title, description) VALUES (?, ?)", connection);
command.Parameters.AddWithValue("title", "input text");
command.Parameters.AddWithValue("description", "input text");
adapter.InsertCommand = command;
adapter.InsertCommand.ExecuteNonQuery();
connection.Close();
But I also need to do same to "visits" table. 但是我也需要对“访问”表进行同样的操作。 Well, I try this: 好吧,我试试这个:
connection.Open();
OleDbCommand command = new OleDbCommand("INSERT INTO Посещения " +
"(patient ID, doctor ID, desease ID, visit date) " +
"VALUES (?, ?, ?, ?)", connection);
command.Parameters.AddWithValue("patient ID", "1");
command.Parameters.AddWithValue("doctor ID", "1");
command.Parameters.AddWithValue("desease ID", "1");
command.Parameters.AddWithValue("visit date", "01.05.1999");
adapter.InsertCommand = command;
adapter.InsertCommand.ExecuteNonQuery();
connection.Close();
And get error: syntax error in instruction INSERT INTO
. 并得到错误: syntax error in instruction INSERT INTO
。
So what is right command for "visits"? 那么“访问”的正确命令是什么? What datatypes i have to send to parameters? 我必须发送给参数什么数据类型? And especially for IDs. 特别是对于ID。 In database they have number type (in translation it called so). 在数据库中,它们具有数字类型(在翻译中这样称呼)。 Visit date
is also have date
type Visit date
也有date
类型
I can't find any info about this. 我找不到有关此的任何信息。
If your column names contains more than one word, you need to them in a square brackets like [patient ID]
, [doctor ID]
, [desease ID]
and [visit date]
如果您的列名包含多个单词,则需要在方括号中将其括起来,例如[patient ID]
, [doctor ID]
, [desease ID]
和[visit date]
As a best practice, change their names to one word. 最佳做法是将其名称更改为一个单词。
Also use using
statement to dispose your connections and commands automatically instead of calling Close
methods manually. 也可以使用using
语句自动处理连接和命令,而不是手动调用Close
方法。
using(var connection = new OleDbConnection(conString))
using(var command = connection.CreateCommand())
{
commmand.CommandText = @"INSERT INTO Посещения([patient ID],[doctor ID], [desease ID], [visit date])
VALUES(?, ?, ?, ?)";
command.Parameters.AddWithValue("patient ID", "1");
command.Parameters.AddWithValue("doctor ID", "1");
command.Parameters.AddWithValue("desease ID", "1");
command.Parameters.AddWithValue("visit date", "01.05.1999");
adapter.InsertCommand = command;
connection.Open();
adapter.InsertCommand.ExecuteNonQuery();
}
Based on your column names, I think your first 3 column should be numeric type instead of character and the last one should be better for DATETIME
type. 根据您的列名,我认为您的前三列应为数字类型而不是字符,而最后一列应更适合DATETIME
类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.