我有一个正在使用的系统/应用程序的本地数据库。 在此数据库中,我具有“书籍和类别”列-CategoryID也作为“外键”成为“书籍”表的一部分,并且我具有10种类别的书籍记录,例如“漫画”(ID:1),“工艺”(ID: 2),“教育”(ID:3)等
我当前正在应用程序中实现“添加书记录”功能,因此在保存时,会在数据库中插入具有给定详细信息的新书记录,但是,对于CategoryID(在“书”表中),我只能输入我不想要的整数(如下所述)
(顺便说一句,抱歉,我还是编码新手)
我看过一些有关枚举的教程,但是我似乎找不到有关如何将枚举与组合框结合在一起的任何信息。
我想要的是让组合框显示如下内容:
1-漫画, 2-手工艺品 3-教育, 等
上面的组合框下拉列表将指向(例如)类别ID:1的“ 1-漫画”,类别ID:2的“ 2-手工艺品”,依此类推。
这是我目前在“添加新记录”按钮之外的内容
public partial class AddBookRecordForm : Form
{
public AddBookRecordForm()
{
InitializeComponent();
}
enum BookCategory
{
Comics = 1,
Crafts = 2,
Education = 3,
History = 4,
Entertainment = 5,
Thriller = 6,
Religion = 7,
Romance = 8,
Fantasy = 9,
Sports = 10
}
不确定现在如何进行-我假设下一部分代码需要在“保存按钮代码”中实现。
作为参考,以下是我在“保存按钮”方法中进行的数据库连接。
string ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=
C:\Program Files\Microsoft SQL
Server\MSSQL14.SQLEXPRESS\MSSQL\DATA\Library System Project.mdf
;Integrated Security=True;Connect Timeout=30";
string Query = "insert into Books (BookName, BookAuthor, CategoryID, ClassificationID, BookAvailabilityQuantity, Price) values ('" + this.txtName.Text.Trim() + "','" + this.txtAuthor.Text.Trim() + "','" + this.txtCategory.Text.Trim() + "','" + this.txtClassification.Text.Trim() + "','" + this.txtAvailabilityQuantity.Text.Trim() + "','" + this.txtPrice.Text.Trim() + "');";
SqlConnection DBCon = new SqlConnection(ConnectionString);
SqlCommand DBCommand = new SqlCommand(Query, DBCon);
SqlDataReader DBReader;
try
{
DBCon.Open();
DBReader = DBCommand.ExecuteReader();
MessageBox.Show("New book record added to the system.", "Library System", MessageBoxButtons.OK);
while (DBReader.Read())
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// *** If you're going to be opening a connection be sure to close it ***
// *** Finally blocks work well for this ***
DBCon.Close();
this.txtName.ResetText();
this.txtAuthor.ResetText();
this.txtCategory.ResetText();
this.txtClassification.ResetText();
this.txtAvailabilityQuantity.ResetText();
this.txtPrice.ResetText();
}
}
答案 0 :(得分:0)
为简单起见,我建议将此作为我的快速解决方案。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
enum BookCategory
{
Comics = 1,
Crafts = 2,
Education = 3,
History = 4,
Entertainment = 5,
Thriller = 6,
Religion = 7,
Romance = 8,
Fantasy = 9,
Sports = 10
}
private void Form1_Load(object sender, EventArgs e)
{
var values = Enum.GetValues(typeof(BookCategory));
var dataSource = new List<BookCategoryData>();
foreach (var bookCatergory in values)
{
dataSource.Add(new BookCategoryData() { Id = (int)bookCatergory, Name = bookCatergory.ToString() });
}
comboBox1.ValueMember = "Id";
comboBox1.DisplayMember = "Name";
comboBox1.DataSource = dataSource;
}
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
public class BookCategoryData
{
public int Id { get; set; }
public string Name { get; set; }
}
也请注意此处的其他成员,请查看 SQL注入,并使用Sql Parameters来使用串联文本。您的代码如下所示:
string query = "insert into Books (BookName, BookAuthor, CategoryID, ClassificationID, BookAvailabilityQuantity, Price) " +
"values (@BookName, @BookAuthor, @CategoryID, @ClassificationID, @BookAvailabilityQuantity, @Price)";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.Add("@BookName", this.txtName.Text.Trim());