vs2005 (c#)+SQL2005, winform 中的datagridview添加、修改、删除如何做呢?
vs2005 (c#)+SQL2005, winform 中的datagridview添加、修改、删除数据库操作如何做呢?希望大侠给一个具体的操作例子。 --------------------编程问答-------------------- 简单的方法就是绑定后`用adapter更新`
绑定:
SqlDataAdapter SDA = new SqlDataAdapter();
SqlCommand SCMD = new SqlCommand(Select_String, Conn);
SDA.SelectCommand = SCMD;
SDT.Clear();
SDA.Fill(SDT);
datagridview.DataSource = SDT;
更新:
SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);
SDA.Update(SDT); --------------------编程问答-------------------- 添加/修改/删除最好的办法是对内存中的DataTable(DataGridView绑定源)操作.
在DataGridView上分别放三个按钮和一个保存按钮.
添加:在DataTable新增一行,值为空,然后绑定到DataGridView,点"保存"按钮更新数据库.
修改:获取DataGridView中当前行的索引,将这一行设置可修改状态,点"保存"按钮更新数据库.
删除:获取DataGridView中当前行的索引,在DataTalbe中找到对就的行删除,再次绑定,同时获取这一行的主键值,删除数据库中的行实现和界面同步. --------------------编程问答-------------------- 留下油箱吧 ! --------------------编程问答-------------------- ERPCoder(Most Valuable Player)
说的是那么回事 --------------------编程问答-------------------- 多表更新呢!
如數據源 select a.id,b.name,b.detail from a,b where a.id=b.id
如果要更新b表,如果b表還有一些字段沒有顯示 --------------------编程问答-------------------- 我的邮箱zhbzsp@sina.com
而且,我也有zsyutiannew() 类似疑问啊 --------------------编程问答-------------------- 我的邮箱zhuoyilin@126.com --------------------编程问答-------------------- 也给我来一份吧 --------------------编程问答-------------------- 我也要
wyyxl2002@126.com --------------------编程问答-------------------- 我也要
110male@sohu.com --------------------编程问答-------------------- bingzai106@163.com我也要啊 --------------------编程问答-------------------- 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 studentshanchu : Form
{
CurrencyManager cmAmend;
public studentshanchu()
{
InitializeComponent();
}
private void studentshanchu_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“studentDataSet.studentinfo”中。您可以根据需要移动或移除它。
//this.studentinfoTableAdapter.Fill(this.studentDataSet.studentinfo);
//数据绑定
string sqlsel = "select * from studentinfo ";
DataTable dt = bangding(sqlsel);
cmAmend = (CurrencyManager)BindingContext[dt];
this.dataGridView1.DataSource = dt;
this.textBox1.DataBindings.Add("text", dt, "sno");
this.textBox2.DataBindings.Add("text", dt, "sname");
this.textBox3.DataBindings.Add("text", dt, "s易做图");
this.textBox4.DataBindings.Add("text", dt, "sage");
this.textBox5.DataBindings.Add("text", dt, "sclass");
this.textBox6.DataBindings.Add("text", dt, "sdept");
this.textBox7.DataBindings.Add("text", dt, "saddress");
this.textBox8.DataBindings.Add("text", dt, "sphone");
this.textBox9.DataBindings.Add("text", dt, "sqq");
}
DataTable bangding(string sqlsel)
{
string connstr = "server=.;uid=sa;pwd=sa;database=sc";
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn);
da.Fill(ds, "coust");
DataTable dt = ds.Tables["coust"];
conn.Close();
return dt;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{ this.Height = 450; }
else
{ this.Height = 250; }
}
private void button1_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你确定要删除该记录吗", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
int pos = this.dataGridView1.CurrentCell.RowIndex; //获取该行
string connstr = "server=.;uid=sa;pwd=sa;database=sc";
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
DataSet ds = new DataSet();
string sqlset = "select * from studentinfo";
//数据集
SqlDataAdapter da = new SqlDataAdapter(sqlset, conn);
da.Fill(ds, "coust");
DataTable dt = ds.Tables["coust"];
SqlCommandBuilder cb = new SqlCommandBuilder(da);
dt.Rows[pos].Delete();
da.Update(ds, "coust");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox9.Text = "";
this.dataGridView1.DataSource = bangding(sqlset);
MessageBox.Show("恭喜你已成功删除","温馨提示");
conn.Close();
}
}
}
}
}
修改的代码:
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 studentxiugai : Form
{
string connstr = "server=.;uid=sa;pwd=sa;database=sc";
string per;
string sno;
CurrencyManager cmAmend;
public studentxiugai()
{
InitializeComponent();
}
public studentxiugai(string k,string s)
{
InitializeComponent();
per = k;
sno = s;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{ //this.Height = 450;
comboBox1.Enabled = false;
textBox1.Enabled = false;
}
else
{ //this.Height = 250;
comboBox1.Enabled = true;
textBox1.Enabled = true;
}
}
private void studentxiugai_Load(object sender, EventArgs e)
{
//数据绑定
if (per == "超级用户")
{
string sqlsel = "select * from studentinfo order by sno asc ";
DataTable dt = bangding(sqlsel);
cmAmend = (CurrencyManager)BindingContext[dt];
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "sno";
this.textBox1.DataBindings.Add("text", dt, "sname");
this.textBox2.DataBindings.Add("text", dt, "s易做图");
this.textBox3.DataBindings.Add("text", dt, "sage");
this.textBox4.DataBindings.Add("text", dt, "sclass");
this.textBox5.DataBindings.Add("text", dt, "sdept");
this.textBox6.DataBindings.Add("text", dt, "saddress");
this.textBox7.DataBindings.Add("text", dt, "sphone");
this.textBox8.DataBindings.Add("text", dt, "sqq");
}
if (per == "普通用户")
{
string sqlsel = "select *from studentinfo where sno='" + sno + "'";
DataTable dt = bangding(sqlsel);
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "sno";
this.textBox1.DataBindings.Add("text", dt, "sname");
this.textBox2.DataBindings.Add("text", dt, "s易做图");
this.textBox3.DataBindings.Add("text", dt, "sage");
this.textBox4.DataBindings.Add("text", dt, "sclass");
this.textBox5.DataBindings.Add("text", dt, "sdept");
this.textBox6.DataBindings.Add("text", dt, "saddress");
this.textBox7.DataBindings.Add("text", dt, "sphone");
this.textBox8.DataBindings.Add("text", dt, "sqq");
}
}
DataTable bangding(string sqlsel)
{
// string connstr = "server=.;uid=sa;pwd=sa;database=sc";
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn);
da.Fill(ds, "coust");
DataTable dt = ds.Tables["coust"];
conn.Close();
return dt;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
string sno = comboBox1.Text;
string sname = textBox1.Text;
string 易做图 = textBox2.Text;
string age = textBox3.Text;
string sclass = textBox4.Text;
string sdept = textBox5.Text;
string saddress = textBox6.Text;
string sphone = textBox7.Text;
string sqq = textBox8.Text;
if (textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "")
{
if (textBox2.Text == "")
{
MessageBox.Show("性别不能为空", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
textBox2.Focus();
return;
}
if (textBox3.Text == "")
{
MessageBox.Show("年龄不能为空", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
textBox3.Focus();
return;
}
if (textBox4.Text == "")
{
MessageBox.Show("班级不能为空", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
textBox4.Focus();
return;
}
if (textBox5.Text == "")
{
MessageBox.Show("系别不能为空", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
textBox5.Focus();
return;
}
}
else
{
string connstr = "server=IT32;uid=sa;pwd='sa';database=sc;";
try
{
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string sqlupdate = " update studentinfo set s易做图='" + 易做图 + "',sage='" + age + "',sclass='" + sclass + "',sdept='" + sdept + "',saddress='" + saddress + "',sphone='" + sphone + "',sqq='" + sqq + "' where sno='" + sno + "'";
SqlCommand sc = new SqlCommand(sqlupdate, conn);
sc.ExecuteNonQuery();
MessageBox.Show("数据已经修改成功", "温馨提示");
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
else
{
MessageBox.Show("对不起,你还没有选中'我要修改',所以您无法修改","温馨提示");
}
}
}
}
--------------------编程问答-------------------- Dim sqlConn As New SqlConnection
sqlConn.ConnectionString = "server=(local);database=northwind;Integrated Security=true"
Dim da As New SqlClient.SqlDataAdapter
Dim delcmd As New SqlCommand
'delcmd.CommandText = "delete from orders where orderID=@id"
'create proc proc_deleteOrdersByID
'@id as int
'as
'delete from orders where orderID=@id
delcmd.CommandText = "proc_deleteOrdersByID"
'delcmd.CommandType = CommandType.Text
delcmd.CommandType = CommandType.StoredProcedure
delcmd.Connection = sqlConn
delcmd.Parameters.Add("@id", SqlDbType.Int)
delcmd.Parameters("@id").SourceColumn = "orderID"
delcmd.Parameters("@id").SourceVersion = DataRowVersion.Original
da.DeleteCommand = delcmd
Dim insertCmd As New SqlCommand
insertCmd.CommandText = "proc_NewOrder"
insertCmd.CommandType = CommandType.StoredProcedure
insertCmd.Connection = sqlConn
insertCmd.Parameters.Add("@custID", SqlDbType.NChar, 5)
insertCmd.Parameters("@custID").SourceColumn = "customerID"
insertCmd.Parameters("@custID").SourceVersion = DataRowVersion.Current
insertCmd.Parameters.Add("@empID", SqlDbType.Int)
insertCmd.Parameters("@empID").SourceColumn = "employeeID"
insertCmd.Parameters("@empID").SourceVersion = DataRowVersion.Current
insertCmd.Parameters.Add("@ordate", SqlDbType.DateTime)
insertCmd.Parameters("@ordate").SourceColumn = "orderdate"
insertCmd.Parameters("@ordate").SourceVersion = DataRowVersion.Current
insertCmd.Parameters.Add("@redate", SqlDbType.DateTime)
insertCmd.Parameters("@redate").SourceColumn = "requireddate"
insertCmd.Parameters("@redate").SourceVersion = DataRowVersion.Current
da.InsertCommand = insertCmd
Dim updateCmd As New SqlCommand
updateCmd.CommandText = "proc_modifyOrder"
updateCmd.CommandType = CommandType.StoredProcedure
updateCmd.Connection = sqlConn
updateCmd.Parameters.Add("@orderID", SqlDbType.Int)
updateCmd.Parameters("@orderID").SourceColumn = "orderID"
updateCmd.Parameters("@orderID").SourceVersion = DataRowVersion.Original
updateCmd.Parameters.Add("@newCustID", SqlDbType.NChar, 5)
updateCmd.Parameters("@newCustID").SourceColumn = "customerID"
updateCmd.Parameters("@newCustID").SourceVersion = DataRowVersion.Current
updateCmd.Parameters.Add("@newEmpID", SqlDbType.Int)
updateCmd.Parameters("@newEmpID").SourceColumn = "employeeID"
updateCmd.Parameters("@newEmpID").SourceVersion = DataRowVersion.Current
updateCmd.Parameters.Add("@newOrdDate", SqlDbType.DateTime)
updateCmd.Parameters("@newOrdDate").SourceColumn = "orderdate"
updateCmd.Parameters("@newOrdDate").SourceVersion = DataRowVersion.Current
updateCmd.Parameters.Add("@newReDate", SqlDbType.DateTime)
updateCmd.Parameters("@newReDate").SourceColumn = "requireddate"
updateCmd.Parameters("@newReDate").SourceVersion = DataRowVersion.Current
da.UpdateCommand = updateCmd
da.Update(ds, "cust")
ds.AcceptChanges()
'ds.Clear() --------------------编程问答-------------------- sqlAdapter.Update(); --------------------编程问答-------------------- 建议MSDN看看,一些文章写挺好 --------------------编程问答-------------------- 同意楼上的,我如果遇到不懂的地方,首先想着MSDN,上面有好多有用的东西,建议多看看. --------------------编程问答-------------------- qq:634229897欢迎交流。634229897@qq.com --------------------编程问答-------------------- mark --------------------编程问答-------------------- UP,支持下,我也想知道。 --------------------编程问答--------------------
....想知道就发帖问、上网查,挖坟是不对滴。。。。 --------------------编程问答-------------------- 简单,设置几个参数即可搞定.
看看这个
http://download.csdn.net/user/xq1101 --------------------编程问答-------------------- 有好多有用的东西,建议多看看 --------------------编程问答-------------------- 看下这个.
http://blog.csdn.net/21aspnet/archive/2007/03/25/1540301.aspx
补充:.NET技术 , C#