Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在删除app.config文件中的硬编码值_C#_Visual Studio_Winforms - Fatal编程技术网

C# 正在删除app.config文件中的硬编码值

C# 正在删除app.config文件中的硬编码值,c#,visual-studio,winforms,C#,Visual Studio,Winforms,我已经开发了一个表单,从用户那里获取用户id和密码,并显示本地服务器中可用的数据库列表。现在我已经用硬编码格式完成了这项工作…如下所示 public void BindDBDropDown() { //Create the connection object SqlConnection sConnection = new SqlConnection( ConfigurationSettings.AppSettings["ConnectionString"]);

我已经开发了一个表单,从用户那里获取用户id和密码,并显示本地服务器中可用的数据库列表。现在我已经用硬编码格式完成了这项工作…如下所示

public void BindDBDropDown()
{
    //Create the connection object
    SqlConnection sConnection = new SqlConnection(
        ConfigurationSettings.AppSettings["ConnectionString"]);

    //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);

    try
    {
        //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;

        //Provides the master mapping between the sourcr table 
        //and system.data.datatable
        cmbDatabases.DataSource = 
            sDataset.Tables["master..sysdatabases"];
        cmbDatabases.DisplayMember = "NAME";
        cmbDatabases.ValueMember = ("NAME");
    }
    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);
    }
    finally
    {
        //If connection is not closed then close the connection
        if(sConnection.State != ConnectionState.Closed)
        {
            sConnection.Close();
        }
    }
}

/// <summary>
///This function binds the names of all the tables with primary 
///keys in a dropdown cmbResults.
/// </summary>
public void GetPrimaryKeyTable()
{
    //An instance of the connection string is created to manage
    //the contents of the connection string.
    var sqlConnection = new SqlConnectionStringBuilder();
    sqlConnection.DataSource = "192.168.10.3";
    sqlConnection.UserID = "gp";
    sqlConnection.Password = "gp";
    sqlConnection.InitialCatalog = 
        Convert.ToString(cmbDatabases.SelectedValue);
    string connectionString = sqlConnection.ConnectionString;

    SqlConnection sConnection = new SqlConnection(connectionString);

    //To Open the connection.
    sConnection.Open();

    //Query to select the table_names that have PRIMARY_KEYS.
    string selectPrimaryKeys = @"
        SELECT  TABLE_NAME 
        FROM    INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
        WHERE   CONSTRAINT_TYPE = 'PRIMARY KEY'
        AND     TABLE_NAME <> 'dtProperties'
        ORDER BY TABLE_NAME";

    //Create the command object
    SqlCommand sCommand = 
        new SqlCommand(selectPrimaryKeys, sConnection);

    try
    {
        //Create the dataset
        DataSet dsListOfPrimaryKeys = 
            new DataSet("INFORMATION_SCHEMA.TABLE_CONSTRAINTS");

        //Create the dataadapter object
        SqlDataAdapter sDataAdapter = 
            new SqlDataAdapter(selectPrimaryKeys, 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(dsListOfPrimaryKeys);

        //Bind the result combobox with primary key tables
        DataViewManager dvmListOfPrimaryKeys = 
            dsListOfPrimaryKeys.DefaultViewManager;
        dgResultView.DataSource = dsListOfPrimaryKeys
            .Tables["INFORMATION_SCHEMA.TABLE_CONSTRAINTS"];
    }
    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.Dispose();
        }
    }
}
public void binddbddropdown()
{
//创建连接对象
SqlConnection sConnection=新的SqlConnection(
ConfigurationSettings.AppSettings[“ConnectionString”];
//打开连接。
sConnection.Open();
//查询以选择数据库列表。
字符串选择数据库名称=
@“从MASTER..SYSDATABASES中选择名称”;
//创建命令对象
SqlCommand命令sCommand=
新建SqlCommand(选择DatabaseNames、sConnection);
尝试
{
//创建数据集
数据集sDataset=新数据集(“主..系统数据库”);
//创建dataadapter对象
SqlDataAdapter sDataAdapter=
新的SqlDataAdapter(选择DatabaseNames,sConnection);
sDataAdapter.TableMappings.Add(“表”,
“主..系统数据库”);
//填充数据集
sDataAdapter.Fill(sDataset);
//在组合框中绑定数据库名称
DataViewManager dsv=sDataset.DefaultViewManager;
//提供sourcr表之间的主映射
//和system.data.datatable
cmbDatabases.DataSource=
表[“master..sysdatabases”];
cmbDatabases.DisplayMember=“NAME”;
cmbDatabases.ValueMember=(“名称”);
}
捕获(例外情况除外)
{
//所有异常都被处理并写入事件日志。
EventLog logException=新事件日志(“应用程序”);
logException.Source=“MFDBAnalyser”;
logException.WriteEntry(例如Message);
}
最后
{
//如果连接未关闭,则关闭连接
if(sConnection.State!=ConnectionState.Closed)
{
sConnection.Close();
}
}
}
/// 
///此函数将所有表的名称与主表绑定
///在下拉列表中输入关键帧。
/// 
public void GetPrimaryKeyTable()
{
//将创建连接字符串的实例以管理
//连接字符串的内容。
var sqlConnection=new SqlConnectionStringBuilder();
sqlConnection.DataSource=“192.168.10.3”;
sqlConnection.UserID=“gp”;
sqlConnection.Password=“gp”;
sqlConnection.InitialCatalog=
Convert.ToString(cmbDatabases.SelectedValue);
字符串connectionString=sqlConnection.connectionString;
SQLConnectionsConnection=新的SqlConnection(connectionString);
//打开连接。
sConnection.Open();
//查询以选择具有主键的表名称。
字符串selectPrimaryKeys=@”
选择表格名称
来自信息\u SCHEMA.TABLE\u约束
其中约束类型='主键'
和表名称“dtProperties”
按表_名称排序”;
//创建命令对象
SqlCommand命令sCommand=
新建SqlCommand(选择PrimaryKeys、sConnection);
尝试
{
//创建数据集
数据集dsListOfPrimaryKeys=
新数据集(“信息模式表约束”);
//创建dataadapter对象
SqlDataAdapter sDataAdapter=
新的SqlDataAdapter(选择PrimaryKeys,sConnection);
//提供sourcr表之间的主映射
//和system.data.datatable
sDataAdapter.TableMappings.Add(“表”,
“信息模式表约束”);
//填充数据集
sDataAdapter.Fill(dsListOfPrimaryKeys);
//将结果组合框与主键表绑定
DataViewManager dvmListOfPrimaryKeys=
dsListOfPrimaryKeys.DefaultViewManager;
dgResultView.DataSource=dsListOfPrimaryKeys
.Tables[“信息模式.表约束”];
}
捕获(例外情况除外)
{
//所有异常都被处理并写入事件日志。
事件日志=新事件日志(“应用程序”);
log.Source=“MFDBAnalyser”;
log.WriteEntry(例如Message);
}
最后
{
//如果连接未关闭,则关闭连接
if(sConnection.State!=ConnectionState.Closed)
{
sConnection.Dispose();
}
}
}

有人能帮我删除这些硬编码的东西,并直接从app.config文件中获取本地服务器地址、用户ID和密码吗?

请在visual studio设置编辑器上查看视频教程:。

请在visual studio设置编辑器上查看视频教程:。

当然

首先,打开app.config或web.config文件

查看以下部分

<appSettings>
    <add key="...." value="....." />
    ....
</appSettings>

现在,您可以更改这些键/值的值(在app.config或web.config中),而无需编译代码:)

当然可以

首先,打开app.config或web.config文件

查看以下部分

<appSettings>
    <add key="...." value="....." />
    ....
</appSettings>

现在,您可以更改这些键/值的值(在app.config或web.config中),而无需编译代码:)


HTH

这可能会对您在
ConfigurationSettings.AppSettings[“ConnectionString”]
行中已经在进行的操作有所帮助。对于其他硬编码的东西,这样做有什么问题?这可能会对您在
ConfigurationSettings.AppSettings[“ConnectionString”]
行中已经在做的事情有所帮助。对于其他硬编码的东西,这样做有什么问题?首先使用系统配置。为了使最后一节更简单,试着这样读
sqlConnection.DataSource=ConfigurationSettings.AppSettings[“myServer”]首先使用系统配置。为了使最后一节更简单,试着这样读
sqlConnection.DataSource=ConfigurationSettings.AppSettings[“myServer”]
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <appSettings>  
      <add key="myServer" value="192.168.10.3" />
      <add key="myUserId" value="gp" />
      <add key="myPassword" value="gp" />
   </appSettings>
</configuration> 
sqlConnection.DataSource = "192.168.10.3";
sqlConnection.UserID = "gp";
sqlConnection.Password = "gp";
sqlConnection.DataSource = ConfiguationManager.AppSettings["myServer"];
sqlConnection.UserID = ConfiguationManager.AppSettings["myUserId"];
sqlConnection.Password = ConfiguationManager.AppSettings["myPassword"];