首页 > 数据库 > Oracle > C#中oracle数据库的连接方法
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;
e.Cancel = true;
7.OracleParameter
的用法
第一步:先创建命令参数
第二步:写
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));
第三步:添加
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));
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/21107739/viewspace-1116727/ ,如需转载,请注明出处,否则将追究法律责任。
注册时间:2009-03-14