a62297562 2015-07-10 01:27 采纳率: 100%
浏览 2425
已采纳

求查改代码,数据库和vs2010的

我从网上复制了一个增删查改的例子,但是只有增和删。没有查和改,,

我设置第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是我另外拖进去的,但是我不会写代码,,改的代码怎么写,,大神求教!!!

  • 写回答

4条回答

  • jamejoe 2015-07-10 02:49
    关注

    button1就是查啊, button2增, button3删, button4是更新,你想改的话讲界面上的改了后重新对表更新下就好了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型