sqlhelper.cs,如何用它实现Datagridview的添删改查功能?????????

sunupsunup 2009-03-25 03:02:27
用C/S模式
请问如何使用sqlhelper.cs,如何调用sqlhelper啊,跪求大侠给详细剖解一下
实现数据库商品表[Product]的添删改查,具体代码如下
如何使用sqlhelper.cs实现下面的四大功能?????请大侠帮小弟实现代码
-----------------------------------------------------------------------------------
这个是商品管理的主窗体frmProduct
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;
using Isms.Sys;

namespace Isms.baseinfo
{
public partial class frmProduct : Form
{
public static int updateId;
public frmProduct()
{
InitializeComponent();
}
private void frmProduct_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“ismsDataSet1.product”中。您可以根据需要移动或移除它。
this.productTableAdapter.Fill(this.ismsDataSet1.product);
}
private void dataGridView1_DoubleClick(object sender, EventArgs e)//双击事件
{
updateId = (int)this.dataGridViewProduct.CurrentRow.Cells[0].Value;
frmProductUpdate fpu = new frmProductUpdate();
fpu.ShowDialog();
}

private void toolStripButtonUpdate_Click(object sender, EventArgs e)//修改按钮
{
updateId = (int)this.dataGridViewProduct.CurrentRow.Cells[0].Value;
frmProductUpdate fpu = new frmProductUpdate();
fpu.ShowDialog();
}
private void toolStripButtonAdd_Click(object sender, EventArgs e)//添加按钮
{
frmProductUpdate fpu = new frmProductUpdate();
fpu.ShowDialog();
}
private void toolStripButtonDel_Click(object sender, EventArgs e)//删除按钮
{
}
private void dataGridViewProduct_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
此窗体有四个四个button按钮,一个datagridview
---------------------------------------------------------------------------------------------------------------
下面是添加/修改窗体frmProductUpdate
namespace Isms.baseinfo
{
public partial class frmProductUpdate : Form
{
public frmProductUpdate()
{
InitializeComponent();
}

private void frmProductUpdate_Load(object sender, EventArgs e)
{
}
private void btn_update_Click(object sender, EventArgs e)//修改按钮
{
}
private void btn_insert_Click(object sender, EventArgs e)//添加按钮
{
}
此窗体有6个textbox,分别是txt_name,txt_unit,txt_ab,txt_cu,txt_code,txt_ic,
-----------------------------------------------------------------------------
app.config 如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="Isms.Properties.Settings.ismsConnectionString" connectionString="Data Source=.;Initial Catalog=isms;Persist Security Info=True;User ID=sa;password=123;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
-------------------------------------------------------------------------------------------------------------------------
config.cs 如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections;

namespace Isms.Sys
{
class Config
{
public static int id;
public static string name;
public static ArrayList roleId;
public static ArrayList roleName;
public static ArrayList roleMenu;
public static DateTime lastLogon;
public static int RefreshTime;
public static string connection = ConfigurationManager.ConnectionStrings["Isms.Properties.Settings.ismsConnectionString"].ConnectionString;

}
}
------------------------------------------------------------------------------------------------------------------------
数据库是sqlserver2008,表[Product]的字段
id 主键;name[nvarchar50];ab[nvarchar50];unit[nvarchar50];code[int];cu[decimal(18,0)];ic[decimal(18,0)];is_active[int(默认值1)]

-------------------------
上述描述的很详细,就多谢高手援助了
本人菜鸟,多谢高手指点
...全文
254 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mmrwbb01 2009-03-25
  • 打赏
  • 举报
回复
路过
秦彬 2009-03-25
  • 打赏
  • 举报
回复
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);
}
}
}
}
tianshangwuyu 2009-03-25
  • 打赏
  • 举报
回复
up···
tianshangwuyu 2009-03-25
  • 打赏
  • 举报
回复
UP```
tianshangwuyu 2009-03-25
  • 打赏
  • 举报
回复
用的是微软已经给封装好的sqlhelper.cs !!!

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧