我正在开发一个库数据库,您可以在其中添加和删除数据库中的书籍。
按钮1问题 当我按下按钮1(向数据库添加记录并在数据网格中重新加载数据库)时,它会在第一次点击button1时工作,当我更改文本框中的文本(标题,作者,股票)以添加到数据库并再次单击按钮时,它不会改变添加到数据库的内容。 例 textbox1.text(title)="哈利波特" textbox2.text(作者)=" JK" textbox3.text(股票)=" 3" 按下按钮1(添加)。第一次单击button1时工作正常。当我更改textbox1-3的值时 例 textbox1.text(title)="炼金术士" textbox2.text(author)=" paollo cuello" textbox3.text(股票)=" 5" 它仍记录着" Harry Potter"," jk"," 3"。
Button2问题 当我按下按钮2(删除数据库中的记录并在数据网格中重新加载数据库)时,它在第一次点击时起作用,但在数据网格上并没有真正显示。 例。 datagrid显示值 标题作者股票 "哈利·波特" " JK" 3和 "炼金" " PC" 4
textbox1.text(title)=" harrypotter" textbox2.text(作者)=" JK" textbox3.text(股票)=" 3" 按下button2(删除并重新加载datagrid)。 result = datagrid不会加载新记录,但哈里波特记录已在数据库中删除 当我尝试删除另一条记录时。 例。 标题作者股票 harrypotter jk 3 alchemist pc 4
注意" harrypotter"还在那里,因为重装没有用。 textbox1.text(title)=" alchemst" textbox2.text(作者)=" PC" textbox3.text(股票)=" 4" 按下button2(删除并重新加载datagrid)。 它不起作用。我打赌它试图删除" harrypotter记录仍然"
为什么?
public partial class AddBook : Form
{
String title = "", author = "";
bool hasValue1 = false, hasValue2 = false, hasValue3 = false;
string holder = "";
int stock = 0;
OleDbConnection connect = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Jc\Documents\Visual Studio 2013\Projects\itswn\Library System\Library System\LibrarySystemDatabase.accdb;Persist Security Info=True");
OleDbCommand command = new OleDbCommand();
OleDbDataReader reader;
public AddBook()
{
InitializeComponent();
}
private void AddBook_Load(object sender, EventArgs e)
{
dataGridView1.Columns.Add("Title", "Title");
dataGridView1.Columns.Add("Author", "Author");
dataGridView1.Columns.Add("Stock", "Stock");
connect.Open();
loaddataBook();
}
private void loaddataBook()
{
int i = 0;
try
{
command.CommandText = "SELECT Title, Author, Stock FROM Book";
command.Connection = connect;
reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells["Title"].Value = reader[0].ToString();
dataGridView1.Rows[i].Cells["Author"].Value = reader[1].ToString();
dataGridView1.Rows[i].Cells["Stock"].Value = reader[2].ToString();
i++;
}
}
reader.Close();
connect.Close();
}
catch (OleDbException ex)
{
connect.Close();
MessageBox.Show(ex.ToString());
}
connect.Close();
}
private void button1_Click(object sender, EventArgs e)
{
title = textBox1.Text.ToLower();
author = textBox2.Text.ToLower();
stock = int.Parse(textBox3.Text);
if (textBox1.Text != "")
{
hasValue1 = true;
}
if (textBox2.Text != "")
{
hasValue2 = true;
}
if (textBox3.Text != "")
{
hasValue3 = true;
}
if (int.Parse(textBox3.Text) >= 0 || textBox3.Text == "")
{
if (hasValue1 && hasValue2 && hasValue3)
{
try
{
connect.Open();
command.Connection = connect;
command.Parameters.AddWithValue("@title", title);
command.Parameters.AddWithValue("@author", author);
command.Parameters.AddWithValue("@stock", stock);
command.CommandText = "INSERT INTO [Book](Title, Author, Stock) VALUES(@title, @author, @stock)";
command.ExecuteNonQuery();
loaddataBook();
connect.Close();
}
catch (OleDbException ex)
{
connect.Close();
MessageBox.Show(ex.ToString());
}
connect.Close();
}
else
{
label4.Text = "required";
label5.Text = "required";
label6.Text = "required";
}
}
else
{
MessageBox.Show("Stock should not be less than 0", "Keep in mind");
}
}
private void button2_Click(object sender, EventArgs e)
{
title = textBox1.Text.ToLower();
author = textBox2.Text.ToLower();
textBox3.Clear();
if (textBox1.Text != "")
{
hasValue1 = true;
}
if (textBox2.Text != "")
{
hasValue2 = true;
}
if (hasValue1 && hasValue2)
{
try
{
connect.Open();
command.Connection = connect;
command.Parameters.AddWithValue("@title", title);
command.Parameters.AddWithValue("@author", author);
command.CommandText = "DELETE FROM [Book] WHERE Title = @title AND Author = @author";
command.ExecuteNonQuery();
loaddataBook();
connect.Close();
}
catch (OleDbException ex)
{
connect.Close();
MessageBox.Show(ex.ToString());
}
}
else
{
label4.Text = "required";
label5.Text = "required";
}
}
}
答案 0 :(得分:0)
你应该使用sql not insert函数的更新功能,例如
update [tablename]
set [Book] = [valueof book] , [author ] = [valueofauthor] , so on~~
where statement you want to update~~~~
title = harrypotter的例子。
这是针对button1。