使用Microsoft Visual Studio连接到Microsoft Access数据库?

时间:2014-04-25 21:35:07

标签: c#

我对这段代码的一个大问题是,当我尝试连接到我的数据库时,该程序会引发一个问题。我按下我的btnSubmit_Click按钮,它告诉我这个错误:“'Microsoft.ACE.OLEDB.12.0'提供程序未在本地计算机上注册。”我把我的代码放在下面。任何人都可以帮我弄清楚出了什么问题吗?

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            OleDbConnection Connect;
            OleDbCommand command;
            Connect = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + @"C:\Users\drwi\Desktop\database\Javadata1.accdb");
            Connect.Open();
            command = Connect.CreateCommand();
            command.CommandText = "insert"+txtFirstName.Text+","+txtLastName+"into T_Users;";
            Connect.Close(); 
        }

        private void Display_Click(object sender, EventArgs e)
        {
            OleDbConnection Connect;
            OleDbCommand command;
            Connect = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + @"C:\Users\drwi\Desktop\database\Javadata1.accdb");
            Connect.Open();
            command = Connect.CreateCommand();
            command.CommandText = "Select * from T_Users;";
            OleDbDataReader read = command.ExecuteReader();
            if (read.Read())
            {
                txtOutput.Text = read["UserID"].ToString()+","+read["FirstName"].ToString()+","+read["LastName"].ToString();
            }
            else
            {
                MessageBox.Show("Invalid DataBase Connection");
            };
            Connect.Close(); 
        }
    }
}

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

command.CommandText = "insert"+txtFirstName.Text+","+txtLastName+"into T_Users;";

应该是

command.CommandText = "insert '" +txtFirstName.Text + "', '" + txtLastName.Text+ "' into T_Users;";

(添加.text)