请问用.Net 怎么连数据库啊?

来源:百度知道 编辑:UC知道 时间:2024/05/23 18:18:08
我主要想实现的是用.Net读取Access数据库中的记录,该怎么做啊?如果说不清楚贴教程也可以的哦

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
namespace ConsoleApplication1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{

//以下是连接Access
OleDbConnection dbconn=new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data source=c:\a.mdb");
dbconn.Open();
//OleDbCommand cmd=new OleDbCommand("select * from test1",dbconn);

OleDbCommand cmd=dbconn.CreateCommand();
cmd.CommandText="select * from test1";
cmd.CommandType=CommandType.Text;
OleDbDataReader reader=cmd.ExecuteReader();

//以下是连接SQL2000
/*SqlConnection conn=new SqlConnection("server=localhost;Initial catalog=NorthWin