C#如何链接本地Oracle数据库 ConnectionString & BadImageError报错

如果你已经创建好了用户名密码、并且已经有了一个可以调用的table,直接跳到第3部分

1. 创建数据库用户名密码

Create the user

create user TENANT01
identified by “”
default tablespace USERS
temporary tablespace TEMP
profile DEFAULT
password expire;

Grant/Revoke role privileges

grant aq_administrator_role to TENANT01 with admin option;
grant dba to TENANT01 with admin option;
grant mgmt_user to TENANT01;

Grant/Revoke system privileges

grant unlimited tablespace to TENANT01 with admin option;

2. 创建一个table

create table userinfo(
userid varchar(48),
userpassword varchar(48),
crdt date,
cruser varchar(48));

录入信息

insert into userinfo(
userid, userpassword, crdt, cruser)
values(,,sys.date(),created_user);

3. VS里修改App.config

<configuration>
	
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    startup>
	<connectionStrings>
		<add name="ConnectionString" connectionString="Data Source="(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL=TCP)(HOST=localhost)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME=orcl)))";Persist Security Info=True;User ID=Your_user_name;Password=Your_Password;Unicode=True" providerName="System.Data.OracleClient" />
	connectionStrings>
configuration>

需要改动userId和Password部分,改成你自己的Oracle登录信息

4. C#语句

上面这些using是需要调用的库:
function主要看那个button_click,里面是connect to database

using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Collections;
using System.Reflection;
using System.Configuration;
using System.Data.Common;```

namespace WpfApp3
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var currPassword = passwordInput.Password;
            var currUsername = usernameInput.Text;


            string name = "ConnectionString";
            ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings[name];
            DbProviderFactory m_Factory = DbProviderFactories.GetFactory(cs.ProviderName);
            DbConnection m_conn = m_Factory.CreateConnection();
            m_conn.ConnectionString = cs.ConnectionString;
            
            m_conn.Open();
            MessageBox.Show("success");
            #成功了的话就会出现“success”消息框
            m_conn.Close();

        }
    }
}

5. 会遇到的问题:32bit和64bit不匹配 BadImageError报错

先看自己pl/sql是不是64bit

怎么看呢?最简单的方法就是打开task manager 任务管理,看看pl/sql是不是后面有32bit.如果没有显示是32bit那就是64bit
C#如何链接本地Oracle数据库 ConnectionString & BadImageError报错_第1张图片
如果自己的

检查自己解决方案project的属性 是不是32bit优先

最上面的项目(project)tab,里最底下的Properties - Build
看看是不是勾选了“prefer 32bit”
并且把platform target改成64bit
这个问题困扰了我一个小时,才找到最后的原因 …
C#如何链接本地Oracle数据库 ConnectionString & BadImageError报错_第2张图片

如果还没有解决,可以看看其他博主~~

你可能感兴趣的:(数据库)