WPF implementation of a database connectivity test

Source: Internet
Author: User

The functionality to be implemented is as follows: because we want to test the data from the input box, our connection string is not written to die in the application. Let me explain in more detail below.

First, we need to find the project--"properties->setiings.setting

Create a new connection string name, type string, and focus on the scope must be set to the user , so that we can rewrite base. Overwrites the last connection string on an inherited basis. The connection string will not be changed if we do not set it to the user and the application is received.

In the EF framework, we have a database table mapping for each new DataContext. Its logic is that when we new (this example DataContext is stockmanagedatacontext) will execute

     PublicStockmanagedatacontext ():Base(Global::D ataModal.Properties.Settings.Default.StockManageSystemConnectionString, Mappingsource) {Console.WriteLine (Global::D ataModal.Properties.Settings.Default.StockManageSystemConnectionString);    Oncreated (); }                 PublicStockmanagedatacontext (stringconnection):Base(connection, Mappingsource) {oncreated (); }                 PublicStockmanagedatacontext (System.Data.IDbConnection connection):Base(connection, Mappingsource) {oncreated (); }                 PublicStockmanagedatacontext (stringconnection, System.Data.Linq.Mapping.MappingSource Mappingsource):Base(connection, Mappingsource) {oncreated (); }                 PublicStockmanagedatacontext (System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource Mappingsource):Base(connection, Mappingsource) {oncreated (); }
Global::D ataModal.Properties.Settings.Default.StockManageSystemConnectionString, Mappingsource)  In this code is the connection string that can be overwritten, and we just need to change him on the line.

Connection test code coming up.
         Public BOOLconnectiontest () {//get the connection string            stringConnectionString ="Data source="+ Server.text +"; Initial catalog="+ Sqlname.text +"; Persist Security info=true; User id="+ Account.text +"; password="+Pass.text; SqlConnection _SqlConnection=NewSqlConnection (ConnectionString); Try{_sqlconnection.open (); SqlCommand sqlcmd=_sqlconnection.createcommand (); Sqlcmd.commandtext="Select Top Ten * from ProductInfo;"; //Save the database connection string if you can connect successfullyDataModal.Properties.Settings.Default.StockManageSystemConnectionString =ConnectionString; DataModal.Properties.Settings.Default.Save ();//save changes using the Save method NMessageBox.Show ("Test Connection OK","Congratulations"); return true; }            Catch(Exception ex) {MessageBox.Show ("cannot connect to database, please reset","Warning"); return false; }            finally{_sqlconnection.close (); }        }

This makes it possible to test and verify it before each login.

There is a more humane question, that is, users want to have a few database connection before each login default value, after all, we do not want to go to enter so much data, but can not directly write dead in the input box (so too low), so, since we can assign values to the connection string, We can also get the last connection string, split it, and load it into the form input box by default when the form is initialized.

Code: (Obtained based on key value pairs)

         Public voidinnital () {stringConnect =DataModal.Properties.Settings.Default.StockManageSystemConnectionString; Dictionary<string,string> dictionary = Connect. Split (New Char[] {';'}, Stringsplitoptions.removeemptyentries). ToDictionary (x = X.split ('=')[0], x = X.split ('=')[1]); List<string> test =Newlist<string>(dictionary.            Values);  for(inti =0; I < test. Count; i++) {Server.text= test[0]; Sqlname.text= test[1]; Pass.text= test[4]; Account.text= test[3]; }        }



WPF implementation of a database connectivity test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.