[英]Calling class to do database connection
我在用C#编程。 我正在尝试创建一个类,该类在被调用时将创建与数据库的连接。
我的数据库连接类在这里:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
namespace HouseServer
{
class db
{
// Variable to hold the driver and location of database
public static OleDbConnection dbConnection;
// Database connection
public db()
{
// Define the Access Database driver and the filename of the database
dbConnection = new OleDbConnection("Provider=Microsoft.Ace.OLEDB.12.0; Persist Security Info = False; Data Source=Houses.accdb");
// Open the connection
dbConnection.Open();
}
}
}
主程序在这里:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
namespace HouseServer
{
class Program : db
{
// List for holding loaded houses
static List<house> houses = new List<house>();
// Variable to hold "command" which is the query to be executed
private static OleDbCommand query;
// Variable to hold the data reader to manipulate data from the database
static OleDbDataReader dataReader;
static void Main(string[] args)
{
// Get the houses in a list
List<house> c = getHousesFromDb();
foreach (house yay in c)
{
// Show each house's full address
Console.WriteLine(yay.house_number + " " + yay.street);
Console.WriteLine(yay.house_town);
Console.WriteLine(yay.postcode);
}
// Readline to prevent window from closing
Console.ReadLine();
}
// Function which loads all of the houses from the database
private static List<house> getHousesFromDb()
{
// Define the query to be executed
query = new OleDbCommand("SELECT * FROM houses", dbConnection);
// Execute the query on the database and get the data
dataReader = query.ExecuteReader();
// Loop through each of the houses
while (dataReader.Read())
{
// Create a new house object for temporarily storing house
house house = new house();
// Create the house that we've just loaded
house.house_id = Convert.ToInt32(dataReader["house_id"]);
house.house_number = Convert.ToInt32(dataReader["house_number"]);
house.street = dataReader["house_street"].ToString();
house.house_town = dataReader["house_town"].ToString();
house.postcode = dataReader["house_postcode"].ToString();
// Now add the house to the list of houses
houses.Add(house);
}
// Return all of the houses in the database as a List<house>
return houses;
}
}
}
我以为放在class Program : db
会在程序打开时调用db
构造函数,但是当代码到达该行时dataReader = query.ExecuteReader();
,出现错误“ ExecuteReader:连接属性尚未初始化”。
我想要实现的只是在另一个类中的数据库连接,我可以调用该数据库连接并将其用于所有代码。
我应该以其他方式调用数据库类吗?
不,什么都没有创建Program
实例,什么也没有创建db
实例。 但是,我强烈建议您完全更改设计:
Program
类型在逻辑上不是从db
派生的 这肯定看起来不合适,您的程序不应继承或扩展数据库类。 您的数据库类本身就是其自己的抽象数据类型。 您的程序应使用数据库类,但不能扩展它。
我会稍微改变一下
DBClass.GetData();
那就是您的程序应该将数据库类用作黑匣子,它绝对不应从该类继承。 它应该在没有详细工作原理的情况下使用它。 在您的代码中:
// List for holding loaded houses
static List<house> houses = new List<house>();
// Variable to hold "command" which is the query to be executed
private static OleDbCommand query;
// Variable to hold the data reader to manipulate data from the database
static OleDbDataReader dataReader;
static void Main(string[] args)
{
// Get the houses in a list
List<house> c = getHousesFromDb();
您应该隐藏OleDbCommand和OleDbDatareader对象的详细信息,尽管并不需要在其他地方对其进行管理。 您的getHousesFromDB
应该这样称呼:
MyDBClass.GetHousesFromDB()
其中MyDBClass
是管理数据库读/写的静态类。 GetHousesFromDB
的签名应该返回一些东西,以达到IList<House> GetHousesFromDB()
的效果。
尽管乔恩·斯凯特(Jon Skeet)和乔恩(JonH)提出了正确的观点,但我会回答您为什么会得到例外的原因。 从那里开始,您应该听取他们的建议,并从头开始重做。
出现异常的原因是它是在db
的构造函数中初始化的,并且永远不会被调用。
如果将此行添加到Main
,则程序应该可以运行。
new Program();
但要重申: 听取他们的建议,然后重新开始。 在许多情况下,这些玩具项目迅速发展为功能完善的企业应用程序,一旦您到达那里,一开始的错误就永远存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.