English 中文(简体)
在访问中插入新行
原标题:Inserting new row into access

或许我做错了,但我试图用 ado.net 在桌子上插入一行新词。 它告诉我语法是错误的。

customers = new DataTable("Customers");
cmd = "SELECT * FROM Customers";
con = new OleDbDataAdapter(cmd, strconn);
con.FillSchema(customers, SchemaType.Source);
con.Fill(customers);
DataRow cur;
cur = customers.NewRow();
cur["Company"] = textBoxCompany.Text;
cur["First Name"] = textBoxFirstName.Text;
cur["Last Name"] = textBoxLastName.Text;
cur["E-mail Address"] = textBoxEmail.Text;
cur["Job Title"] = textBoxTitle.Text;
cur["Business Phone"] = textBoxPhone.Text;
customers.Rows.Add(cur);
OleDbCommandBuilder cb = new OleDbCommandBuilder(con);
con.InsertCommand = cb.GetInsertCommand();           
con.InsertCommand.Parameters.AddWithValue("Company",cur["Company"]);
con.InsertCommand.Parameters.AddWithValue("Last Name",cur["Last Name"]);
con.InsertCommand.Parameters.AddWithValue("First Name", cur["First Name"]);
con.InsertCommand.Parameters.AddWithValue("E-Mail Address",cur["E-mail Address"]);
con.InsertCommand.Parameters.AddWithValue("Job Title",cur["Job Title"]);
con.InsertCommand.Parameters.AddWithValue("Business Phone",cur["Business Phone"]);
con.Update(customers);

"在 INSERT 输入语句中的语法错误"

堆叠跟踪 :

at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
at Project2.Form1.button1_Click(Object sender, EventArgs e) in Form1.cs:line 54
最佳回答

根据"此关于命令生成的文件

Automatic command generation logic fails if column names or table names contain any special characters, such as spaces, periods, quotation marks, or other nonalphanumeric characters, even if delimited by brackets. Fully qualified table names in the form of catalog.schema.table are supported.

由于您的列名称有空格, 查询将无法工作。 您需要手动创建插入语句, 该语句相当简单。 只要在所有列名称上加上括号, 并使用参数来计算没有空格或特殊字符的值 。

INSERT INTO [TableName] ([Column1], [Column2], ...)
VALUES (@Column1, @Column2, ...)

您在创建参数时也会使用这些参数名称 :

con.InsertCommand.Parameters.AddWithValue("@column1", cur["..."]);
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签