vs2010 mysql数据库增删改查_利用VS2010使用C#语言建立数据库Oracle_11g实现增删改查管理系统...

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSys

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.OracleClient;

namespace WindowsFormsApplication2

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

OracleConnection conn;//声明一个OracleConnection变量

OracleDataAdapter adapter;

DataSet ds;

OracleDataAdapter sda;

private void Form1_Load(object sender, EventArgs e)

{

}

private void button1_Click(object sender, EventArgs e)

{

//实例化OracleConnection变量conn,,连接数据库

conn = new OracleConnection("Data Source=网络服务名;Persist Security Info=True;User ID=***;Password=***;Unicode=True");

//创建一个OracleDataAdapter对象sda

sda = new OracleDataAdapter("select * from T_table2", conn);

//创建一个DataSet对象

ds = new DataSet();

//使用OracleDataAdapter对象的Fill方法填充DataSet

sda.Fill(ds, "T_table2");

//设置dataGridView1控件数据源

dataGridView1.DataSource = ds.Tables[0];

//button1.Enabled = false;//可以使该按钮操作后不可用,适用于一次行为

//dataGridView1.Columns[0].ReadOnly =false;

}

private DataTable dbconn(string strSql)

{

conn.Open();

this.adapter = new OracleDataAdapter(strSql, conn);

DataTable dtSelect = new DataTable();

int rnt = this.adapter.Fill(dtSelect);

conn.Close();

return dtSelect;

}

private void button_modify_Click(object sender, EventArgs e)

{

conn = new OracleConnection("Data Source=网络服务名;Persist Security Info=True;User ID=***;Password=***;Unicode=True");

conn.Open();

//update table2 set field1=value1 where 范围

string sql = "update T_table2 set " + textBox_modifyType.Text + "='" + textBox_nameAfter.Text + "'where " + textBox_modifyType.Text + "= '" + textBox_nameBefore.Text + "'";

//string sql = "update T_table2 setwhere name= '" + textBox_nameBefore.Text + "'";

OracleCommand cmd = new OracleCommand(sql, conn);

int result = cmd.ExecuteNonQuery();

if (result !=0)

{

MessageBox.Show("修改成功!");

}

else

{

MessageBox.Show("修改失败!");

}

dataGridView1.DataSource = Display();

}

private void button_delete_Click(object sender, EventArgs e)

{

conn = new OracleConnection("Data Source=网络服务名;Persist Security Info=True;User ID=***;Password=***;Unicode=True");

conn.Open();

int i = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);

string sql = "delete from T_table2 where";

//this.textBox_id.Text = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();

//string sql = "delete from T_table2 where";

OracleCommand cmd = new OracleCommand(sql, conn);

int result = cmd.ExecuteNonQuery();

if (result == 1)

{

MessageBox.Show("删除成功!");

}

else

{

MessageBox.Show("删除失败!");

}

dataGridView1.DataSource = Display();

}

private void button_add_Click(object sender, EventArgs e)

{

conn = new OracleConnection("Data Source=网络服务名;Persist Security Info=True;User ID=***;Password=***;Unicode=True");

conn.Open();

string sql = "insert into T_table2(ID,NAME,AGE,SEX,SCHOOL) values('" + textBox_id.Text + "','" + textBox_name.Text + "','" + textBox_age.Text+ "','"+ textBox_sex.Text + "','" + textBox_school.Text + "')";

OracleCommand cmd = new OracleCommand(sql, conn);

int result = cmd.ExecuteNonQuery();

if (result == 1)

{

MessageBox.Show("添加成功!");

}

else

{

MessageBox.Show("添加失败!");

}

dataGridView1.DataSource = Display();

}

private DataTable Display()

{

conn = new OracleConnection("Data Source=网络服务名;Persist Security Info=True;User ID=***;Password=***;Unicode=True");

conn.Open();

string sql = "select * from T_table2";

OracleCommand cmd = new OracleCommand(sql, conn);

OracleDataAdapter oda = new OracleDataAdapter(cmd);

DataTable dt = new DataTable();

oda.Fill(dt);

conn.Close();

cmd.Dispose();

return dt;

}

private void button_select_Click(object sender, EventArgs e)

{

conn = new OracleConnection("Data Source=网络服务名;Persist Security Info=True;User ID=***;Password=***;Unicode=True");

sda = new OracleDataAdapter("select * from T_table2 where or sex ='" + textBox_sex.Text + "'", conn);

ds = new DataSet();

sda.Fill(ds, "T_table2");

//设置dataGridView1控件数据源

dataGridView1.DataSource = ds.Tables[0];

}

private void textBox_modifyType_TextChanged(object sender, EventArgs e)

{

}

}

}

运行效果:

14b5c69104cfe3e8ec42e50928f92f07.png

Oracle数据库管理系统

c75a8b4512f926e08be8f99572612ab2.png

设置选择数据库记录方式dataGridViews—SelectionMode

本文出自 “有志青年” 博客,请务必保留此出处

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值