我不明白为什么你会想在这种情况下使用嵌套类。你写它的方式,子类是你所需要的。如果你想要多个方法(或者你称之为“函数”),只需添加你的方法。
有没有一些隐藏的原因,你想在这里使用嵌套类?作为一般规则,很少需要嵌套类。
namespace SameAsPrincipal
{
public class Class1
{
private SQLiteConnection handle;
public Class1(string db_file)
{
handle = new SQLiteConnection(db_file);
}
public int AddRecord(Record record)
{
// use handle to add record and get record Id
return record.Id;
}
public void DeleteRecord(int id)
{
// Use handle to delete record
}
}
}
当你实例化对象时,你将传入你的db_file并且连接对象将被创建。然后每个方法都可以在被调用时使用该连接对象。但是,在调用每个方法时创建连接并在操作完成后尽快处置连接通常是更好的主意。当然,这取决于您的业务以及它们是否跨国。大多数情况下,使用“using”块来实例化连接是使用连接对象的好方法。越早释放连接越快机器将重新使用该连接,您可以查找连接池以了解更多信息。
下面是使用“使用”使用存储过程添加一个人的实例方法:
public int AddPerson(Person person)
{
using (var connection = new SQLiteConnection(dbFile))
{
connection.Open();
using (var command = new SQLiteCommand("spAddPerson",connection))
{
command.CommandType = CommandType.StoredProcedure;
var idParameter = new SQLiteParameter("@Id", DbType.Int32);
idParameter.Direction = ParameterDirection.Output;
command.Parameters.Add(idParameter);
command.Parameters.AddWithValue("@FirstName", person.FirstName);
command.Parameters.AddWithValue("@LirstName", person.LastName);
command.ExecuteNonQuery();
}
}
return person.Id;
}
编辑:至于下面
有几件事情您的评论:
- 使用名称空间而不是父类来对类进行分组。
- 您应该将所有数据库方法添加到数据库类并创建用于建模对象的类,而不是子类。
- 每个类应该在它自己的文件中
- 命名空间部分是.. [] * I.E. Music类具有名称空间YourApplication.YourProject.Models - 在YourProject项目中,在名为Music的第一级文件夹中,您将找到一个名为Music.cs的文件,并在该文件中找到您的音乐课程。这不是要求,编译器不关心这样的结构。当你开始获得更多的代码时,它只会让你的生活更轻松。
这里是我讲的代码结构的一个例子(记住每一部分是它自己的文件)
在您的项目称为模型的根目录下创建一个文件夹。在这个模型文件夹中创建一个名为Music.cs
namespace YourApplication.YourProject.Models
{
public class Music
{
public int Id { get; set; }
public string Title { get; set; }
public double Length { get; set; }
public string Artist { get; set; }
public string Album { get; set; }
}
}
在这同一(模型)文件夹中创建一个名为Film.cs
namespace YourApplication.YourProject.Models
{
public class Film
{
public int Id { get; set; }
public string Title { get; set; }
public double Length { get; set; }
public string Director { get; set; }
public string[] Actors { get; set; }
}
}
现在回到项目根(在模型不再文件文件夹)创建一个名为Persistence的新文件夹。
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using YourApplication.YourProject.Models;
namespace YourApplication.YourProject.Persistence
{
public static class DatabaseActions
{
public static string dbFile;
public static Music[] ListMusic()
{
var musicList = new List<Music>();
// database call to get all music
using (var connection = new SQLiteConnection(dbFile))
{
connection.Open();
using (var command = new SQLiteCommand("spGetMusic", connection))
{
var reader = command.ExecuteReader();
// The try finally blocks are not strictly needed as these will are suppose to be called upon disposal
try
{
// loop through records creating music objects
while (reader.Read())
{
var music = new Music();
music.Id = reader.GetInt32(0);
music.Title = reader.GetString(1);
musicList.Add(music);
}
}
finally
{
reader.Close();
connection.Close();
}
}
}
return musicList.ToArray();
}
public static int SaveMusic(Music music)
{
if (music.Id == 0)
{
// database stuff - getting the newly created database id
}
else
{
// database calls to update record
}
return music.Id;
}
public static int SaveFilm(Film film)
{
if (film.Id == 0)
{
// database stuff - getting the newly created database id
}
else
{
// database calls to update record
}
return film.Id;
}
public static Music GetMusic(int id)
{
var music = new Music();
// database call and setting of values on music
return music;
}
public static Film GetFilm(int id)
{
var film = new Film();
// database call and setting of values on music
return film;
}
}
}
现在终于创建的根文件名为WorkHarness.cs
using System;
using YourApplication.YourProject.Persistence;
namespace YourApplication.YourProject
{
public class WorkHarness
{
public void Initialize()
{
DatabaseActions.dbFile = "your db file";
}
public void ShowMusicList()
{
// list the id and title so user can select by Id
foreach (var music in DatabaseActions.ListMusic())
{
Console.WriteLine("{0,-10}{1}",music.Id,music.Title);
}
}
public void DisplayMusicItem(int id)
{
var music = DatabaseActions.GetMusic(id);
Console.WriteLine("Title: " + music.Title);
Console.WriteLine("Length: " + music.Length);
Console.WriteLine("Artist: " + music.Artist);
Console.WriteLine("Album: " + music.Album);
}
}
}
为了得到你想要的语法,subclass1其方法需要是静态的,但是,我不确定,如果你能够访问class1的属性,方法等,而无需将它们作为参数传递给subclass1的方法。 – AntiTcb
感谢所有帮助和建议。最后,我创建了一个名为db_manage的新命名空间,并且在该命名空间中我使用她的方法创建了类。现在就像我想要的:db_manage.class.method(); –