




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C# 与ORACLE直接连接代码private void Form1_Load(object sender, EventArgs e) string connectionString=Data Source=winner;User ID=system;Password=orcl;Unicode=True; OracleConnection oc = new OracleConnection(connectionString); oc.Open(); /写在具体的方法里面,上面两句,写在类里面。 string comsel = select * from product where product_types_id=1; OracleCommand ocom = new OracleCommand(comsel,oc); OracleDataReader reader = ocom.ExecuteReader(); string a=; if (reader.HasRows) if (reader.Read() a=(string)readername; /oracledatareader reader读取列值,如果为0,说明读取的是第一列; this.textBox1.Text = a; reader.Close(); oc.Close(); 注:小知识: 内部变量一定要赋值!比如定义在 类 内部的变量 如string a=; 不能写成string a; 因为a 为内部变量,否则会报错。第二种:使用connect,command,及 dataset和dataadapter来连接数据库。 string connectionString=Data Source=winner;User ID=system;Password=orcl;Unicode=True; OracleConnection oc = new OracleConnection(connectionString); oc.Open(); string comsel = select * from product ; OracleCommand ocom = new OracleCommand(comsel,oc); DataSet ds = new DataSet(); OracleDataAdapter oda = new OracleDataAdapter(); oda.SelectCommand = ocom; oda.Fill(ds,aa); /定义 ds中的一个表 名称为aa,来接受oda中经过 select 的值。 dgv.DataSource = ds.Tablesaa; oc.Close(); 第三种:使用 C#调用过程; (1) string connectionString = Data Source=winner;User ID=system;Password=orcl;Unicode=True; OracleConnection oc = new OracleConnection(connectionString); try oc.Open(); OracleCommand pcom = new OracleCommand(); pcom.CommandText = insert_pro; /定义 过程名; pcom.Connection = oc; pcom.CommandType = CommandType.StoredProcedure; /指定COMMAND调用 存储过程; /指定 ORACLE参数的 一个数组 去接受需要插入数据库的值。 new OracleParameter(p_id,OracleType.Number), new OracleParameter(p_name,OracleType.VarChar) ; op0.Direction = ParameterDirection.Input; /定义 变量的 类型为 IN op0.Value = Convert.ToInt32(this.txtid.Text); /定义 变量的值 由 textbox 文本框导入。 op1.Direction = ParameterDirection.Input; op1.Value = this.txtname.Text; pcom.Parameters.AddRange(op); /把定义好的变量,加入到 COMMAND中,供使用。 pcom.ExecuteNonQuery(); /一般进行DML操作时,都使用该方法,此方法没有返回值。 MessageBox.Show(success insert one account); catch (Exception eee) MessageBox.Show(eee.Message); /eee.message用来报 过程中写的错误。 finally oc.Close(); (2)过程 代码 create or replace procedure insert_pro(p_id in duct_types_id%type,p_name in %type ) is dis_num exception; /定义一个自定义错误 a number; begin select product_types_id into a from product where product_types_id=p_id; if(a0) then raise dis_num; else insert into product values(p_id,p_name); end if; exception when dis_num then raise_application_error(-20000,must be distinct id); when others then rollback; end insert_pro; 第四钟: 定义一个输入NAME, 返回值为DATASET值的 方法,可以直接和GRIDVIEW连接。 下面为这个方法: public DataSet select(string name) OracleConnection oc = new OracleConnection(a); oc.Open(); OracleCommand ocom = new OracleCommand(pack__emp, oc); ocom.CommandType = CommandType.StoredProcedure; OracleParameter op = new OracleParameter(pname,OracleType.VarChar); OracleParameter op1 = new OracleParameter(cur_emp, OracleType.Cursor); op.Direction = ParameterDirection.Input; op1.Direction = ParameterDirection.Output; op.Value = name; ocom.Parameters.Add(op); ocom.Parameters.Add(op1); DataSet ds = new DataSet(); OracleDataAdapter da = new OracleDataAdapter(); da.SelectCommand = ocom; da.Fill(ds, myselecttable); oc.Close(); return ds; 2010年01月29日1,在web.config中输入 连接字符串,把替换成: 即可在网页中调用代码:static string constr = ConfigurationManager.ConnectionStringsmycon.ConnectionString;OracleConnection oc = new OracleConnection(constr);接下来的不变。2,利用GridView进行分页显示。首先设置 GridView的属性:allowpaging = TRUE; pagesize设置为每页要显示的行数;然后 调用GridView的方法:pageindexchanging();protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) this.GridView1.PageIndex = e.NewPageIndex; /把新页的索引赋给PAGEINDEX。 grviewsel(); /为自定义的连接方法。 public void grviewsel() string constr = ConfigurationManager.ConnectionStringsmycon.ConnectionString; OracleConnection oc = new OracleConnection(constr); oc.Open(); string sqlstr = select names as 用户名,pass as 密码 from users; OracleCommand ocm = new OracleCommand(sqlstr,oc); OracleDataAdapter oda = new OracleDataAdapter(); oda.SelectCommand = ocm; DataSet ds = new DataSet(); oda.Fill(ds); this.GridView1.DataSource = ds; this.GridView1.DataBind(); 3,/ 利用pagedatasource类和viewstate属性,实现GRIDVIEW分页显示,并控制上下页。 public void datalistedbind(int index) string constr = ConfigurationManager.ConnectionStringsmycon.ConnectionString; OracleConnection oc = new OracleConnection(constr); oc.Open(); string sqlstr = select names as 用户名,pass as 密码 from users; OracleCommand ocm = new OracleCommand(sqlstr, oc); OracleDataAdapter oda =new OracleDataAdapter(); oda.SelectCommand=ocm; DataSet ds =new DataSet(); oda.Fill(ds); PagedDataSource pds = new PagedDataSource(); /页面级的数据源,能够存储当页的数据。 pds.DataSource = ds.Tables0.DefaultView; pds.AllowPaging = true; pds.PageSize = 5; pds.CurrentPageIndex = index; ViewStatecount = pds.Count; /pds.count自动计算页面的值,并赋给前者。 this.Label1.Text = 第 + (index + 1) + 页 共 + Convert.ToInt32(ViewStatecount) + 页; /控制 按钮的显示和隐藏达到控制显示的目的。 if (index = 0) this.GridView2.DataSource = pds; this.GridView2.DataBind(); this.btnnext.Enabled = true; this.btnback.Enabled = true; else this.btnback.Enabled = false; else this.btnnext.Enabled = false; this.btnback.Enabled = true; protected void btnback_Click(object sender, EventArgs e) ViewStateindex = Convert.ToInt32(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 肿瘤影像组学分析-洞察及研究
- 2025中级导游等级考试(汉语言文学知识)复习题及答案
- 2025年征兵心理纸笔测试试题及答案
- 航空燃油喷射工程中的环境影响分析-洞察及研究
- 农遗法律保护框架-洞察及研究
- 2025年度员工正式聘用合同协议
- 2025年度供货协议合同
- 德阳高二期末考试卷子及答案
- 出入境检验检疫
- 2025建筑混凝土用碎石采购合同
- GB/T 20801.6-2020压力管道规范工业管道第6部分:安全防护
- GB/T 19355.2-2016锌覆盖层钢铁结构防腐蚀的指南和建议第2部分:热浸镀锌
- 主编-孙晓岭组织行为学-课件
- 核心素养视角下教师专业发展课件
- 企业信用信息公告系统年度报告模板:非私营其他企业
- 施工员钢筋工程知识培训(培训)课件
- 质量管理体系审核中常见的不合格项
- 共用水电费分割单模板
- 《阿房宫赋》全篇覆盖理解性默写
- 学校体育学(第三版)ppt全套教学课件
- NCStudioGen6A编程手册
评论
0/150
提交评论