C#中oracle数据库的连接方法
一、关于数据库的操作
1.数据库连接
有2种:
第一种:古老的方法(较为死板,不利于灵活操作),即用OracleConnection的类来连接
string
mysqlstr ="user id = xal;data source = xal;password = xal";
OracleConnection mycnn = new OracleConnection(mysqlstr);
mycnn.open();
第二种:新式的方法(使用较为灵活),即利用OracleConnectoinStringBuilder类来连接
OracleConnectionStringBuilder OcnnStrB = new OracleConnectionStringBuilder;
OCnnStrB.DataSource = "xal";
OCnnStrB.UserID = "xal";
OCnnStrB.Password = "xal";
myCnn
= new OracleConnection(OCnnStrB.ConnectionString);
myCnn.open();
2.事务操作
myConn.open();
OracleCommand insertComm = new OracleCommand();
insertComm.Connection = myCnn;
insertComm.Transaction = myCnn.BeginTransaction();
try
{
事务操作语句;
insertComm.Transaction.Commit();
}
catch(exption ex)
{
insertComm.Transaction.Rollback();
MessageBox(ex.Message);
}
finally
{
myConn.close();
}
3.创建命令参数
private OracleParameter
CreateOraParam(string ParamName, object ParamValue)
{
OracleParameter Result = new OracleParameter();
Result.ParameterName = ParamName;
if
(ParamValue != null)
{
Result.Value = ParamValue;
}
else
{
Result.Value = DBNull.Value;
}
return
Result;
}
这样的话,当要对数据库操作时就可以:
insertComm.CommandText = "insert into TESTADODOTNET (ID, NAME, AGE, PIC)
values (:pID, :pName, :pAge, :pPic)";
insertComm.Parameters.Add(CreateOraParam("pID", (txtID.Text.Trim() !=
"") ? txtID.Text.Trim() : null));
insertComm.Parameters.Add(CreateOraParam("pName",
(txtName.Text.Trim() != "") ? txtName.Text.Trim() : null));
insertComm.Parameters.Add(CreateOraParam("pAge", (txtAge.Text.Trim()
!= "") ? txtAge.Text.Trim() : null));
4.数据集的浏览(例:将结果显示在comboBox1中)
OracleDataAdapter oda = new OracleDataAdapter(selectCommand);
DataTable newtable = new DataTable();
oda.Fill(newtable);
foreach (DataRow dr in
newtable.Rows) //共有newtable.rows.count条记录
{
comboBox1.Items.Add(dr[0].ToString());
}
5.设置输入只能是数字(例:现在往textBox1中输入。如只能输入字母的方法类似)
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)//属性中的事件
{
e.Handled = !((Char.IsNumber(e.KeyChar)) ||
((Keys)e.KeyChar == Keys.Back));
}
6.Form窗口关闭时引发的事件:弹出一个确定退出的对话框
private void form1_FormClosing(object sender, FormClosingEventArgs
e)
{
if
(MessageBox.Show("是否退出系统?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
DialogResult.Yes)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
7.OracleParameter的用法
第一步:先创建命令参数
private OracleParameter CreateOraParam(string
ParamName, object ParamValue)
{
OracleParameter Result = new OracleParameter();
Result.ParameterName = ParamName;
if
(ParamValue != null)
{
Result.Value = ParamValue;
}
else
{
Result.Value = DBNull.Value;
}
return
Result;
}
第二步:写SQL语句,并调用第一步的参数(例如::pID是个参数,代表调用insertComm.Parameters.Add中的pID的值)
insertComm.CommandText = "insert
into TESTADODOTNET (ID, NAME, AGE, PIC) values (:pID, :pName, :pAge,
:pPic)";
insertComm.Parameters.Add(CreateOraParam("pID",
(txtID.Text.Trim() != "") ? txtID.Text.Trim() : null));
insertComm.Parameters.Add(CreateOraParam("pName", (txtName.Text.Trim()
!= "") ? txtName.Text.Trim() : null));
insertComm.Parameters.Add(CreateOraParam("pAge", (txtAge.Text.Trim()
!= "") ? txtAge.Text.Trim() : null));
第三步:添加pictureBox1图片的二进制流字段pAge
//创建字节数组用于给IMAGE字段赋值,fileLength是指所选的文件的大小
byte[] tmpImage = new
byte[fileLength];
//根据字节数组创建内存流,之后对该流的操作将会影响字节数组的内容
MemoryStream curStream = new
MemoryStream(tmpImage);
//把控件内显示的图形写入到流中,需强制指定格式
pictureBox1.Image.Save(curStream, curImageFormat);//curImageFormat前面指定的图片格式
insertComm.Parameters.Add(CreateOraParam("pPic", tmpImage));
|
请发表评论