无法使用SQL(OLEDB)C#

时间:2017-03-28 14:27:39

标签: c# sql oledb sqlclient

我知道这可能被认为是一个重复的问题,但我陷入了僵局,我不知道我做错了什么,我需要帮助。重复的问题说使用OLEDB来写入Microsoft Access文件,就像我之前使用SQL连接完成我的任务一样。据我所知,没有语法,逻辑或运行时错误,Visual Studios也没有问题。

当我运行代码并向Microsoft Access数据库表中添加一个新条目时,它表示它有效,但是当我去查看该文件时,那里没有任何内容。有人请帮助我,我已经完成了所有的链接,所有的网页,所有的搜索引擎,我不知道有什么问题。我很想知道出了什么问题以及如何解决这个问题,所以我再也不用再寻求帮助了。

目前我是一名大学生,我正在从事团队工作。我们的任务是创建一个窗口,该窗口将从用户获取输入,然后将其作为条目添加到Microsoft Access文件中,就像它是SQL数据库一样。

我们遇到的问题是我们正在尝试将新条目添加到名为Artist的表下的本地Microsoft Access文件中。我以前连接过一个实际的SQL服务器而且我的组中没有其他人,更糟糕的是没有人在我们的PC上使用Microsoft Access文件。

我已使用数据库配置向导在Visual Studios中添加了数据库(Microsoft Access文件)。起初我使用直接SQL做到这一点,然后我被告知我需要使用OLEDB才能做到这一点。所以我已经在代码中实现了,由于某种原因,我仍然无法让它工作。如果有人能帮我一把,告诉我这是错的,我会非常感激。

我认为对任何人都有帮助的重要信息:

我试图写一个新条目的表的名称:艺术家

Microsoft Access文件的名称:Kays.accdb

Microsoft Access文件的位置:C:\ KayDB \ Kays.accdb(本地计算机)

我再次非常感谢任何人都能给予我的帮助。我真的很好奇为什么代码不工作,请帮助我理解。

我的代码如下:

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();
    }


}

}

1 个答案:

答案 0 :(得分:0)

此代码

command = new OleDbCommand(sql, connection);

设置命令但不运行它

之后你需要运行它:

command.ExecuteNonQuery();

这与连接字符串

无关
相关问题