设为首页 加入收藏

TOP

.net连接oracle数据库---Shinepans(一)
2015-11-21 02:07:00 来源: 作者: 【 】 浏览:1
Tags:.net 连接 oracle 数据库 ---Shinepans

常见问题:

1.缺少引用 解决办法 ,添加引用:

\
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using Oracle.DataAccess;

2.连接字符串:

 globals.connectionString = "Data Source="+this.databaseName.Text.Trim()+";uid="+this.username.Text.Trim()+";pwd="+this.password.Text.Trim()+";";
            globals.username = this.username.Text.Trim();
            globals.databaseName = this.databaseName.Text.Trim();

这是我的一贯风格,喜欢用globals类管理通用的字符串.方便管理,通过上面的代码就可以知道连接字符串了.


3.查询:


\
查询格式如上

运行结果:

1.登录:
\

2.登录结果:
\

3.显示主界面 (测试)
\

4.查询数据库:


\


至此,查询功能已完成


关键点:


1.查询语句中,不要带分号,会提示关键字错误
2.引用要添加好:
using System.Data.Common;
using Oracle.DataAccess;
3.连接字符串要写好:
"Data Source=shinepans;uid=system;pwd=123;unicode=True"

上面的改写成你的


代码片:
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Net3._5_oracleTest
{
    static class Program
    {
        /// 
        /// 应用程序的主入口点。
        /// 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new LoginForm());
        }
    }
}


LoginForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Net3._5_oracleTest
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            globals.connectionString = "Data Source="+this.databaseName.Text.Trim()+";uid="+this.username.Text.Trim()+";pwd="+this.password.Text.Trim()+";";
            globals.username = this.username.Text.Trim();
            globals.databaseName = this.databaseName.Text.Trim();
            OracleConnection conn = new OracleConnection(globals.connectionString);
            try
            {
                conn.Open();
                MessageBox.Show("已连接到Oracle数据库!");
                MainForm mf = new MainForm();
                this.Hide();
                mf.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }finally
            {
                conn.Close();
            }
        }
    }
}


MainForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Net3._5_oracleTest
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            globals.connectionString = "Data Source="+this.databaseName.Text.Trim()+";uid="+this.username.Text.Trim()+";pwd="+this.password.Text.Trim()+";";
            globals.username = this.username.Text.Trim();
            globals.databaseName = this.databaseName.Text.Trim();
            OracleConnection conn = new OracleConnection(globals.connectionString);
            try
            {
                conn.Open();
                MessageBox.Show("已连接到Oracle数据库!");
                MainForm mf = new MainForm();
                this.Hide();
                mf.Show();
            }
            catch (Exception ex
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇解决Oracle11g空表无法导出的问题 下一篇7.oracle学习入门系列之七---网络..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: