我收到以下错误
可访问性不一致:参数类型'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;
}
//当用户点击testbutton
时private void btnConnTest_Click(object sender, EventArgs e)
{
if (IsValidConnectionForPrinting())
{
MessageBox.Show("Connection succeeded", "Connection Test");
}
}
答案 0 :(得分:34)
如上所述here,如果未指定访问修饰符,则默认情况下类和结构是私有的。您将结构定义为:
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;
}
答案 1 :(得分:12)
您的ConnectionString
类型不是public
,但公共类的公共方法正在将其用作参数。您还必须输入类型public
,例如:
// If it's a class
public class ConnectionString { ... }