可访问性不一致

本文关键字:不一致 访问 | 更新日期: 2023-09-27 18:30:39

我收到以下错误

可访问性不一致:参数类型"Db.Form1.ConnectionString"比方法"Db.Form1.BuildConnectionString(Db.Form1.ConnectionString)"更难访问

//Name spaces
using System;
using System.Collections.Generic;
using System.ComponentModel;
 using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
 using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Collections;
using System.Diagnostics;
using System.Data.OleDb;
using System.IO;
using System.Drawing.Printing;
 //
  namespace Db
{
  public partial class Form1 : Form
  {
     public Form1()
     {
        InitializeComponent();
     }
     public void SetBusy()
    {
        this.Cursor = Cursors.WaitCursor;
        Application.DoEvents();
    }
    public void SetFree()
    {
        this.Cursor = Cursors.Default;
        Application.DoEvents();
    }
   //connection string into parts
    struct ConnectionString
    {
        public string Provider;
        public string DataSource;
        public string UserId;
        public string Password;
        public string Database;
    }
  //Declare
    public string BuildConnectionString(ConnectionString connStr) ------> getting error here
    {
        string[] parts = new string[5];
        parts[0] = "Provider=" + connStr.Provider;
        parts[1] = "Data Source=" + connStr.DataSource;
        parts[2] = "User Id=" + connStr.UserId;
        parts[3] = "Password=" + connStr.Password;
        parts[4] = "Initial Catalog=" + connStr.Database;
        return string.Join(";", parts);
    }
    // settings 
    public bool IsValidConnectionForPrinting()
    {
        SetBusy();
        ConnectionString connStr = new ConnectionString();
        connStr.Provider = cboProvider.Text;
        connStr.DataSource = cboDataSource.Text;
        connStr.UserId = txtUserId.Text;
        connStr.Password = txtPassword.Text;
        connStr.Database = cboDatabase.Text;
       //connection string to database 
        string connectionString = BuildConnectionString(connStr);
        OleDbConnection conn = new OleDbConnection(connectionString);
        try
        {
            conn.Open();
            OleDbCommand cmd = conn.CreateCommand;
            cmd.CommandType = CommandType.TableDirect;
            cmd.CommandText = "vw_pr_DL";
            cmd.ExecuteScalar();
            cmd.CommandText = "vw_pr_VR";
            cmd.ExecuteScalar();
            //cmd.CommandText = "vw_pr_VR"
            //cmd.ExecuteScalar()
            conn.Close();
        } 
   //Exception messages
        catch (Exception ex)
        {
            SetFree();
            if (ex.Message.StartsWith("Invalid object name"))
            {
                MessageBox.Show(ex.Message.Replace("Invalid object name", "Table or view not found"), "Connection Test");
            }
            else
            {
                MessageBox.Show(ex.GetBaseException().Message, "Connection Test");
            }
            return false;
        }
        SetFree();
        return true;
    }

当用户单击测试按钮时

private void btnConnTest_Click(object sender, EventArgs e)
{
if (IsValidConnectionForPrinting())
    {
         MessageBox.Show("Connection succeeded", "Connection Test");
    }
}

可访问性不一致

如此处所述,如果未指定访问修饰符,则类和结构默认为私有。将结构定义为:

struct ConnectionString
{
    public string Provider;
    public string DataSource;
    public string UserId;
    public string Password;
    public string Database;
}

您需要将其定义为:

public struct ConnectionString
{
    public string Provider;
    public string DataSource;
    public string UserId;
    public string Password;
    public string Database;
}

您的ConnectionString类型不是public,但是,公共类的公共方法将其用作参数。您还必须使类型public,例如:

// If it's a class
public class ConnectionString { ... }