C#如何实现对Oracle数据库的操作,连接数据库,添删改查这四个功能,这是老师给我布置的任务,求求各位给些帮助,谢谢!
下面是我自己写的代码
//添加数据部分
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
namespace Add
{
public partial class Form1 : Form
{
public OracleConnection myConn;
public Form1()
{
InitializeComponent();
}
public bool Check1()
{
try
{
string myConnStr = "user id= xbh;data source = practice;password = xbh";
myConn = new OracleConnection(myConnStr);
myConn.Open();
return true;
}
catch
{
return false;
}
finally
{
myConn.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
Check1();
string sqlStr = "Select * from Addresslist";
OracleDataAdapter myAdapter = new OracleDataAdapter(sqlStr, myConn);
DataTable myDataTable = new DataTable();
DataRow myRow = myDataTable.NewRow();
myAdapter.Fill(myDataTable);
bool temp = true;
foreach (DataRow myrow in myDataTable.Rows)
{
if (textBox1.Text.Trim() == myrow["name"].ToString().Trim())
{
MessageBox.Show("此人已存在!");
textBox1.Text = "";
textBox2.Text = "";
temp = false;
}
}
try
{
myRow["name"] = textBox1.Text.Trim();
myRow["phone"] = textBox2.Text.Trim();
myDataTable.Rows.Add(myRow);
OracleCommandBuilder myBuilder = new OracleCommandBuilder(myAdapter);
myAdapter.Update(myDataTable);
myDataTable.AcceptChanges();
MessageBox.Show("添加成功!");
textBox1.Text = "";
textBox2.Text = "";
}
catch
{
MessageBox.Show("输入信息有误!");
textBox1.Text = "";
textBox2.Text = "";
temp = false;
}
}
}
}
//删除部分代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
namespace delete
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string cnnString = "user id = xbh;data source = practice;password = xbh";
OracleConnection curConnection = new OracleConnection(cnnString);
try
{
curConnection.Open();
OracleCommand orac = curConnection.CreateCommand();
orac.CommandText = "delete from AddressList where name = '" + textBox1.Text + "'";
OracleDataReader myDataReader = orac.ExecuteReader();
MessageBox.Show("删除成功!");
textBox1.Text = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
curConnection.Close();
}
}
}
}
我的查询更新没有写好,谁能帮我写上啊,谢谢!还有添删该查时要注意那些异常情况,能帮我指出,写出,再次谢谢!