c# win下如何实现删除 增加 修改

mmiiaaoo 2008-07-07 03:35:08
小弟初学 大家帮帮忙 分不在话下
...全文
362 38 打赏 收藏 转发到动态 举报
写回复
用AI写文章
38 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuhz_jsmstc 2008-07-09
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 mmiiaaoo 的回复:]
如何取的选中的行的值呢
[/Quote]

如果你用的是datagrid的话,你要以这样子取:
int i=this.datagrid1.currentrow.index;
string uname=ds.table[0].row[i]["uname"].tostring();
qjlsharp 2008-07-08
  • 打赏
  • 举报
回复
这是我以前做的一个用户管理窗体界面实现增删改查的功能,应该能对你有所帮助,具体的控件你自己托吧

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.SqlClient;

namespace _0122Demo
{
public partial class Form8 : Form
{
public Form8()
{
InitializeComponent();
}
string con = "server=.;database=hangkong;uid=sa;pwd=";
SqlDataAdapter DA;
DataTable DT;
private void Form8_Load(object sender, EventArgs e)
{ //查询方法
CX();
}

//实现查询
private void CX()
{
string sql = "select userid,username,userpass,userjb from users";

//以下实现数据的查询
DA = new SqlDataAdapter(sql, con);
DT = new DataTable();//IIF(total>1000, 'expensive', 'dear')
SqlCommandBuilder scb = new SqlCommandBuilder(DA);
DA.Fill(DT);
DT.Columns.Add("jbsm", typeof(string), "IIF(userjb=0,'超级用户','普通用户')");


dataGridView1.DataSource = DT;
}

private void button1_Click(object sender, EventArgs e)
{
DA.Update(DT);
}

private void button2_Click(object sender, EventArgs e)
{//设置标志判断用户级别
string bz = "";
if (this.comboBox1.Text == "超级用户")
{
bz = "0";
}
else
{
bz ="1";
}
//增加数据
string sql = "insert into users values('"+textBox1.Text +"','"
+textBox2.Text +"','"+textBox3.Text +"','"
+bz +"')" ;


GXSJK(sql);

}
//实现增删改的方法
private void GXSJK(string sql)
{
SqlConnection conn = new SqlConnection(con);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
CX();
Clear();
}

//清空窗体上所有textbox
private void Clear()
{
foreach (Control co in this.Controls )
{
if (co is TextBox)
{
((TextBox)co).Clear();
}
}
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{//从内存表中读出数据到控件中
if (e.RowIndex > -1)
{
textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
textBox3.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();

if ("超级用户" == dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString())
{
comboBox1.SelectedIndex = 0;
}
else
{
comboBox1.SelectedIndex = 1;
}
textBox1.Enabled = false;
}

}

private void button3_Click(object sender, EventArgs e)
{//设置标志判断用户级别
string bz = "";
if (this.comboBox1.Text == "超级用户")
{
bz = "0";
}
else
{
bz = "1";
}
//修改数据
string sql = "update users set username='" + textBox2.Text + "',"
+ "userpass='" + textBox3.Text + "',userjb='" + bz + "'"
+ " where userid='" + textBox1.Text + "'";
GXSJK(sql);
CX();
Clear();
textBox1.Enabled = true;

}

private void button4_Click(object sender, EventArgs e)
{//删除数据
string sql = "delete users where userid='" + textBox1.Text + "'";
GXSJK(sql);
CX();
Clear();
textBox1.Enabled = true;

}
}
}

GhostAdai 2008-07-08
  • 打赏
  • 举报
回复
多看看别人做的例子,举一反三,学习一下就明白了。你这问题可问得宽泛了,增删改查以及最后数据的展示涉及的技巧可就多了。
hecker728 2008-07-08
  • 打赏
  • 举报
回复
学习下!
lchh0917 2008-07-08
  • 打赏
  • 举报
回复
上传了几个C#数据库应用程序,涵盖了数据库的基本操作和其他特殊操作功能,有兴趣的可以看一下:
http://download.csdn.net/source/529881
mmiiaaoo 2008-07-08
  • 打赏
  • 举报
回复
事先绑定了数据 对原绑定数据的增加 删除 和修改
mmiiaaoo 2008-07-08
  • 打赏
  • 举报
回复
private int adrow;//增加前的datagridview的行数 
private int suremofify;//是否是修改还是增加,在保存按钮中用到


dataclass dbclass = new dataclass();
public weldmodify()
{
InitializeComponent();
}

private void weldmodify_Load(object sender, EventArgs e)
{
this.Text = "焊装模板修改";


}

private void toolStripLabel2_Click(object sender, EventArgs e)
{

}

private void toolStripLabel1_Click(object sender, EventArgs e)
{
Bind();


}
#region 绑定数据
private void Bind()
{
//绑定焊装模板
string sql = "select * from tabweld order by weldid";

dataGridView1.DataSource = dbclass.dt(sql, "p");
dataGridView1.ReadOnly = true;
dataGridView1.AllowUserToAddRows = false;
//停在第1列,或者焦点,当强停在位置
//this.dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[1];
// dataGridView1.BeginEdit(false);
//停在第1列,


}
#endregion

#region 增加按钮事件
private void toolStripLabel2_Click_1(object sender, EventArgs e)
{
adrow = dataGridView1.Rows.Count;
dataGridView1.AllowUserToAddRows=true;
dataGridView1.ReadOnly =false;
suremofify = 0;

}
#endregion

#region 删除事件

private void toolStripLabel3_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
if (MessageBox.Show("确定要删除?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{

string d = this.dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();


string sql = "delete from tabweld where weldid='" + d + "'";
dbclass.excommand(sql);
Bind();

}
}
else
{
dbclass.msgbox("请选择删除列","提示",0);return;


}

//dbclass.msgbox(d, "提示", 0);
//return;


}
#endregion

#region 修改数据

private void toolStripLabel4_Click(object sender, EventArgs e)
{
this.dataGridView1.ReadOnly = false;
suremofify = 1;
dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[1];//当前停在
dataGridView1.BeginEdit(true);

}
#endregion
#region 保存
private void toolStripLabel5_Click(object sender, EventArgs e)
{

if (suremofify == 0)
{
//增加事件
int t = dataGridView1.Rows.Count;//增加的条数
for (int i = adrow; i < t - 1; i++)
{
//dbclass.msgbox(i.ToString(), "t", 0);
//return;
string weldid = dataGridView1.Rows[i].Cells["weldid"].Value.ToString();

string workconent = dataGridView1.Rows[i].Cells["workconent"].Value.ToString();
string jiancha = dataGridView1.Rows[i].Cells["jiancha"].Value.ToString();
string guzhang = dataGridView1.Rows[i].Cells["guzhang"].Value.ToString();
string trueman = dataGridView1.Rows[i].Cells["trueman"].Value.ToString();
string mysql = "insert into tabweld (weldid,workconent,jiancha,guzhang,trueman) values('" + weldid + "' ,'" + workconent + "','" + jiancha + "','" + guzhang + "','" + trueman + "')";
//string mysql = "";
dbclass.excommand(mysql);




}

Bind();
dbclass.msgbox("增加提交成功!", "提示", 0);
}

else
{
//修改事件
for (int i = 0; i < dataGridView1.Rows.Count;i++)
{


string weldid = dataGridView1.Rows[i].Cells["weldid"].Value.ToString();
string workconent = dataGridView1.Rows[i].Cells["workconent"].Value.ToString();
string jiancha = dataGridView1.Rows[i].Cells["jiancha"].Value.ToString();
string guzhang = dataGridView1.Rows[i].Cells["guzhang"].Value.ToString();
string trueman = dataGridView1.Rows[i].Cells["trueman"].Value.ToString();
string mysql = "update tabweld set workconent='" + workconent + "',jiancha='" + jiancha + "',guzhang='" + guzhang + "',trueman='" + trueman + "'where weldid='" + weldid + "'";
// dbclass.msgbox(mysql, "t", 0);
// return;

dbclass.excommand(mysql);






}
Bind();

dbclass.msgbox("修改提交成功!", "提示", 0);


}

}
#endregion
#region datagridview 回车代替TAB
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{

if (msg.WParam.ToInt32() == (int)Keys.Enter)
{

SendKeys.Send("{Tab}");

return true;

}

return base.ProcessCmdKey(ref msg, keyData);

}

#endregion
net0003 2008-07-08
  • 打赏
  • 举报
回复
删除 增加 修改 什么和什么啊
mmiiaaoo 2008-07-08
  • 打赏
  • 举报
回复
C# code
{
private int adrow;//增加前的datagridview的行数
private int suremofify;//是否是修改还是增加,在保存按钮中用到


dataclass dbclass = new dataclass();
public weldmodify()
{
InitializeComponent();
}

private void weldmodify_Load(object sender, EventArgs e)
{
this.Text = "焊装模板修改";


}

private void toolStripLabel2_Click(object sender, EventArgs e)
{

}

private void toolStripLabel1_Click(object sender, EventArgs e)
{
Bind();


}
#region 绑定数据
private void Bind()
{
//绑定焊装模板
string sql = "select * from tabweld order by weldid";

dataGridView1.DataSource = dbclass.dt(sql, "p");
dataGridView1.ReadOnly = true;
dataGridView1.AllowUserToAddRows = false;
//停在第1列,或者焦点,当强停在位置
//this.dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[1];
// dataGridView1.BeginEdit(false);
//停在第1列,


}
#endregion

#region 增加按钮事件
private void toolStripLabel2_Click_1(object sender, EventArgs e)
{
adrow = dataGridView1.Rows.Count;
dataGridView1.AllowUserToAddRows=true;
dataGridView1.ReadOnly =false;
suremofify = 0;

}
#endregion

#region 删除事件

private void toolStripLabel3_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
if (MessageBox.Show("确定要删除?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{

string d = this.dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();


string sql = "delete from tabweld where weldid='" + d + "'";
dbclass.excommand(sql);
Bind();

}
}
else
{
dbclass.msgbox("请选择删除列","提示",0);return;



}

//dbclass.msgbox(d, "提示", 0);
//return;


}
#endregion

#region 修改数据

private void toolStripLabel4_Click(object sender, EventArgs e)
{
this.dataGridView1.ReadOnly = false;
suremofify = 1;
dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[1];//当前停在
dataGridView1.BeginEdit(true);

}
#endregion
#region 保存
private void toolStripLabel5_Click(object sender, EventArgs e)
{

if (suremofify == 0)
{
//增加事件
int t = dataGridView1.Rows.Count;//增加的条数
for (int i = adrow; i < t - 1; i++)
{
//dbclass.msgbox(i.ToString(), "t", 0);
//return;
string weldid = dataGridView1.Rows[i].Cells["weldid"].Value.ToString();

string workconent = dataGridView1.Rows[i].Cells["workconent"].Value.ToString();
string jiancha = dataGridView1.Rows[i].Cells["jiancha"].Value.ToString();
string guzhang = dataGridView1.Rows[i].Cells["guzhang"].Value.ToString();
string trueman = dataGridView1.Rows[i].Cells["trueman"].Value.ToString();
string mysql = "insert into tabweld (weldid,workconent,jiancha,guzhang,trueman) values('" + weldid + "' ,'" + workconent + "','" + jiancha + "','" + guzhang + "','" + trueman + "')";
//string mysql = "";
dbclass.excommand(mysql);




}

Bind();
dbclass.msgbox("增加提交成功!", "提示", 0);
}

else
{
//修改事件
for (int i = 0; i < dataGridView1.Rows.Count;i++)
{


string weldid = dataGridView1.Rows[i].Cells["weldid"].Value.ToString();
string workconent = dataGridView1.Rows[i].Cells["workconent"].Value.ToString();
string jiancha = dataGridView1.Rows[i].Cells["jiancha"].Value.ToString();
string guzhang = dataGridView1.Rows[i].Cells["guzhang"].Value.ToString();
string trueman = dataGridView1.Rows[i].Cells["trueman"].Value.ToString();
string mysql = "update tabweld set workconent='" + workconent + "',jiancha='" + jiancha + "',guzhang='" + guzhang + "',trueman='" + trueman + "'where weldid='" + weldid + "'";
// dbclass.msgbox(mysql, "t", 0);
// return;

dbclass.excommand(mysql);







}
Bind();

dbclass.msgbox("修改提交成功!", "提示", 0);


}

}
#endregion
#region datagridview 回车代替TAB
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{

if (msg.WParam.ToInt32() == (int)Keys.Enter)
{

SendKeys.Send("{Tab}");

return true;

}

return base.ProcessCmdKey(ref msg, keyData);

}

#endregion


我自己实现了功能
可能写的不好 请大家指点一下 哪里需要改进
amandag 2008-07-08
  • 打赏
  • 举报
回复
建议增删改用参数,楼主拼接SQL语句的方式很容易被SQL注入

dbclass.excommand这个方法接受sql语句和参数数组即可
marvelstack 2008-07-08
  • 打赏
  • 举报
回复
winforms使用Ado。net的例子,
http://blog.csdn.net/zhzuo/archive/2004/07/03/33273.aspx
http://blog.csdn.net/zhzuo/archive/2004/08/06/67016.aspx
http://blog.csdn.net/zhzuo/archive/2004/08/06/67037.aspx
http://blog.csdn.net/zhzuo/archive/2005/01/03/238273.aspx
peng_weida 2008-07-08
  • 打赏
  • 举报
回复
我也来学习!
haomas 2008-07-08
  • 打赏
  • 举报
回复
多学习学习就好了
pp_shy 2008-07-07
  • 打赏
  • 举报
回复
增加和修改可以弹出一个模态对话框来实现,删除可以在显示DataGridView的窗体直接实现
mmiiaaoo 2008-07-07
  • 打赏
  • 举报
回复
不是sql的问题了 不是这么简单的哦
gongyi101459 2008-07-07
  • 打赏
  • 举报
回复
SQL语句:
插入语句:insert into 学籍(学号,姓名,性别,出生日期,班级) values ('20055099','小龚','男','1985-9-8','05-22')
更新语句:update 学籍 set 姓名='gy',性别 ='男',出生日期='85-5-8',班级='05' where 学号='20055099'
删除语句:DELETE FROM 学籍 where 学号='20055099'
查询语句:SELECT * from 成绩 WHERE 学号='20055036'
strivechou 2008-07-07
  • 打赏
  • 举报
回复
又学习了一次。
mmiiaaoo 2008-07-07
  • 打赏
  • 举报
回复
是啊 是已经实现了绑定 在删除 修改 更改
amandag 2008-07-07
  • 打赏
  • 举报
回复
实现已经绑定了数据
==
使用DataGridView ?
xiaoqiuwyh 2008-07-07
  • 打赏
  • 举报
回复
直接对数据库进行操作
加载更多回复(18)

110,620

社区成员

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

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

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