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!");
}
}
}
}