简体   繁体   English

如何在链接到另一个表的MS Access表中添加行?

[英]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.

相关问题 从ms-access数据库中选择一个具有链接表的列 - selecting a column from ms-access database which has linked tables 如何向使用MS Access表中的数据填充的数据表添加新行 - How can I add a new row to a datatable that I populated with data from a MS Access table 如何在MS Access表中获取最后插入的行标识 - How to get last inserted row identity in a table of MS Access 如何使用VBA(或C#)将MS Access 2007中的链接表复制到本地表? - How to copy a linked table in MS Access 2007 to local table with VBA (or C#)? 通过DSN与SQLServer链接表的MS Access数据库的ODBC连接 - ODBC connection to MS Access database with SQLServer linked tables via DSN 以编程方式使用 C# 更新 MS Access 数据库中的链接表 - Update linked tables in MS Access Database with C# programatically 如何在视图中访问链接到另一个表的所有元素? - How can I access, in the view, all the elements of a table linked to another? 如何通过C#以编程方式刷新MS Access链接表(带有Excel文件)? - How to refresh a MS Access linked table (with Excel file) programatically via C#? 无法从.NET C#Winforms向MS-Access 2000表添加新行 - Can not add new row to MS-Access 2000 table from .NET C# Winforms 连接到具有链接表的远程Access 2003数据库 - Connect to remote Access 2003 database which has linked tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM