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条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退