2012-01-01 27 views
1

这是我的app.config文件看起来像:如何将应用程序名称添加到<appSettings />?

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <appSettings> 
     <add key="Application Name" value="/MyApplication" /> 
    </appSettings> 
    <connectionStrings> 
    <add name="frmStartup.My.MySettings.HDIMembershipProviderConnectionString" 
     connectionString="Data Source=.\sqlexpress;Initial Catalog=HDIMembershipProvider;Integrated Security=True" 
     providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
    <system.web> 
    <membership defaultProvider="HDIMembershipProvider"> 
     <providers> 
     <clear/> 
     <add name="HDIMembershipProvider" type="MyApplication.HDIMembershipProvider, MyApplication"/> 
     </providers> 
    </membership> 
    </system.web> 
    <system.diagnostics> 
     <sources> 
      <!-- This section defines the logging configuration for My.Application.Log --> 
      <source name="DefaultSource" switchName="DefaultSwitch"> 
       <listeners> 
        <add name="FileLog"/> 
        <!-- Uncomment the below section to write to the Application Event Log --> 
        <!--<add name="EventLog"/>--> 
       </listeners> 
      </source> 
     </sources> 
     <switches> 
      <add name="DefaultSwitch" value="Information" /> 
     </switches> 
     <sharedListeners> 
      <add name="FileLog" 
       type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" 
       initializeData="FileLogWriter"/> 
      <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log --> 
      <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> --> 
     </sharedListeners> 
    </system.diagnostics> 
</configuration> 

我试图用HDI会员供应商,这是我的用户表结构:

enter image description here

在我最近的问题问hereOded帮我解决了这个问题,我重新修改了它,并在我的数据库结构中有一个ApplicationName列,我需要指定它(因为它不应该是空值)

现在我需要添加我的应用程序名称默认情况下进入数据库,因为我们这样做的web.config。

这就是我的意思。

enter image description here

我需要的是所有MyApplication添加到我的app.config文件。

那么我该怎么做?

这是我正在试图进入用户的详细情况数据库,但它不enetering单个值

Try 
      Dim connectionString As String = "Data Source=.\sqlexpress;Initial Catalog=HDIMembershipProvider;Integrated Security=True" 
      Using cn As New SqlConnection(connectionString) 
       cn.Open() 
       Dim cmd As New SqlCommand() 
       cmd.CommandText = "INSERT INTO Users (Username, Password, Email, PasswordQuestion, PasswordAnswer) VALUES(@Username,@Password,@Email,@PasswordQuestion,@PasswordAnswer)" 

       Dim param1 As New SqlParameter() 
       param1.ParameterName = "@Username" 
       param1.Value = txtUsername.Text.Trim() 
       cmd.Parameters.Add(param1) 

       Dim param2 As New SqlParameter() 
       param2.ParameterName = "@Password" 
       param2.Value = txtPassword.Text.Trim() 
       cmd.Parameters.Add(param2) 


       Dim param3 As New SqlParameter() 
       param3.ParameterName = "@Email" 
       param3.Value = txtEmail.Text.Trim() 
       cmd.Parameters.Add(param3) 

       Dim param4 As New SqlParameter() 
       param4.ParameterName = "@PasswordQuestion" 
       param4.Value = txtSecurityQuestion.Text.Trim() 
       cmd.Parameters.Add(param4) 

       Dim param5 As New SqlParameter() 
       param5.ParameterName = "@PasswordAnswer" 
       param5.Value = txtSecurityAnswer.Text.Trim() 
       cmd.Parameters.Add(param5) 

       cmd.Connection = cn 
       cmd.ExecuteNonQuery() 
       cn.Close() 
      End Using 
      Successlbl.show 
      Successlbl.show.Text = "Regisration Success." 
     Catch 
      Errolbl.Show() 
      Errolbl.Text = "Your account was not created.Please try again." 
     End Try 

任何人都可以点我在哪里,我会犯错。

这是最终的结果,而进入到数据库我得到:

enter image description here

更短,明确我需要输入上面显示用户的详细情况使用HDI会员我的数据库供应商。

+4

@ Downvoter,而不是把的 - 1你能否提供一些信息,以便我可以尝试改进我的问题,以便更好地理解它。 – coder 2012-01-01 10:36:56

+0

(没有downvote)目前还不清楚你的实际问题是什么。你需要从你的(winforms)应用程序中修改app.config,还是在构建或部署应用程序时修改? – Filburt 2012-01-01 10:42:20

+0

当我将应用程序部署为当我尝试创建新用户时,值不会输入到我的数据库中,因为最近我在这里提出了问题http://stackoverflow.com/questions/8692584/inserting-data-into-sqlserver-database-using -vb-net/8692611#8692611.So现在提到的应用程序路径是根据我的HDI成员资格提供程序手动输入的,所以我需要在app.config文件中提及我的应用程序名称。 – coder 2012-01-01 10:44:46

回答

0

我有很多的头发拉后想通了自己的问题,我发现我用这种方式已经改变了我的代码的解决方案:

cmd.CommandText = "INSERT INTO Users (Username,ApplicationName,Password, 
    Email, PasswordQuestion, PasswordAnswer) VALUES(@Username,@ApplicationName,@Password, 
    @Email,@PasswordQuestion,@PasswordAnswer)" 

并添加另一个帕拉姆这样:

Dim param6 As New SqlParameter() 
param6.ParameterName = "@ApplicationName" 
param6.Value = txtApplicationName.Text.Trim() 
cmd.Parameters.Add(param6) 

我知道这不是如果最好的方法任何人发现任何其他最佳解决方案让我知道。

1

添加appSettings部分,以你的web.config

<appSettings> 

<add key="ApplicationName" value="/website1" /> 

</appSettings> 
+0

@ user188308-尽管我已经提供了应用程序名称,但已尝试过,但无法正常工作。 – coder 2012-01-01 11:09:06

+0

+ 1 - 这是我在app.config文件中的错误 – coder 2012-01-01 19:52:40

3

有通过以下步骤更好的办法:

1)System.Configuration引用添加到您的应用程序。

2)将以下块添加到您的应用程序。配置文件中,configuration部分中:

<appSettings> 
    <add key="ApplicationName" value="/MyApplication" /> 
    </appSettings> 

3)检索值,并用它在那里与显示的代码(例如,从你的答案)需要:

param6.Value = System.Configuration.ConfigurationManager.AppSettings("ApplicationName") 
相关问题