C#数据库问题
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Lab6Q5
{
public partial class Form1 : Form
{
OleDbConnection mDB = new OleDbConnection();
public Form1()
{
InitializeComponent();
}
private void UpdateDataGrid(string sqlStr)
{
OleDbConnection connStr = new OleDbConnection(
@"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:\adpDB\Library2.accdb");
DataTable dt = new DataTable();
OleDbDataAdapter da =
new OleDbDataAdapter(sqlStr, connStr);
da.Dispose();
}
private void btnLoan_Click(object sender, EventArgs e)
{
int loanNumber = int.Parse(txtloanNo.Text);
int patronNumber = int.Parse(txtpatronNo.Text);
string ISBN = txtISBN.Text;
string sqlLoan;
OleDbCommand cmd;
sqlLoan = "INSERT INTO Loan ("
+ "LoanNo, "
+ "LoanDate, "
+ "PatronNo, "
+ ") VALUES ("
+ toSql(txtloanNo.Text) + ", "
+ toSql(txtpatronNo.Text) + ", "
+ toSql(dateTimePicker1.Value.ToShortDateString())
+ ")";
try
{
cmd = new OleDbCommand(sqlLoan, mDB);
cmd.ExecuteNonQuery();
MessageBox.Show("It is inserted successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
UpdateDataGrid(sqlLoan);
string sqlLoanBook;
sqlLoanBook = "INSERT INTO LoanBook ("
+ "LoanNo, "
+ "ISBN, "
+ "DueDate, "
+ ") VALUES ("
+ toSql(txtloanNo.Text) + ", "
+ toSql(txtISBN.Text) + ", "
+ toSql(dateTimePicker1.Value.ToShortDateString())
+ ")";
try
{
cmd = new OleDbCommand(sqlLoanBook, mDB);
cmd.ExecuteNonQuery();
MessageBox.Show("It is inserted successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
UpdateDataGrid(sqlLoanBook);
string sqlBook;
sqlBook = "UPDATE BOOK SET "
+ "ISBN = " + toSql(txtISBN.Text) + ", "
+ "Title = " + ", "
+ "Author = " + ", "
+ "Publisher = " + ", "
+ "Subject_Code = " + ", "
+ "Shelf_Location = " + ", "
+ "Fiction = " + ", "
+ "Price = " + ", "
+ "Bstatus = " + ", ";
UpdateDataGrid(sqlBook);
try {
cmd = new OleDbCommand(sqlBook, mDB);
cmd.ExecuteNonQuery();
MessageBox.Show("It is inserted successfully");
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
private string toSql(bool boolValue)
{
return boolValue.ToString();
}
private string toSql(DateTime dateTimeValue)
{
return "#" + dateTimeValue.ToString("mm-dd-yyyy") + "#";
//return "'" + dateTimeValue.ToString("mm-dd-yyyy") + "'";
}
private string toSql(decimal decimalValue)
{
return decimalValue.ToString();
}
private string toSql(double doubleValue)
{
return doubleValue.ToString();
}
private string toSql(int intValue)
{
return intValue.ToString();
}
private string toSql(string stringValue)
{
return "'" + stringValue.Replace("'", "''") + "'";
}
}
}
這個是我寫的程序
是用作图书管的借书程序。一位顾客在这种情况下,只能借出一本书。图书管理员输入书本编号,顾客编号及书本的ISBN。当图书管理员点击按钮时,借书细节将被保存到数据库中的表格及书本的记录将被更新。
但當我按了LOAN按鍵後
為何會出現這個消息框
資料未能被更新
求大大們幫助啊!!!! --------------------编程问答-------------------- 连接关闭了,而你还要访问数据库,调试代码,保持连接正常 --------------------编程问答-------------------- connection.Open();
//code
connection.Close(); --------------------编程问答--------------------
請問這句該放在那裏? --------------------编程问答-------------------- 连接都没有打开,怎更新数据库? --------------------编程问答-------------------- 消息显示的很明确,在执行非查询操作时,当然要保持你程序和数据库的通信啊,你关掉了怎么和数据库信息交互啊.
你可以在执行非查询之后关掉和释放和数据库的连接嘛. --------------------编程问答--------------------
意思就是,在进行增删该查之前要打开数据库,操作完了也要记着关闭。
看你的代码要在 每一句cmd.ExecuteNonQuery();之前加上sqlLoan.Open();之后加上sqlLoan.Close(); --------------------编程问答-------------------- 如果你不确定放在那里你就直接在定义sqlconnection之后就open,然后在程序的最后在close
补充:.NET技术 , C#