2012-01-01 78 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") 
相關問題