加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

在C#中使用控件DataGridView实现数据库增删改查

(2014-09-02 20:35:02)
标签:

it

c

分类: .Net
using System;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
using System.Collections.Generic;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
using System.ComponentModel;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
using System.Data;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
using System.Data.SqlClient;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
using System.Drawing;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
using System.Text;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
using System.Windows.Forms;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
namespace DataSource
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif
{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    public partial class Form1 Form
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        public Form1()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            InitializeComponent();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        private DataSet ds new DataSet();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        private SqlConnection conn null;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        private SqlDataAdapter da null;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        private const string DRIVER "server=.;database=northwind;uid=sa;pwd=sa";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        private const string sql_select "select from region";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif         
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        private void Form1_Load(object sender, EventArgs e)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            conn new SqlConnection(DRIVER);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            da new SqlDataAdapter(sql_select,conn);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            da.Fill(ds,"table");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            this.dataGridView1.DataSource ds.Tables["table"].DefaultView;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        private bool BtnInsert() //此方法作用于添加
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif
        {
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            da.InsertCommand conn.CreateCommand();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            da.InsertCommand.CommandText "insert into region values(@id,@ption)";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            da.InsertCommand.Parameters.Add("@id", SqlDbType.Int, 4, "regionid");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            da.InsertCommand.Parameters.Add("@ption", SqlDbType.VarChar, 10, "regiondescription");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            int count da.Update(ds);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            bool result count true false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            return result;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        private void button1_Click(object sender, EventArgs e)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            if (this.BtnInsert())//调用此方法
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif
            {
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                MessageBox.Show("添加成功!");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif            }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            else 
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif            {
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                MessageBox.Show("添加失败!");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif            }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        private bool BtnDelect() //此方法作用于删除
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif
        {
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            SqlParameter sp new SqlParameter();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            da.DeleteCommand conn.CreateCommand();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            da.DeleteCommand.CommandText "delete region where regionid=@id";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            sp da.DeleteCommand.Parameters.Add("@id", SqlDbType.Int, 4, "regionid");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            sp.SourceVersion DataRowVersion.Original;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            ds.Tables["table"].Rows[this.dataGridView1.CurrentRow.Index].Delete();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            int count da.Update(ds);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            bool result count true false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            return result;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        private void button2_Click(object sender, EventArgs e)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            if (this.BtnDelect())//调用删除方法
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif
            {
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                MessageBox.Show("删除成功!");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif            }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            else
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif            {
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                MessageBox.Show("删除失败!");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif            }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        private bool BtnUpdate() //此方法作用于修改
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif
        {
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            SqlParameter sp new SqlParameter();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            da.UpdateCommand conn.CreateCommand();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            da.UpdateCommand.CommandText "update region set regionid=@id,regiondescription=@ption where regionid=@oldid";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            da.UpdateCommand.Parameters.Add("@id", SqlDbType.Int, 4, "regionid");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            da.UpdateCommand.Parameters.Add("@ption", SqlDbType.VarChar, 10, "regiondescription");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            sp da.UpdateCommand.Parameters.Add("@oldid", SqlDbType.Int, 4, "regionid");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            sp.SourceVersion DataRowVersion.Original;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            int count da.Update(ds);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            bool result count true false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            return result;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        private void button3_Click(object sender, EventArgs e)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            if (this.BtnUpdate())//调用修改方法
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif
            {
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                MessageBox.Show("修改成功!");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif            }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            else
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif            {
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                MessageBox.Show("修改失败!");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif            }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}

转自 http://www.cnblogs.com/da6wei6/archive/2008/08/27/1277309.html

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有