に動作していない私は、この機能が
private void btnConnect_Click(object sender, EventArgs e)
{
string localHost = "192.168.10.3";
string logInDetails = "gp";
//SqlConnection sConnection = new
//SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
try
{
//Checking for the Valid entries in textboxes.
if ((txtPassword.Text == logInDetails) && (txtUsername.Text == logInDetails))
//Checking for the appropriate local server address.
if (txtHost.Text == localHost)
{
BindDBDropDown();
SetOperationDropDown();
PrimaryKeyTable();
lblError.Text = "You are connected to the SQL Server....";
}
else
{
lblError.Text = "Invalid Credentials";
}
}
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
//{
// //To close the connection
// if (sConnection != null)
// {
// sConnection.Close();
// }
//}
}
のようなボタンのクリックイベントに関数を呼び出す必要がありBindDBDropDown関数定義は
public class DataAccessMaster
{
/// <summary>
/// This function gets the list of all the databases present in the local server.
/// </summary>
/// <returns></returns>
public static DataSet GetAllDataBaseNames()
{
SqlConnection sConnection = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
//To Open the connection.
sConnection.Open();
string selectDatabase = @"SELECT
[NAME]
FROM
[master..sysdatabases]";
SqlCommand sCommand = new SqlCommand(selectDatabase, sConnection);
try
{
DataSet dsListOfDatabases = new DataSet("master..sysdatabases");
SqlDataAdapter da = new SqlDataAdapter(selectDatabase, sConnection);
da.TableMappings.Add("Table", "master..sysdatabases");
da.Fill(dsListOfDatabases);
DataViewManager dsv = dsListOfDatabases.DefaultViewManager;
return dsListOfDatabases;
}
catch (Exception ex)
{
EventLog log = new EventLog("Application");
log.Source = "MFDBAnalyser";
log.WriteEntry(ex.Message);
return null;
}
finally
{
//To close the connection
if(sConnection != null)
{
sConnection.Close();
}
}
のような別のクラスであり、私はと呼ばれますこの
public void BindDBDropDown()
{
DataSet dsDatabaseList = default(DataSet);
try
{
//The function GetAllDataBaseNames() is called from the DataAccessMaster class.
dsDatabaseList = DataAccessMaster.GetAllDataBaseNames();
//Path to the combo box is given to get the value through the GetAllDataBaseNames().
cmbDatabases.DataSource = dsDatabaseList.Tables["master..sysdatabases"];
cmbDatabases.DisplayMember = "NAME";
cmbDatabases.ValueMember = ("");
}
しかし、それは、ドロップダウンに必要なリストをバインドされていないようにこの機能。 皆さん、私を助けてくれるのですか?
ドロップダウンリストで必要なリストを結合しないでみては?あなたのドロップダウンには何が表示されますか?それは例外をスローしますか?はいの場合は? – pavanred
これは何もバインドされておらず、例外もスローされません。 – Srivastava
btw、try/finally接続を閉じるのではなく、 'using'を使うほうがクリーンで堅牢です。 –