我有一个显示3行的DataGridView
,当我尝试更新记录时,它说成功更改了记录。但是当我检查数据库时,记录没有更新。
在删除按钮中,它说我必须选择要删除的记录,但我已在DataGridView
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.Sql;
using System.Data.SqlClient;
namespace RFG_Inventory_and_Management_System
{
public partial class AdminForm : Form
{
SqlCommand cmd;
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Acer\Documents\Visual Studio 2010\Projects\RFG Inventory and Management System\RFG Inventory and Management System\Database1.mdf;Integrated Security=True;User Instance=True");
SqlCommandBuilder cmdbl;
SqlDataAdapter adap;
DataSet ds;
int ID = 0;
public AdminForm()
{
InitializeComponent();
PayratesDisplay();
NonMemberDisplay();
}
//Display Data in DataGridView
private void PayratesDisplay()
{
con.Open();
DataTable dt = new DataTable();
adap = new SqlDataAdapter("select * from tbl_payrates", con);
adap.Fill(dt);
dataGridView4.DataSource = dt;
con.Close();
}
private void NonMemberDisplay()
{
con.Open();
DataTable dt = new DataTable();
adap = new SqlDataAdapter("SELECT tbl_nonMember.*, tbl_customer.lname, tbl_customer.fname, tbl_customer.mname, tbl_customer.gender, tbl_customer.age, tbl_customer.membership_type FROM tbl_nonMember INNER JOIN tbl_customer ON tbl_nonMember.customerID = tbl_customer.customerID", con);
adap.Fill(dt);
dataGridView2.DataSource = dt;
con.Close();
}
//Clear Data
private void ClearData()
{
tbMemship.Text = "";
tbPerses.Text = "";
tbDisc.Text = "";
ID = 0;
}
private void AdminForm_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'database1DataSet.tbl_customer' table. You can move, or remove it, as needed.
this.tbl_customerTableAdapter.Fill(this.database1DataSet.tbl_customer);
// TODO: This line of code loads data into the 'database1DataSet.tbl_nonMember' table. You can move, or remove it, as needed.
this.tbl_nonMemberTableAdapter.Fill(this.database1DataSet.tbl_nonMember);
}
private void fillByToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.tbl_customerTableAdapter.FillBy(this.database1DataSet.tbl_customer);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
//----------------------------------BUTTONS FOR PAY RATE-------------------------------------------------------------//
//dataGridView1 RowHeaderMouseClick Event
private void dataGridView4_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
tbMemship.Text = dataGridView4.Rows[e.RowIndex].Cells[0].Value.ToString();
tbPerses.Text = dataGridView4.Rows[e.RowIndex].Cells[1].Value.ToString();
tbDisc.Text = dataGridView4.Rows[e.RowIndex].Cells[2].Value.ToString();
}
private void btn_update_Click(object sender, EventArgs e)
{
if (tbMemship.Text != "" && tbPerses.Text != "" && tbDisc.Text != "")
{
cmd = new SqlCommand("update tbl_payrates set Membership=@Membership,PerSession=@PerSession where Discounted=@Discounted", con);
con.Open();
cmd.Parameters.AddWithValue("@Membership", tbMemship.Text);
cmd.Parameters.AddWithValue("@PerSession", tbPerses.Text);
cmd.Parameters.AddWithValue("@Discounted", tbDisc.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Record Updated Successfully");
con.Close();
PayratesDisplay();
ClearData();
}
else
{
MessageBox.Show("Please Select Record to Update");
}
}
private void btn_PRadd_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("Are you sure to save Changes", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dr == DialogResult.Yes)
{
con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Acer\Documents\Visual Studio 2010\Projects\RFG Inventory and Management System\RFG Inventory and Management System\Database1.mdf;Integrated Security=True;User Instance=True");
con.Open();
cmd = new SqlCommand("INSERT INTO tbl_payrates (Membership, PerSession, Discounted) VALUES (@Membership, @PerSession, @Discounted)", con);
cmd.Parameters.AddWithValue("@Membership", tbMemship.Text);
cmd.Parameters.AddWithValue("@PerSession", tbPerses.Text);
cmd.Parameters.AddWithValue("@Discounted", tbDisc.Text);
cmd.ExecuteNonQuery();
}
}
private void btn_PRdel_Click_1(object sender, EventArgs e)
{
//Update button update dataset after insertion,upadtion or deletion
DialogResult dr = MessageBox.Show("Are you sure to save Changes", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dr == DialogResult.Yes)
{
if (ID != 0)
{
cmd = new SqlCommand("DELETE FROM tbl_payrates WHERE ID=@id", con);
con.Open();
cmd.Parameters.AddWithValue("@id", ID);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Deleted Successfully!");
PayratesDisplay();
ClearData();
}
else
{
MessageBox.Show("Please Select Record to Delete");
}
}
}
}
}
答案 0 :(得分:1)
整个 AttachDbFileName = 方法存在缺陷 - 充其量!在Visual Studio中运行应用程序时,它将复制.mdf
文件(从App_Data
目录到输出目录 - 通常是.\bin\debug
- 应用程序运行的地方)和最有可能,您的INSERT
工作正常 - 但您最后只是查看错误的.mdf文件!
如果你想坚持这种方法,那么尝试在myConnection.Close()
调用上设置一个断点 - 然后用SQL Server Mgmt Studio Express检查.mdf
文件 - 我几乎可以肯定你的数据在那里。
我认为真正的解决方案将是
安装SQL Server Express(你已经完成了)
安装SQL Server Management Studio Express
在 SSMS Express 中创建数据库,为其指定一个逻辑名称(例如InventoryDb
)
使用其逻辑数据库名称(在服务器上创建时给定)连接到它 - 并且不要乱用物理数据库文件和用户实例。在这种情况下,您的连接字符串将类似于:
Data Source=.\\SQLEXPRESS;Database=InventoryDb;Integrated Security=True
其他所有内容都完全与以前相同......
另请参阅Aaron Bertrand的优秀博客文章Bad habits to kick: using AttachDbFileName以获取更多背景信息。
答案 1 :(得分:0)
我认为你没有为id分配值。所以id始终为0,你无法删除记录。尝试将值分配给ID并删除。我希望它能正常运行。
private void btn_PRdel_Click_1(object sender, EventArgs e)
{
//Update button update dataset after insertion,upadtion or deletion
DialogResult dr = MessageBox.Show("Are you sure to save Changes", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dr == DialogResult.Yes)
{
if (ID != 0)// where is the id value?
{
cmd = new SqlCommand("DELETE FROM tbl_payrates WHERE ID=@id", con);
con.Open();
cmd.Parameters.AddWithValue("@id", ID);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Deleted Successfully!");
PayratesDisplay();
ClearData();
}
else
{
MessageBox.Show("Please Select Record to Delete");
}
}
}