C#2005 the use of control DataGridView to implement database additions and deletions to check operations

Source: Internet
Author: User
Tags bool

Using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Data.SqlClient;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


namespace DataSource


{


public partial class Form1:form


{


public Form1 ()


     {


InitializeComponent ();


     }


Private DataSet ds = new DataSet ();


private SqlConnection conn = null;


private SqlDataAdapter da = null;


Private Const string DRIVER = "server=.; Database=northwind;uid=sa;pwd=sa ";


Private Const string sql_select = "SELECT * from Region";


/**//**


* This method queries the data from the region table in the database Northwind and puts it into the dataset


**/


private void Form1_Load (object sender, EventArgs e)


  {


conn = new SqlConnection (DRIVER);


da = new SqlDataAdapter (Sql_select,conn);


da. Fill (ds, "table");


This.dataGridView1.DataSource = ds. tables["Table"]. DefaultView;


}


private bool Btninsert ()///This method is used to add


  {


da. InsertCommand = conn. CreateCommand ();


Da.InsertCommand.CommandText = "INSERT into region (id,ption) VALUES (@id, @ption)";


da. INSERTCOMMAND.PARAMETERS.ADD ("@id", SqlDbType.Int, 4, "RegionID");


da. INSERTCOMMAND.PARAMETERS.ADD ("@ption", SqlDbType.VarChar, "regiondescription");


int count = da. Update (DS);


bool result = count > 0? True:false;


return result;


}


private void Button1_Click (object sender, EventArgs e)


{


if (this. Btninsert ())//Call this method


     {


MessageBox.Show ("Add success!");


     }


Else


     {


MessageBox.Show ("Add failed!");


    }


}


private bool Btndelect ()///This method is used to delete


{


Sqlparameterpara = new SqlParameter ();


da. DeleteCommand = conn. CreateCommand ();


da.DeleteCommand.CommandText = "Delete fromregion where regionid= @id";


para = da. DELETECOMMAND.PARAMETERS.ADD ("@id", SqlDbType.Int, 4, "RegionID");


para. SourceVersion = datarowversion.original;//Get original value


ds. tables["Table"]. Rows[this.datagridview1.currentrow.index]. Delete ();


int count = da. Update (DS);


bool result = count > 0? True:false;


return result;


}


private void Button2_Click (object sender, EventArgs e)


{


if (this. Btndelect ())//Call Delete method


   {


MessageBox.Show ("delete successful!");


  }


Else


  {


MessageBox.Show ("Delete failed!");


  }


}


private bool Btnupdate ()///This method is used to modify


{


SqlParameter para = new SqlParameter ();


da. UpdateCommand = conn. CreateCommand ();


da.UpdateCommand.CommandText = "Update region set regionid= @id, regiondescription= @ption where regionid= @oldid";


da. UPDATECOMMAND.PARAMETERS.ADD ("@id", SqlDbType.Int, 4, "RegionID");


da. UPDATECOMMAND.PARAMETERS.ADD ("@ption", SqlDbType.VarChar, "regiondescription");


Para= da. UPDATECOMMAND.PARAMETERS.ADD ("@oldid", SqlDbType.Int, 4, "RegionID");


para. SourceVersion = datarowversion.original;


int count = da. Update (DS);


bool result = count > 0? True:false;


return result;


}


private void Button3_Click (object sender, EventArgs e)


{


if (this. Btnupdate ())//Call modification method


   {


MessageBox.Show ("Modify success!");


  }


Else


  {


MessageBox.Show ("Modify failed!");


  }


}


}


}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.