sqlhelper.cs,如何用它实现Datagridview的添删改查功能????????? 用的是微软已经给封装好的sqlhelper.cs !!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace 医院管理系统代码{ public partial class EmployeeSalariesManage : Form { SqlConnection conn; SqlDataAdapter sda; SqlCommandBuilder builder; DataSet ds; DataTable dt; DataRow row; float shouldSalaries = 0; float rate = 0; float prize = 0; string connection = "Data Source=.;Initial Catalog=LifeLine;Integrated Security=True"; public EmployeeSalariesManage() { InitializeComponent(); }//添加数据 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { txtEmployeeID.Text = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString(); txtEmployeType.Text = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString(); txtShouldSalaries.Text = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString(); txtRate.Text = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString(); txtPrize.Text = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString(); txtFactSalaries.Text = dataGridView1[5, dataGridView1.CurrentCell.RowIndex].Value.ToString(); } private void Add_Click(object sender, EventArgs e) { try { conn = new SqlConnection(connection); sda = new SqlDataAdapter("select * from EmployeeSalaries", conn); SqlCommandBuilder builder1 = new SqlCommandBuilder(sda); DataTable table = new DataTable(); sda.Fill(table); DataRow DR = table.NewRow(); DR[0] = txtEmployeeID.Text; table.Rows.Add(DR); sda.Update(table); dataGridView1.DataSource = table; MessageBox.Show("数据添加成功!"); } catch (Exception ee) { MessageBox.Show(ee.Message); } }//数据查询 private void Query_Click(object sender, EventArgs e) { try { string command = ""; if (comboBoxSelectCondition.Text == "") { MessageBox.Show("查询条件不能为空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { if (comboBoxSelectCondition.Text == "员工代号") { command = "select * from dbo.EmployeeSalaries where 代号='" + txtEmployeeID.Text + "'"; } if (txtEmployeeID.Text.Length == 0 && txtEmployeType.Text.Length == 0 && txtShouldSalaries.Text.Length == 0 && txtRate.Text.Length == 0 && txtPrize.Text.Length == 0 && txtFactSalaries.Text.Length==0) { MessageBox.Show("输入的内容不能为空!"); } else { conn = new SqlConnection(connection); sda = new SqlDataAdapter(command, conn); builder = new SqlCommandBuilder(sda); ds = new DataSet(); dt = new DataTable(); sda.Fill(dt); if (int.Parse(dt.Rows.Count.ToString()) == 0) { MessageBox.Show("你输入的内容表中不存在!"); } else { dataGridView1.DataSource = dt; txtEmployeeID.Text = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString(); } } } } catch (Exception ee) { MessageBox.Show(ee.Message); } }//数据删除 private void Delete_Click(object sender, EventArgs e) { try { if (MessageBox.Show("你确定要删除吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { conn = new SqlConnection(connection); conn.Open(); SqlCommand cmd = new SqlCommand("delete from dbo.EmployeeSalaries where 代号=" + txtEmployeeID.Text, conn); SqlDataReader sdr = cmd.ExecuteReader(); MessageBox.Show("删除成功!"); sdr.Close(); sda = new SqlDataAdapter("select * from dbo.EmployeeSalaries", conn); builder = new SqlCommandBuilder(sda); ds = new DataSet(); dt = new DataTable(); sda.Fill(dt); dataGridView1.DataSource = dt; txtEmployeeID.Text = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString(); dataGridView1.DataSource = dt; } } catch (Exception ee) { MessageBox.Show(ee.Message); } }//数据修改 private void Change_Click(object sender, EventArgs e) { try { shouldSalaries = float.Parse(txtShouldSalaries.Text.ToString()); rate = float.Parse(txtRate.Text.ToString()); prize = float.Parse(txtPrize.Text.ToString()); txtFactSalaries.Text = Convert.ToString((shouldSalaries + prize)-shouldSalaries*rate); conn = new SqlConnection(connection); conn.Open(); SqlCommand cmd1 = new SqlCommand("update dbo.EmployeeSalaries set 应发工资=@ShouldSalaries where 代号=" + txtEmployeeID.Text, conn); cmd1.Parameters.Add(new SqlParameter("@ShouldSalaries", txtShouldSalaries.Text)); SqlDataReader sdr1 = cmd1.ExecuteReader(); sdr1.Close(); SqlCommand cmd2 = new SqlCommand("update dbo.EmployeeSalaries set 纳税率=@Rate where 代号=" + txtEmployeeID.Text, conn); cmd2.Parameters.Add(new SqlParameter("@Rate", txtRate.Text)); SqlDataReader sdr2 = cmd2.ExecuteReader(); sdr2.Close(); sda = new SqlDataAdapter("select * from EmployeeSalaries", conn); builder = new SqlCommandBuilder(sda); ds = new DataSet(); dt = new DataTable(); sda.Fill(dt); dataGridView1.DataSource = dt; txtEmployeeID.Text = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString(); MessageBox.Show("数据修改成功!"); } catch (Exception ee) { MessageBox.Show(ee.Message); } } }} vs2008版本 ajax工具条为什么没有出来 WCF 10049: 在其上下文中,该请求的地址无效,急急急! NotSupportedException c#调用c++动态链接库问题,已经半个月 (急!在线等)如何根据Code39字体生成条码并打印的问题 小型游戏中怎样记录怪物移动的位置 关于Form的初始化的问题(棘手) 关于datagrid的菜鸟问题 datareader 怪问题 DataGrid如何自动生成一个字段,表示记录的序号 【急】在创建项目时出现无法创建脱机缓存的提示,不能创建项目,怎么解决? 如何在DataGridView的cell中填加控件来显示数据 一个刷新的问题
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 医院管理系统代码
{
public partial class EmployeeSalariesManage : Form
{
SqlConnection conn;
SqlDataAdapter sda;
SqlCommandBuilder builder;
DataSet ds;
DataTable dt;
DataRow row;
float shouldSalaries = 0;
float rate = 0;
float prize = 0;
string connection = "Data Source=.;Initial Catalog=LifeLine;Integrated Security=True";
public EmployeeSalariesManage()
{
InitializeComponent();
}
//添加数据
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
txtEmployeeID.Text = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
txtEmployeType.Text = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();
txtShouldSalaries.Text = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString();
txtRate.Text = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
txtPrize.Text = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString();
txtFactSalaries.Text = dataGridView1[5, dataGridView1.CurrentCell.RowIndex].Value.ToString();
}
private void Add_Click(object sender, EventArgs e)
{
try
{
conn = new SqlConnection(connection);
sda = new SqlDataAdapter("select * from EmployeeSalaries", conn);
SqlCommandBuilder builder1 = new SqlCommandBuilder(sda);
DataTable table = new DataTable();
sda.Fill(table);
DataRow DR = table.NewRow(); DR[0] = txtEmployeeID.Text;
table.Rows.Add(DR);
sda.Update(table);
dataGridView1.DataSource = table;
MessageBox.Show("数据添加成功!");
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
//数据查询
private void Query_Click(object sender, EventArgs e)
{
try
{
string command = "";
if (comboBoxSelectCondition.Text == "")
{
MessageBox.Show("查询条件不能为空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (comboBoxSelectCondition.Text == "员工代号")
{
command = "select * from dbo.EmployeeSalaries where 代号='" + txtEmployeeID.Text + "'";
}
if (txtEmployeeID.Text.Length == 0 && txtEmployeType.Text.Length == 0 && txtShouldSalaries.Text.Length == 0 && txtRate.Text.Length == 0 && txtPrize.Text.Length == 0 && txtFactSalaries.Text.Length==0)
{
MessageBox.Show("输入的内容不能为空!");
}
else
{
conn = new SqlConnection(connection);
sda = new SqlDataAdapter(command, conn);
builder = new SqlCommandBuilder(sda);
ds = new DataSet();
dt = new DataTable();
sda.Fill(dt);
if (int.Parse(dt.Rows.Count.ToString()) == 0)
{
MessageBox.Show("你输入的内容表中不存在!");
}
else
{
dataGridView1.DataSource = dt;
txtEmployeeID.Text = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
}
}
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
//数据删除
private void Delete_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show("你确定要删除吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
conn = new SqlConnection(connection);
conn.Open();
SqlCommand cmd = new SqlCommand("delete from dbo.EmployeeSalaries where 代号=" + txtEmployeeID.Text, conn);
SqlDataReader sdr = cmd.ExecuteReader();
MessageBox.Show("删除成功!");
sdr.Close();
sda = new SqlDataAdapter("select * from dbo.EmployeeSalaries", conn);
builder = new SqlCommandBuilder(sda);
ds = new DataSet();
dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
txtEmployeeID.Text = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
dataGridView1.DataSource = dt;
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
//数据修改
private void Change_Click(object sender, EventArgs e)
{
try
{
shouldSalaries = float.Parse(txtShouldSalaries.Text.ToString());
rate = float.Parse(txtRate.Text.ToString());
prize = float.Parse(txtPrize.Text.ToString());
txtFactSalaries.Text = Convert.ToString((shouldSalaries + prize)-shouldSalaries*rate); conn = new SqlConnection(connection);
conn.Open();
SqlCommand cmd1 = new SqlCommand("update dbo.EmployeeSalaries set 应发工资=@ShouldSalaries where 代号=" + txtEmployeeID.Text, conn);
cmd1.Parameters.Add(new SqlParameter("@ShouldSalaries", txtShouldSalaries.Text));
SqlDataReader sdr1 = cmd1.ExecuteReader();
sdr1.Close(); SqlCommand cmd2 = new SqlCommand("update dbo.EmployeeSalaries set 纳税率=@Rate where 代号=" + txtEmployeeID.Text, conn);
cmd2.Parameters.Add(new SqlParameter("@Rate", txtRate.Text));
SqlDataReader sdr2 = cmd2.ExecuteReader();
sdr2.Close();
sda = new SqlDataAdapter("select * from EmployeeSalaries", conn);
builder = new SqlCommandBuilder(sda);
ds = new DataSet();
dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
txtEmployeeID.Text = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
MessageBox.Show("数据修改成功!");
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
}
}