错误的access数据库插入 [英] Wrongful acces database insertion

查看:89
本文介绍了错误的access数据库插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中插入信息时遇到了麻烦,我正在开展一个项目,我需要能够将火车车厢的位置存储在数据库中。每列代表不同的轨道,但是当我在列中插入列车时,下一行i
插入不同的列将在下一行中,这不可能发生,它们需要能够在同一栏。我还将发布我在c#中编写的插入代码。所以我的问题是,有没有人知道我如何解决这个问题?。


亲切的问候Arno。


ps。因为我的帐户未经过验证,我无法将图片放入图片中。我希望它足够清楚。

 

static void Insert(string data,string spoor)//在数据库中插入货车
{
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = @" Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\ Users\Arno \Documents\Code\Visual Studio \Database_GIP20161.accdb;持久安全信息=错误;";
connection.Open();

string query = String.Format(@" INSERT INTO Posities({0})VALUES('{1}');",spoor,data);

OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = query;
command.ExecuteNonQuery();
connection.Close();
}

解决方案

试试此代码,查看

$ b $中的评论b

使用System.Data.OleDb; 
命名空间TransactionsViewer //更改命名空间名称
{
公共类操作
{
public void Insert(string data,string spoor)//在数据库中插入货车
{
/ *
*在下一行代码上放置一个断点,检查传入值
* /

var connectionString =" Provider = Microsoft .ACE.OLEDB.12.0;数据" +
@" Source = C:\ Users\Arno \Documents\Code\Visual Studio \Database_GIP20161.accdb; " +
" Persist Security Info = False;" ;;

var insertStatement =


" INSERT INTO Posities({spoor})VALUES @ Value" ;;

使用(var cn = new OleDbConnection(){ConnectionString = connectionString})
{
using(var cmd = new OleDbCommand(){Connection = cn,CommandText = insertStatement} )
{
cn.Open();
cmd.Parameters.AddWithValue(" @ Value",data);
cmd.ExecuteNonQuery();
}
}
}
}
}

编辑:哇,发布后,语法已经过时了/ p>




I'm having trouble with inserting information in a database, i'm working on a project where i need to be able to store the location of trainwagons in a database. Each column stands for a different track but when i insert a train in column the next one i insert in a different column will be in the next row and this can't happen, they need to be able to be in the same column. I will also post my insert code that i wrote in c#. So my question is, does anybody know how i can fix this ?.

Kind regards Arno.

ps. I wasn't able to put in the picture because my account isn't verified. I hope it's clear enough.

static void Insert(string data,string spoor) // Insert wagons in the database { OleDbConnection connection = new OleDbConnection(); connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Arno\Documents\Code\Visual Studio\Database_GIP20161.accdb; Persist Security Info=False;"; connection.Open(); string query = String.Format(@"INSERT INTO Posities({0}) VALUES ('{1}');", spoor, data); OleDbCommand command = new OleDbCommand(); command.Connection = connection; command.CommandText = query; command.ExecuteNonQuery(); connection.Close(); }

解决方案

Try this code, see comments within

using System.Data.OleDb;
namespace TransactionsViewer // change namespace name
{
    public class Operations
    {
	    public void Insert(string data, string spoor) // Insert wagons in the database
	    {
            /*
             * Place a breakpoint on the next line of code, examine incoming values
             */

			var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data " + 
			                       @"Source=C:\Users\Arno\Documents\Code\Visual Studio\Database_GIP20161.accdb; " + 
			                       "Persist Security Info=False;";

		    var insertStatement =


"INSERT INTO Posities ({spoor}) VALUES @Value"; using (var cn = new OleDbConnection() {ConnectionString = connectionString }) { using (var cmd = new OleDbCommand() {Connection = cn, CommandText = insertStatement }) { cn.Open(); cmd.Parameters.AddWithValue("@Value", data); cmd.ExecuteNonQuery(); } } } } }

EDIT: Wow, syntax is way off after posting


这篇关于错误的access数据库插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆