如何在Visual Studio 2012中连接Ms访问数据库

如何在Visual Studio 2012中连接Ms访问数据库

问题描述:

您好,

我正在使用visual studio2012设计一个带有ms-access 2007数据库的Windows应用程序。我试图连接数据库,但我收到了错误



未在ConnectionString中指定OLE DB提供程序。一个例子是'Provider = SQLOLEDB;'。



请帮帮我

这是我的代码



使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System .Data;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System.Threading.Tasks;

使用System.Windows.Forms;

使用System.Data.OleDb;

使用System.Data。 SqlClient;



名称空间Nilkamal

{

公共部分类addnewspaper:表格

{



public addnewspaper()

{

InitializeComponent();

}

private OleDbConnection conn2 = new OleDbConnection();

private void Form2_Load(object sender,EventArgs e)

{



}



private void textBox1_TextChanged(object sender,EventArgs e)

{



}



private void label2_Click(object sender,EventArgs e)

{



}



private void button1_Click(object sender,EventArgs e)

{

conn2.ConnectionString = @ Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\\Users\\DEVENDRA \\Documents\\ nilkamal.mdb;

string newspaper_name = txtPaper.Text;

conn2.Open();

OleDbCommand cmd = new OleDbCommand(insert into newspaper_detail(newspaper_name)+values(@newspaper_name), conn2);

if(conn2.State == ConnectionState.Open)



{

cmd.Parameters .Add(@ newspaper_name,OleDbType.Ch ar,50);

cmd.Parameters [@ newaper_name]。值= newspaper_name;

尝试

{

cmd.ExecuteNonQuery();

MessageBox.Show(已插入记录);

conn2.Close();


}

catch(OleDbException exp)

{

MessageBox.Show(exp.Source);



}

}

else

{

MessageBox 。显示(连接失败);

}

}



}

}

Hello,
I am using visual studio2012 for designing a windows application with ms-access 2007 database . I tried to connect the database but i got the error

An OLE DB Provider was not specified in the ConnectionString. An example would be, 'Provider=SQLOLEDB;'.

Please help me
Here is my Code

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

namespace Nilkamal
{
public partial class addnewspaper : Form
{

public addnewspaper()
{
InitializeComponent();
}
private OleDbConnection conn2 = new OleDbConnection();
private void Form2_Load(object sender, EventArgs e)
{

}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void label2_Click(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
conn2.ConnectionString = "@Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\DEVENDRA\\Documents\\nilkamal.mdb";
string newspaper_name = txtPaper.Text;
conn2.Open();
OleDbCommand cmd = new OleDbCommand("insert into newspaper_detail(newspaper_name)" + "values(@newspaper_name)", conn2);
if (conn2.State == ConnectionState.Open)

{
cmd.Parameters.Add("@newspaper_name", OleDbType.Char, 50);
cmd.Parameters["@newaper_name"].Value = newspaper_name;
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Records Inserted");
conn2.Close();

}
catch(OleDbException exp)
{
MessageBox.Show(exp.Source);

}
}
else
{
MessageBox.Show("Connection failed");
}
}

}
}

http://msdn.microsoft.com/en-us/library/ms171893.aspx
http://msdn.microsoft.com/en-us/library/ms171893.aspx