[英]C# - Implementing Enum / Combobox
I have a local database for a system / application I'm doing. 我有一个我正在做的系统/应用程序的本地数据库。 Within this DB I have Books & Category Columns - CategoryID also makes part of the Books table as a Foreign Key, and I have 10 Category Records, for book genre, like 'Comics' (ID: 1), 'Crafts' (ID: 2), 'Education' (ID: 3), etc.
在这个DB中我有书籍和类别列 - CategoryID也将Books表的一部分作为外键,我有10个类别记录,用于书籍类型,如'漫画'(ID:1),'Crafts'(ID: 2),'教育'(ID:3)等
I am currently implementing an Add Book Record function in the application, so that when I save, a new book record is inserted in my DB with the details given, however, for CategoryID (in Books table), I can only input integers, which I don't want (as explained below) 我目前正在应用程序中实现一个Add Book Record功能,这样当我保存时,我的数据库中会插入一个新的书记录,但是对于CategoryID(在Books表中),我只能输入整数,我不想要(如下所述)
(BTW, apologies but I'm still a coding newbie) (顺便说一句,道歉,但我仍然是编码新手)
I have seen some tutorials on Enums, but I can't seem to find anything on how to combine an Enum with a combobox. 我已经看过一些关于Enums的教程,但我似乎无法找到关于如何将Enum与组合框结合起来的任何内容。
What I want is to have the combobox show something like: 我想要的是让组合框显示如下:
1 - Comics, 2 - Crafts, 3 - Education, etc. 1 - 漫画,2 - 工艺品,3 - 教育等
The above combobox dropdown list would point out to (for example) '1 - Comics' for CategoryID: 1, '2 - Crafts' for CategoryID: 2, and so on. 上面的组合框下拉列表会指出(例如)类别ID为1的“1 - 漫画”:1,类别ID为2的“2 - 工艺”,依此类推。
This is what I currently have outside of the Add New Record button 这是我目前在添加新记录按钮之外的内容
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
}
Not sure how to proceed now - I'm assuming that the next part of code would need to be implemented within the Save Button Code. 不知道如何继续 - 我假设下一部分代码需要在保存按钮代码中实现。
For Reference, the below is the DB connection I did within the Save Button method. 对于Reference,下面是我在Save Button方法中执行的DB连接。
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();
}
}
For simplicity I would suggest this as my quick soluction. 为简单起见,我建议将其作为我的快速解决方案。
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; }
}
Also as metioned here for other members please take a look on SQL Injection and use Sql Parameters in order to use concatenated text. 另外,对于其他成员,请参阅SQL注入并使用Sql参数以使用连接文本。 Your code will looks like something below:
您的代码如下所示:
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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.