【问题标题】:use connection with different forms c#使用不同形式的连接c#
【发布时间】:2018-09-23 08:38:56
【问题描述】:

我在 C# 中有一个表格说form1,用户必须将数据库名称用户名密码放在form1 的文本框中。建立连接后,form2 将打开,它将显示带有预定义 sql 的数据。还需要其他表格来使用相同的连接字符串。我怎样才能做到这一点。 我正在使用ODAC 连接到oracle 数据库。

这是代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;


namespace Sparrow1
{
    public partial class connectform : Form
    {
        public connectform()
        {
            InitializeComponent();                       
        }

        private OracleConnection conn = new OracleConnection();

        private void button1_Click(object sender, EventArgs e)
        {               
            conn.ConnectionString = "User Id=" + username.Text +
            ";Password=" + password.Text +
            ";Data Source=" + dataSource.Text + ";";

            try
            {
                conn.Open();
                button1.Enabled = false;
                statuslabel.Text = "Success";
                this.Hide();
                overviewform oviewform = new overviewform();
                oviewform.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                statuslabel.Text = "Failed";
            }
            finally
            {
                conn.Dispose();             
            }
        }
    }
}

【问题讨论】:

标签: c# .net oracle odac


【解决方案1】:

在 Form1.cs 中创建一个名为 connectionString 的公共静态字符串变量

public static string connectionString 

通过Form1.connectionString从其他表单中获取其值;

Form1.connectionString;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 2012-08-12
    • 2021-09-04
    • 1970-01-01
    • 2017-10-31
    • 2018-01-16
    • 1970-01-01
    相关资源
    最近更新 更多