[英]C# Database Insert not working from textbox to listbox
I've made a library management system. 我已经建立了图书馆管理系统。 Put 4 boxes containing the title, author, no of pages and publisher.
放置4个框,其中包含标题,作者,页数和发布者。 And then add a button to add it on the database, and I also have 3 listboxes containing the title, author and publisher.
然后添加一个按钮以将其添加到数据库中,我还有3个列表框,其中包含标题,作者和发布者。 It seems that when I accomplished filling up the 4 textboxes and then clicking the button for insert, it's not updating the listboxes and even the database.
看来当我完成4个文本框的填充并单击“插入”按钮时,它并没有更新列表框甚至数据库。 Nothing is happening.
没事 How do you do that?
你是怎样做的?
OleDbCommand cmd = new OleDbCommand();
OleDbConnection cn = new OleDbConnection();
OleDbDataReader dr;
public frm1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database2.accdb;Persist Security Info=True";
cmd.Connection = cn;
loaddata();
}
private void loaddata()
{
lstbxTitle.Items.Clear();
lstbxAuthor.Items.Clear();
lstbxPub.Items.Clear();
try
{
string q = "SELECT * FROM Table2";
cmd.CommandText = q;
cn.Open();
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
lstbxTitle.Items.Add(dr[1].ToString());
lstbxAuthor.Items.Add(dr[2].ToString());
lstbxPub.Items.Add(dr[6].ToString());
}
}
dr.Close();
cn.Close();
}
catch (Exception e)
{
cn.Close();
MessageBox.Show(e.Message.ToString());
}
}
private void lstbxTitle_Click(object sender, EventArgs e)
{
ListBox l = sender as ListBox;
try
{
if (l.SelectedIndex != 1)
{
lstbxTitle.SelectedIndex = l.SelectedIndex;
lstbxAuthor.SelectedIndex = l.SelectedIndex;
lstbxAuthor.Text = lstbxAuthor.SelectedItem.ToString();
lstbxPub.SelectedIndex = l.SelectedIndex;
lstbxPub.Text = lstbxPub.SelectedItem.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (txtbxAddT.Text != "")
{
string q = "insert into Table2 (Title) values ('" + txtbxAddT.Text.ToString() + "')";
txtbxAddT.Text = null;
}
if (txtbxAddA.Text != "")
{
string q = "insert into Table2 (Author) values ('" + txtbxAddA.Text.ToString() + "')";
txtbxAddA.Text = null;
}
if (txtbxAddNP.Text != "")
{
string q = "insert into Table2 (Page Number) values ('" + txtbxAddNP.Text.ToString() + "')";
txtbxAddNP.Text = null;
}
if (txtbxAddP.Text != "")
{
string q = "insert into Table2 (Publisher) values ('" + txtbxAddP.Text.ToString() + "')";
txtbxAddP.Text = null;
}
loaddata();
}
The problem is you are not executing your query
. 问题是您没有执行
query
。 It is in the string q
, but you are not executing it anywhere. 它在
string q
,但是您没有在任何地方执行它。
Create a method: 创建一个方法:
private void UpdateData(query)
{
try
{
cn.Open();
cmd.CommandText = query; //set query to execute
cmd.ExecuteNonQuery(); //executing the query
cn.Close();
}
catch (Exception ex)
{
}
}
Call this method in every if-statement
of method btnAdd_Click
: 在方法
btnAdd_Click
每个if-statement
中调用此方法:
if (txtbxAddT.Text != "")
{
string q = "insert into Table2 (Title) values ('" + txtbxAddT.Text.ToString() + "')";
UpdateData(q);
txtbxAddT.Text = String.Empty; //Set it to "Empty", Null is not Empty
}
//... and so on for every if check
Best practice advice: 最佳做法建议:
Assigning textbox.Text
to ""
or null
is not a good practice. 将
textbox.Text
分配为""
或null
不是一个好习惯。
Use String.Empty
instead. 改用
String.Empty
。
While checking for textbox.Text
for being Empty
, never use textbox.Text == null
or textbox.Text == ""
, these are also not the best practices and some times create problems. 在检查
textbox.Text
是否为Empty
,切勿使用textbox.Text == null
或textbox.Text == ""
,这些也不是最佳实践,有时会产生问题。
Use String.IsNullOrEmpty(textbox.Text)
instead. 请改用
String.IsNullOrEmpty(textbox.Text)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.