开发者

How to save the input values?

Using VB.Net

Database Form

Server Name, Username, Password - textbox
SQL Authentication, windows Authentication - checkbox

I have Database Form, First Time i run my software, I have to give Server Name, Window or SQL Authentication mode, UserName and password. Next Time I run the software, given data's like Server name, username, password, window or sql authentication should appear in the form.

Before I used VB6, I used the ini file for getting the username, password and servername.

vb6 code.

Dim File As String, OFLen As Double, str As String
File = App.Path & "开发者_StackOverflow\SQLServer.ini"
OFLen = FileLen(File)
SName = ReadIni(File, "Server", "ServerName")
UName = ReadIni(File, "UserName", "UName")
PWord = ReadIni(File, "Password", "PWord")

Dim ConnectionString As String
Set DLTConn = New ADODB.Connection
ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI; Persist Security Info=False;Initial Catalog=database1;Data Source=" & SName & ""
DLTConn.Open ConnectionString

There is any option is available in the vb.net for saving the data's or i have to use the same format(ini file) in vb.net

Need vb.net code Help.


These instructions assume VB2008 but I believe that 2010 is similar.

As @treaschf said, right click on your project in the Solution Explorer and select properties. If a window pops up with a title "Solution '...' Property Pages" then you clicked on the solution instead of the project. In the property window click on Settings. In the name column enter "Username", leave the type as String, the scope as User and the value as empty (unless you want to enter a default). Repeat this with each property that you want to store. These steps automatically create variables that you can access using My.Settings..

So in your code-behind you can do:

My.Settings.Username = "bob"

And:

Dim username as String = My.Settings.Username

I believe that settings will automatically be saved on exit but I recommend making an explicit call after updating just in case.

My.Settings.Save()

So that will pretty much do the same as you INI file used to do. But like @treaschf said, you should really encrypt the password when saving to disk. Below is a modified version of the routine found here. http://weblogs.asp.net/jgalloway/archive/2008/04/13/encrypting-passwords-in-a-net-app-config-file.aspx . I removed the System.Security.SecureString because as nice as it is it also makes life more complicated. Change the value of Entropy to anything you want, call Encrypt(string) to encrypt your text and Decrypt(string) to decrypt it. This uses DPAPI so you don't have to worry about messing with the registry, permissions and everything else.

Private Shared entropy As Byte() = System.Text.Encoding.Unicode.GetBytes("Enter some text here for entropy")
Public Shared Function EncryptString(ByVal text As String) As String
    Dim data = System.Security.Cryptography.ProtectedData.Protect(System.Text.Encoding.Unicode.GetBytes(text), _
                                                                     entropy, _
                                                                     System.Security.Cryptography.DataProtectionScope.CurrentUser)
    Return System.Convert.ToBase64String(data)
End Function
Public Shared Function DecryptString(ByVal encryptedData As String) As String
    Dim data As Byte() = System.Security.Cryptography.ProtectedData.Unprotect(System.Convert.FromBase64String(encryptedData), _
                                                                                  entropy, _
                                                                                  System.Security.Cryptography.DataProtectionScope.CurrentUser)
    Return System.Text.Encoding.Unicode.GetString(data)
End Function

And then to put it all together:

        My.Settings.Username = EncryptString("bob")

    Dim username As String
    If Not String.IsNullOrEmpty(My.Settings.Username) Then
        Try
            username = DecryptString(My.Settings.Username)
        Catch ex As Exception
            'There was a problem decrypting the username
        End Try
    End If

    Trace.WriteLine(username)


If you right-click on your project in Solution Explorer, and chose Properties, on the properties page there will be a Settings tab, in which you define the settings of your application.

These settings will be stored in an XML file, near the executable of your application. (Or in case of user-settings, in the C:\Users\username... directory.

If you name your connection string setting as DbConnectionString, you will be able to access it from code like this:

 My.Settings.DbConnectionString

However if you need to store user names and passwords, I would recommend you to store the settings in the Windows registry, and encrypt them with the System.Security.Cryptography.ProtectedData class. (Storing the password unencrypted is not recommended.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜