主题:这段代码不能执行我需要的修改和删除!
//这里作了一个LinkDataBase的类
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace StockSys
{
public class LinkDataBase
{
private string strSQL;
//与SQL Server的连接字符串设置
private string connectionString = "Data Source=MYBULLET\\MYBULLET;Initial Catalog=Stockbook;Integrated Security=True";//与数据库的连接
private SqlConnection myConnection;
private SqlCommandBuilder sqlCmBld;
private DataSet ds = new DataSet();
private SqlDataAdapter da;
public LinkDataBase()
{
}
public DataSet SelectDataBase(string tempStrSQL, string tempTableName)
{
this.strSQL = tempStrSQL;
this.myConnection = new SqlConnection(connectionString);
this.da = new SqlDataAdapter(this.strSQL, this.myConnection);
this.ds.Clear();
this.da.Fill(ds, tempTableName);
return ds;//返回填充了数据的DataSet,其中数据表以tempTableName给出的字符串命名
}
//数据库数据更新(传DataSet和DataTable的对象)
public DataSet UpdateDataBase(DataSet changedDataSet, string tableName)
{
this.myConnection = new SqlConnection(connectionString);
this.da = new SqlDataAdapter(this.strSQL, this.myConnection);
this.sqlCmBld = new SqlCommandBuilder(da);
this.da.Update(changedDataSet, tableName);
return changedDataSet;//返回更新了的数据库表
}
///////////////////////////////// 直接操作数据库(未创建该类的实例时直接用) /////////////////////////////////////////////////////
//检索数据库数据(传字符串,直接操作数据库)
public DataTable SelectDataBase(string tempStrSQL)
{
this.myConnection = new SqlConnection(connectionString);
DataSet tempDataSet = new DataSet();
this.da = new SqlDataAdapter(tempStrSQL, this.myConnection);
this.da.Fill(tempDataSet);
return tempDataSet.Tables[0];
}
//数据库数据更新(传字符串,直接操作数据库)
public int UpdateDataBase(string tempStrSQL)
{
this.myConnection = new SqlConnection(connectionString);
//使用Command之前一定要先打开连接,后关闭连接,而DataAdapter则会自动打开关闭连接
myConnection.Open();
SqlCommand tempSqlCommand = new SqlCommand(tempStrSQL, this.myConnection);
int intNumber = tempSqlCommand.ExecuteNonQuery();//返回数据库中影响的行数
myConnection.Close();
return intNumber;
}
}
}
//一下是另一个窗体代码!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.SqlClient;
namespace StockSys
{
public partial class departmentSetFrm : Form
{
private DataSet ds = new DataSet();
private LinkDataBase link = new LinkDataBase();
private string sendTableName = "部门清单";
private string sendStrSQL = "SELECT 部门编号,部门名称 FROM 部门清单";
private SqlDataAdapter da;
public departmentSetFrm()
{
InitializeComponent();
this.ds = link.SelectDataBase(sendStrSQL, sendTableName);
this.dgv_Department.DataSource = ds.Tables[0];
}
private void departmentSetFrm_Load(object sender, EventArgs e)
{
}
private void but_Cancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void but_Save_Click(object sender, EventArgs e)//修改的数据不能返回!还是修改前的数据!
{
try
{
int row = this.dgv_Department.CurrentCell.RowNumber;
this.dgv_Department.CurrentCell = new DataGridCell(row + 1, 0);
if (this.ds.HasChanges())
{
this.link.UpdateDataBase(this.ds.GetChanges(), sendTableName);
MessageBox.Show("数据修改成功!", "信息");
}
else
{
MessageBox.Show("没有需要修改的数据!");
return;
}
}
catch
{
MessageBox.Show("数据保存失败,请重新确认输入!", "提示");
return;
}
}
private void but_Delete_Click(object sender, EventArgs e)//在这里执行删除不能执行!
{
if (MessageBox.Show("确实要删除这条记录吗?", "询问", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
int intRowNumber = this.dgv_Department.CurrentCell.RowNumber;
try
{
this.ds.Tables[0].Rows[intRowNumber].Delete();
this.link.UpdateDataBase(this.ds.GetChanges(), sendTableName);
MessageBox.Show("数据删除成功!", "信息");
}
catch
{
MessageBox.Show("该数据不能删除!", "提示");
string tempStrSQL = "select * from " + sendTableName;
this.link.SelectDataBase(tempStrSQL, sendTableName);
return;
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace StockSys
{
public class LinkDataBase
{
private string strSQL;
//与SQL Server的连接字符串设置
private string connectionString = "Data Source=MYBULLET\\MYBULLET;Initial Catalog=Stockbook;Integrated Security=True";//与数据库的连接
private SqlConnection myConnection;
private SqlCommandBuilder sqlCmBld;
private DataSet ds = new DataSet();
private SqlDataAdapter da;
public LinkDataBase()
{
}
public DataSet SelectDataBase(string tempStrSQL, string tempTableName)
{
this.strSQL = tempStrSQL;
this.myConnection = new SqlConnection(connectionString);
this.da = new SqlDataAdapter(this.strSQL, this.myConnection);
this.ds.Clear();
this.da.Fill(ds, tempTableName);
return ds;//返回填充了数据的DataSet,其中数据表以tempTableName给出的字符串命名
}
//数据库数据更新(传DataSet和DataTable的对象)
public DataSet UpdateDataBase(DataSet changedDataSet, string tableName)
{
this.myConnection = new SqlConnection(connectionString);
this.da = new SqlDataAdapter(this.strSQL, this.myConnection);
this.sqlCmBld = new SqlCommandBuilder(da);
this.da.Update(changedDataSet, tableName);
return changedDataSet;//返回更新了的数据库表
}
///////////////////////////////// 直接操作数据库(未创建该类的实例时直接用) /////////////////////////////////////////////////////
//检索数据库数据(传字符串,直接操作数据库)
public DataTable SelectDataBase(string tempStrSQL)
{
this.myConnection = new SqlConnection(connectionString);
DataSet tempDataSet = new DataSet();
this.da = new SqlDataAdapter(tempStrSQL, this.myConnection);
this.da.Fill(tempDataSet);
return tempDataSet.Tables[0];
}
//数据库数据更新(传字符串,直接操作数据库)
public int UpdateDataBase(string tempStrSQL)
{
this.myConnection = new SqlConnection(connectionString);
//使用Command之前一定要先打开连接,后关闭连接,而DataAdapter则会自动打开关闭连接
myConnection.Open();
SqlCommand tempSqlCommand = new SqlCommand(tempStrSQL, this.myConnection);
int intNumber = tempSqlCommand.ExecuteNonQuery();//返回数据库中影响的行数
myConnection.Close();
return intNumber;
}
}
}
//一下是另一个窗体代码!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.SqlClient;
namespace StockSys
{
public partial class departmentSetFrm : Form
{
private DataSet ds = new DataSet();
private LinkDataBase link = new LinkDataBase();
private string sendTableName = "部门清单";
private string sendStrSQL = "SELECT 部门编号,部门名称 FROM 部门清单";
private SqlDataAdapter da;
public departmentSetFrm()
{
InitializeComponent();
this.ds = link.SelectDataBase(sendStrSQL, sendTableName);
this.dgv_Department.DataSource = ds.Tables[0];
}
private void departmentSetFrm_Load(object sender, EventArgs e)
{
}
private void but_Cancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void but_Save_Click(object sender, EventArgs e)//修改的数据不能返回!还是修改前的数据!
{
try
{
int row = this.dgv_Department.CurrentCell.RowNumber;
this.dgv_Department.CurrentCell = new DataGridCell(row + 1, 0);
if (this.ds.HasChanges())
{
this.link.UpdateDataBase(this.ds.GetChanges(), sendTableName);
MessageBox.Show("数据修改成功!", "信息");
}
else
{
MessageBox.Show("没有需要修改的数据!");
return;
}
}
catch
{
MessageBox.Show("数据保存失败,请重新确认输入!", "提示");
return;
}
}
private void but_Delete_Click(object sender, EventArgs e)//在这里执行删除不能执行!
{
if (MessageBox.Show("确实要删除这条记录吗?", "询问", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
int intRowNumber = this.dgv_Department.CurrentCell.RowNumber;
try
{
this.ds.Tables[0].Rows[intRowNumber].Delete();
this.link.UpdateDataBase(this.ds.GetChanges(), sendTableName);
MessageBox.Show("数据删除成功!", "信息");
}
catch
{
MessageBox.Show("该数据不能删除!", "提示");
string tempStrSQL = "select * from " + sendTableName;
this.link.SelectDataBase(tempStrSQL, sendTableName);
return;
}
}
}
}
}