C# 控件不从其他类继承
我在一个类中定义了这样一个函数:C# 控件不从其他类继承,c#,.net,winforms,C#,.net,Winforms,我在一个类中定义了这样一个函数: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; using System.Configuration; using System.Diagnostics; using MFDBAnal
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Configuration;
using System.Diagnostics;
using MFDBAnalyser;
namespace MFDBAnalyser
{
public class DataAccessMaster:MFDBAnalyser
{
// /// <summary>
// /// This function gets the list of all the databases present in the local server.
// /// </summary>
// /// <returns></returns>
public static DataSet GetAllDataBaseNames()
{
SqlConnectionStringBuilder objConnectionString = new SqlConnectionStringBuilder();
objConnectionString.DataSource = txtHost.Text;
objConnectionString.UserID = txtUsername.Text;
objConnectionString.Password = txtPassword.Text;
SqlConnection sConnection = new SqlConnection(objConnectionString.ConnectionString);
//If connected then give this message to user
lblMessage.Visible = true;
lblMessage.Text = "You are connected to the SQL Server....";
try
{
//To Open the connection.
sConnection.Open();
//Query to select the list of databases.
string selectDatabaseNames = @"SELECT
NAME
FROM
[MASTER]..[SYSDATABASES]";
//Create the command object
SqlCommand sCommand = new SqlCommand(selectDatabaseNames, sConnection);
//Create the data set
DataSet sDataset = new DataSet("master..sysdatabases");
//Create the dataadapter object
SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectDatabaseNames, sConnection);
sDataAdapter.TableMappings.Add("Table", "master..sysdatabases");
//Fill the dataset
sDataAdapter.Fill(sDataset);
//Bind the database names in combobox
DataViewManager dsv = sDataset.DefaultViewManager;
}
catch(Exception ex)
{
//All the exceptions are handled and written in the EventLog.
EventLog logException = new EventLog("Application");
logException.Source = "MFDBAnalyser";
logException.WriteEntry(ex.Message);
MessageBox.Show("Login Failed!!", "Error Occured");
}
finally
{
//If connection is not closed then close the connection
if(sConnection.State != ConnectionState.Closed)
{
sConnection.Close();
}
}
}
}
}
但是显示了一个错误,比如TXTOST etx不存在,当我将designer.cs
类的受保护修饰符更改为public
时,它也显示了错误
有人能告诉我发生了什么吗?请验证txtHost的修饰符是否也是受保护的或公共的。U无法获取其他类中某个窗体的值。尝试使用参数。或者使用
properties{get;set;}
访问这些值。我想你可以使用一些属性并指定textbox的值。如果您在winforms中执行此操作,则可以使用静态变量。但在web中,您必须传递参数。假设您的文本框是在MFDBAnayser类中定义的,您仍然无法在GetAllDataBaseNames函数中访问它们。GetAllDataBaseNames是一个静态函数,因此无法访问文本框或其他控件等实例变量
public void BindDBDropDown()
{
DataSet dsTablesWithoutForeignKeys = default(DataSet);
try
{
//The function GetAllForeignKeyTables() is called from the class PluginManager.
dsTablesWithoutForeignKeys = DataAccessMaster.GetAllDataBaseNames();
cmbDatabases.DisplayMember = "TABLE_NAME";
cmbDatabases.ValueMember = "";
cmbDatabases.DataSource = dsTablesWithoutForeignKeys.Tables["master..sysdatabases"];
}
catch(Exception ex)
{
//All the exceptions are handled and written in the EventLog.
EventLog logException = new EventLog("Application");
logException.Source = "MFDBAnalyser";
logException.WriteEntry(ex.Message);
}
}