尝试连接到 Access 数据库 table 以检索数据,但遇到困难
Trying to connect to a Access database table in order to retrieve data, but encounter difficulties
第一次做数据库编程,所以我只是在Access中做了一个数据库来尝试用它做点什么。我在桌面上创建的数据库名为 "TestDatabase",而我在该数据库中创建的 table 名为 "TestTable"。这是我的代码:
using System;
using System.Data;
using System.Data.SqlClient;
namespace DatabaseTest
{
class Test
{
static void Main(string[] args)
{
// I don't know if my connection is correct or not. My access database is on my local desktop though
string connectionString = "Data Source = (local); Initial Catalog = TestDatabase; Integrated Security = SSPI";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataReader reader = null;
SqlCommand command = new SqlCommand("SELECT * from TestTable", connection);
connection.Open();
try
{
reader = command.ExecuteReader();
}
catch (InvalidOperationException e)
{
Console.WriteLine(e.ToString());
}
// print all the data in the table
while (reader.Read())
{
Console.Write(reader[0].ToString() + ", ");
Console.Write(reader[1].ToString() + ", ");
Console.Write(reader[2].ToString() + ", ");
Console.Write(reader[3].ToString() + ", ");
Console.WriteLine(reader[4].ToString());
}
}
Console.ReadLine();
}
}
}
这是我的 table 样子,如果您想知道:(只是一个玩具示例)
ID First Name Last Name Age Friend
1 Leon Ma 18 Yes
2 Amy Jane 16 No
3 David Zhang 20 No
4 Alan Yue 19 Yes
但是,它不起作用,因为我的控制台上没有显示任何内容。我做错了什么。真的需要一些帮助。谢谢
您需要类似以下内容才能连接到 Access DB
conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\DbPath\SomeAccessFileName.accdb")
对于标准安全性,配置文件将像这样设置
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;
Persist Security Info=False;
参考LinkAccess connection strings
在代码隐藏中创建连接对象的用法
using System.Data.Odbc;
using(OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
OleDbCommand command = new OleDbCommand("SELECT * from TestTable", connection)
using(OleDbDataReader reader = command.ExecuteReader())
{
while(reader.Read())
{
Console.Write(reader[0].ToString() + ", ");
Console.Write(reader[1].ToString() + ", ");
Console.Write(reader[2].ToString() + ", ");
Console.Write(reader[3].ToString() + ", ");
Console.WriteLine(reader[4].ToString());
}
}
}
第一次做数据库编程,所以我只是在Access中做了一个数据库来尝试用它做点什么。我在桌面上创建的数据库名为 "TestDatabase",而我在该数据库中创建的 table 名为 "TestTable"。这是我的代码:
using System;
using System.Data;
using System.Data.SqlClient;
namespace DatabaseTest
{
class Test
{
static void Main(string[] args)
{
// I don't know if my connection is correct or not. My access database is on my local desktop though
string connectionString = "Data Source = (local); Initial Catalog = TestDatabase; Integrated Security = SSPI";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataReader reader = null;
SqlCommand command = new SqlCommand("SELECT * from TestTable", connection);
connection.Open();
try
{
reader = command.ExecuteReader();
}
catch (InvalidOperationException e)
{
Console.WriteLine(e.ToString());
}
// print all the data in the table
while (reader.Read())
{
Console.Write(reader[0].ToString() + ", ");
Console.Write(reader[1].ToString() + ", ");
Console.Write(reader[2].ToString() + ", ");
Console.Write(reader[3].ToString() + ", ");
Console.WriteLine(reader[4].ToString());
}
}
Console.ReadLine();
}
}
}
这是我的 table 样子,如果您想知道:(只是一个玩具示例)
ID First Name Last Name Age Friend
1 Leon Ma 18 Yes
2 Amy Jane 16 No
3 David Zhang 20 No
4 Alan Yue 19 Yes
但是,它不起作用,因为我的控制台上没有显示任何内容。我做错了什么。真的需要一些帮助。谢谢
您需要类似以下内容才能连接到 Access DB
conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\DbPath\SomeAccessFileName.accdb")
对于标准安全性,配置文件将像这样设置
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;
Persist Security Info=False;
参考LinkAccess connection strings
在代码隐藏中创建连接对象的用法
using System.Data.Odbc;
using(OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
OleDbCommand command = new OleDbCommand("SELECT * from TestTable", connection)
using(OleDbDataReader reader = command.ExecuteReader())
{
while(reader.Read())
{
Console.Write(reader[0].ToString() + ", ");
Console.Write(reader[1].ToString() + ", ");
Console.Write(reader[2].ToString() + ", ");
Console.Write(reader[3].ToString() + ", ");
Console.WriteLine(reader[4].ToString());
}
}
}