0
我對填充包含從另一個組合框中選擇相應的數據庫,這樣在winform
///This function binds the names of all the tables without primary keys in a dropdown cmbResults.
public void GetNonPrimaryKeyTables()
{
//An instance of the connection string is created to manage the contents of the connection string.
var sqlConnection = new SqlConnectionStringBuilder();
//Declare the datasource,UserId,Password
sqlConnection.DataSource = "192.168.10.3";
sqlConnection.UserID = "gp";
sqlConnection.Password = "gp";
//Add the initial catalog to the connection string
sqlConnection.InitialCatalog = Convert.ToString(cmbDatabases.SelectedValue);
//Assign the ConnectionString value to a new variable
string connectionString = sqlConnection.ConnectionString;
//Create the connection object
SqlConnection sConnection = new SqlConnection(connectionString);
//To Open the connection.
sConnection.Open();
//Query to select table_names that doesn't have PRIMARY_KEY.
string selectNonPrimaryKeys = @"SELECT
TABLE_NAME
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE
CONSTRAINT_TYPE <> 'PRIMARY KEY'
ORDER BY
TABLE_NAME";
//Create the command object
SqlCommand sCommand = new SqlCommand(selectNonPrimaryKeys, sConnection);
try
{
//Create the dataset
DataSet dsListOfNonPrimaryKeys = new DataSet("INFORMATION_SCHEMA.TABLE_CONSTRAINTS");
//Create the dataadapter object
SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectNonPrimaryKeys, sConnection);
//Provides the master mapping between the sourcr table and system.data.datatable
sDataAdapter.TableMappings.Add("Table", "INFORMATION_SCHEMA.TABLE_CONSTRAINTS");
//Fill the dataset
sDataAdapter.Fill(dsListOfNonPrimaryKeys);
//Bind the result combobox with non primary key table names
DataViewManager dvmListOfNonPrimaryKeys = dsListOfNonPrimaryKeys.DefaultViewManager;
cmbResults.DataSource = dsListOfNonPrimaryKeys.Tables["INFORMATION_SCHEMA.TABLE_CONSTRAINTS"];
cmbResults.DisplayMember = "TABLE_NAME";
cmbResults.ValueMember = ("");
}
catch(Exception ex)
{
//All the exceptions are handled and written in the EventLog.
EventLog log = new EventLog("Application");
log.Source = "MFDBAnalyser";
log.WriteEntry(ex.Message);
}
finally
{
//If connection is not closed then close the connection
if(sConnection.State != ConnectionState.Closed)
{
sConnection.Close();
}
}
}
但是在組合框中的主鍵的所有表的列表中選擇桌面應用程序的工作在列表視圖填充值我應該這樣做,如果我需要根據從另一個下拉列表中選擇的數據庫,將列表視圖中的組合框替換爲填充相同項目的組合框。
你能幫我嗎?