我从网上复制了一个增删查改的例子,但是只有增和删。没有查和改,,
我设置第5个button,求大神写下里面的改代码。,
另外查代码例子说(// 查询数据还是运用SQL语句较好。使用 SqlCommand对象即可)
什么意思?,是说查询只能在数据库里面查,VS2010不能查询吗?
以下是网上的代码,可以运行。。
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 TEXT6
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection("Server=.;Database=student1;integrated security=SSPI");//创建一个数据库连接,·代表服务器在本地
SqlDataAdapter da;//创建一个数据容器
DataSet ds;//建一个接受数据库返回结果集的容器
SqlCommandBuilder cb;
string tablename = "student1";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
da = new SqlDataAdapter("select * from " + tablename, con);
ds = new DataSet();
da.Fill(ds, tablename);
dataGridView1.DataSource = ds.Tables[tablename];
}
private void button2_Click(object sender, EventArgs e)
{
DataRow dr = ds.Tables[tablename].NewRow();
dr[0] = textBox1.Text;//给第一个字段赋值
dr[1] = textBox2.Text;//给第二个字段赋值
dr[2] = textBox3.Text;
dr[3] = textBox4.Text;
ds.Tables[tablename].Rows.Add(dr);//将新行添加到表中 // 调用提交更新程序
}
private void button3_Click(object sender, EventArgs e)
{
string message = "是否真的要删除该行,并不可恢复?";
string caption = "警告提示"; DialogResult result;
int w = dataGridView1.CurrentCell.RowIndex; //当前鼠标点击的所在行
result = MessageBox.Show(this, message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
DataRowCollection dr = ds.Tables[tablename].Rows;
dr[w].Delete();
}
}
private void button4_Click(object sender, EventArgs e)
{
SqlCommandBuilder cb = new SqlCommandBuilder(da);
da.Update(ds, tablename);
MessageBox.Show("更新成功!");
ds.Tables[tablename].AcceptChanges();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
}
}
}
button5是我另外拖进去的,但是我不会写代码,,改的代码怎么写,,大神求教!!!