14

我收到以下错误

可访问性不一致:参数类型“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");
    }
}
4

2 回答 2

35

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

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;
}
于 2012-04-09T09:13:13.243 回答
13

您的ConnectionString类型不是public,但是,公共类的公共方法正在使用它作为参数。您还必须创建 type public,例如:

// If it's a class
public class ConnectionString { ... }
于 2012-04-09T06:08:30.950 回答