简体   繁体   English

无法使用SQL(OLEDB)C#写入Microsoft Access文件

[英]Cannot write to a Microsoft Access file using SQL (OLEDB) C#

I understand that this may be considered a duplicate question, but I am at an impasse and i do not know what it is I am doing wrong, I need help. 我知道这可能是一个重复的问题,但是我陷入了僵局,我不知道这是错的,我需要帮助。 The duplicate question said to use OLEDB in order to write to the Microsoft Access file, where as before I was using the SQL connection to accomplish my task. 重复的问题是说要使用OLEDB来写入Microsoft Access文件,就像我以前使用SQL连接来完成任务一样。 As far as i can tell there are no syntax, logic, or runtime errors and Visual Studios doesnt have an issue either. 据我所知,没有语法,逻辑或运行时错误,Visual Studio也没有问题。

When i run the code and go to add a new entry to the Microsoft Access Database Table, it says it worked, but when I go and look at the file there is NOTHING there. 当我运行代码并去向Microsoft Access数据库表添加新条目时,它说它起作用了,但是当我去查看文件时,那里什么也没有。 Someone please help me, I have goe through all the links, all the web pages, all the search engines, and I dont know what is wrong. 有人请帮助我,我已经遍历了所有链接,所有网页,所有搜索引擎,但我不知道出什么问题了。 I would love to learn what is wrong and how to fix it so I wont ever have to ask for help again. 我很想学习哪里出了问题以及如何解决它,因此我不再需要寻求帮助。

Currently I am a college student and I am working on a team assignment. 目前,我是一名大学生,正在从事团队任务。 Our task, is to create a window that will take input from a user and then add it as a entry into an Microsoft Access File as if it were a SQL database. 我们的任务是创建一个窗口,该窗口将接收来自用户的输入,然后将其作为条目添加到Microsoft Access File中,就好像它是SQL数据库一样。

The issue we are having is that we are trying to add the new entry to a local Microsoft Access file under the table named Artist. 我们遇到的问题是我们试图将新条目添加到名为Artist的表下的本地Microsoft Access文件中。 I have connected to an actual SQL server before and no one else in my group has, even worse no one has done this with using a Microsoft Access file on our PC either. 我以前已经连接到一台实际的SQL服务器,并且我的团队中没有其他人连接过,更糟糕的是,也没有人通过在PC上使用Microsoft Access文件来做到这一点。

I have added the Database (Microsoft Access File) in Visual Studios using the Database Configuration Wizard. 我已经使用数据库配置向导在Visual Studios中添加了数据库(Microsoft Access文件)。 At first I was using straight SQL do to this and then i was told i need to use OLEDB in order to do this. 最初,我使用直接的SQL来做到这一点,然后被告知我需要使用OLEDB才能做到这一点。 SO I have implemented in the code and for some reason I still cannot get it to work. 所以我已经在代码中实现了,由于某种原因,我仍然无法使其正常工作。 If anyone can give me a hand and tell me what it is I am doing wrong, I would greatly appreciate it. 如果有人可以帮助我并告诉我我做错了什么,我将不胜感激。

Details that I believe are important for anyone to help me: 我认为对任何人都有帮助的重要细节:

The name of the table I am attempting to write a new entry to: Artist 我正尝试将新条目写入的表的名称:Artist

Name of the Microsoft Access File: Kays.accdb Microsoft Access文件的名称:Kays.accdb

Location of the Microsoft Access file: C:\\KayDB\\Kays.accdb (local machine) Microsoft Access文件的位置:C:\\ KayDB \\ Kays.accdb(本地计算机)

Once again I would greatly appreciate any help that anyone can give me. 再一次,我将非常感谢任何人都能给我的帮助。 I really am curious as to why the code is not working, please help me understand. 我真的很好奇为什么代码无法正常工作,请帮助我理解。

My code is as follows: 我的代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Kay
{
public partial class Kay_Green : Form
{

    string Username,Fname, Mname, Lname, streetaddress, city, phonenumber, emailaddress, zipcode, taxIDnummber, state;

    string[,] SQLTable = new string[0, 10];

    public Kay_Green()
    {
        InitializeComponent();
    }

    private void btnSave_Click(object sender, EventArgs e)
    {//Save Button

        /*Upon clicking the save button, gather all info and save it into an temp array.
          Then send it to the SQL database.*/

        /*The order of the data in the array will be the same order in the SQL table.*/

        Fname = tbFirstName.Text;
        Mname = tbMiddleInitial.Text;
        Lname = tbLastName.Text;
        streetaddress = tbStreet.Text;
        city = tbCity.Text;
        state = cbState.Text;
        phonenumber = tbPhoneNumber.Text;
        emailaddress = tbEmailAddress.Text;
        zipcode = tbZipCode.Text;
        taxIDnummber = tbTaxID.Text;
        Username = tbUserName.Text;

        /*SQLTable[0,0] = taxIDnummber;
        SQLTable[0,1] = Fname;
        SQLTable[0,2] = Mname;
        SQLTable[0,3] = Lname;
        SQLTable[0,4] = streetaddress;
        SQLTable[0,5] = city;
        SQLTable[0,6] = state;
        SQLTable[0,7] = zipcode;
        SQLTable[0,8] = phonenumber;
        SQLTable[0,9] = emailaddress;*/



        /*Below is the details for the SQL connection*/

        string connectstring="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\KayDB\\Kays.accdb";
        OleDbConnection connection=new OleDbConnection(connectstring);
        OleDbCommand command;
        OleDbDataAdapter adapter;
        DataTable dt = new DataTable();


        string sql ="Insert into ARTIST values ('" + taxIDnummber + "','" 
            + emailaddress + "','" + Fname + "','" + Mname + "','"
            + Lname + "','" + phonenumber+"','"+ Username + "','" 
            + streetaddress + "','" + city + "','" +state+ "','" 
            + zipcode + "')";



        try
        {

            connection.Open();

            command = new OleDbCommand(sql, connection);

            MessageBox.Show("Connection Open And data added to table! ");


            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Can not open connection ! " + ex.StackTrace.ToString());

        }
        /*Above is the details for the SQL connection*/

    }

    private void btnCancel_Click(object sender, EventArgs e)
    {//Cancel Button
        tbCity.Clear();
        tbEmailAddress.Clear();
        tbFirstName.Clear();
        tbLastName.Clear();
        tbMiddleInitial.Clear();
        tbPhoneNumber.Clear();
        tbStreet.Clear();
        tbTaxID.Clear();
        tbZipCode.Clear();
        tbUserName.Clear();

        Close();//Go back to switchboard from here


    }

    private void btnClear_Click(object sender, EventArgs e)
    {//Clear button

        tbCity.Clear();
        tbEmailAddress.Clear();
        tbFirstName.Clear();
        tbLastName.Clear();
        tbMiddleInitial.Clear();
        tbPhoneNumber.Clear();
        tbStreet.Clear();
        tbTaxID.Clear();
        tbZipCode.Clear();
        tbUserName.Clear();
    }


}

} }

This code 此代码

command = new OleDbCommand(sql, connection);

sets up a command but does not run it 设置命令但不运行

you need to run this afterwards: 您需要随后运行此命令:

command.ExecuteNonQuery();

This has nothing to do with connection strings 这与连接字符串无关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM