如何在中添加应用程序名称?

发布于 2024-12-23 15:59:02 字数 4744 浏览 0 评论 0原文

这是我的 app.config 文件,如下所示:

<?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 成员资格提供程序,这是我的用户表结构:

在我最近提出的问题这里Oded提供了 我试图找出问题所在我的 Insert 语句,我重新修改了它,我的数据库结构中有一个 ApplicationName 列,我需要指定它。(因为它不应该为空值)

现在我需要默认添加我的应用程序名称以输入数据库,因为我们为 web.config 执行此操作。

这就是我的意思。

在此处输入图像描述

我需要将该 MyApplication 添加到我的 app.config 文件中。

那么我该怎么做呢?

这就是我尝试将用户详细信息输入数据库的方式,但它没有输入单个值

 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

任何人都可以指出我在哪里犯了错误。

这是我在输入数据库时​​得到的最终结果:

在此处输入图像描述

为了更简短和清晰,我需要使用 HDI 会员资格提供商将上面显示的用户详细信息输入到我的数据库中。

This is my app.config file looks like:

<?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>

I am trying to use HDI membership provider and this is my Users table structure:

enter image description here

And in my recent question asked here Oded helped me out trying to figure the problem with my Insert statement and I re-modified it and I have an ApplicationName column in my database structure I need to specify it.(As it should not be null value)

Now I need to add my application name by default to enter to database as we do it for web.config.

This is what I mean.

enter image description here

I need to add that MyApplication to my app.config file.

So How do I do that?

This is how I'm trying to enter user details to database but it is not enetering a single value

 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

Can anyone point me out where I'm making mistake.

And this is the final result I'm getting while entering to database:

enter image description here

To be more short and clear I need to enter the above shown User details to my database using the HDI membership provider.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一指流沙 2024-12-30 15:59:02

有一种更好的方法,可以使用以下步骤:

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

2) 将以下块添加到 app.config 文件的 configuration 部分中:

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

3) 检索该值并在需要时通过以下代码使用它(所示答案中的示例):

param6.Value = System.Configuration.ConfigurationManager.AppSettings("ApplicationName")

There is a better way by using the following steps:

1) Add a reference to System.Configuration to your application.

2) Add the following block to your app.config file, within the configuration section:

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

3) Retrieve the value and use it where needed with the following code (example from your answer shown):

param6.Value = System.Configuration.ConfigurationManager.AppSettings("ApplicationName")
最终幸福 2024-12-30 15:59:02

将 AppSettings 部分添加到您的 web.config

<appSettings>

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

</appSettings>

Add a AppSettings section to your web.config

<appSettings>

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

</appSettings>
墨洒年华 2024-12-30 15:59:02

经过一番努力后,我自己解决了这个问题,并找到了解决方案,我以这种方式更改了我的代码:

 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)

我知道这不是最好的方法如果有人找到任何其他最佳解决方案,请让我知道。

I have figured out the problem Myself after a lot of hair pulling and found the solution that I have changed my code in this way:

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

And add another Param this way:

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

I know this not the best way If anyone found any other best solution let me know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文