在C#中使用控件DataGridView实现数据库增删改查
标签:
itc |
分类: .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 > 0 ? true : false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.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
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.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
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.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
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.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/ExpandedSubBlockStart.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
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.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

加载中…