Description: The connection string to determine whether the user name and password is consistent, mainly want to use the configuration file to resolve the database connection problem
1 First built UI interface
2 Code
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceuserlogin{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidBtnlogin_click (Objectsender, EventArgs e) { //01 First determine if the user and password are empty stringUserName =TxtUserName.Text.Trim (); stringPWD =TxtPwd.Text.Trim (); if(String.IsNullOrEmpty (userName) | |string. IsNullOrEmpty (pwd)) {MessageBox.Show ("user name and password cannot be empty!"); return; } //01 Creating a connection String stringConnStr ="server=.; Uid=sa;pwd=sa;database=demodb;"; SqlConnection Conn=NewSqlConnection (CONNSTR); using(conn) {using(SqlCommand cmd =NewSqlCommand ()) {cmd. Connection=Conn; Conn. Open (); //Creating SQL Scripts stringSqlselect =string. Format ("SELECT * from UserInfo where EmpId = {0} and pwd= ' {1} '", USERNAME,PWD); Cmd.commandtext=Sqlselect; ObjectDatared=cmd. ExecuteScalar (); if(Datared! =NULL) {MessageBox.Show ("Login Successful"); } Else{MessageBox.Show ("incorrect user name and password"); } } } } }}
View Code
3 Operating Effect
4 Modify the connection string, where the specific instance is not lifted, if there are multiple connections to the database, each time you need to write the connection string. And it's a lot more difficult to change.
Method one we can encapsulate the connection string to realize the reusability of the code.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.Remoting.Messaging;usingSystem.Text;usingSystem.Threading.Tasks;namespaceuserlogin{ Public classdbconnectionstring { Public Static stringgetconnectionstring () {return "server=.; Uid=sa;pwd=sa;database=demodb;"; } }}
dbconnectionstring
Modify the code in the Form1, and you can call this method in all future classes
01 Creating a connection string
String connstr = Dbconnectionstring.getconnectionstring ();
5 In fact, this method is also a lot of unfavorable, the most commonly used is still through the configuration file
5.1 Modify configuration file, add <connectionStrings> node
<?xml version="1.0"encoding="Utf-8"?><configuration> <connectionStrings> <add name="ConnStr"connectionstring="Data source=127.0.0.1;uid=sa;pwd=sa;initial Catalog=demodb;"/> </connectionStrings> <startup> <supportedruntime version="v4.0"sku=". netframework,version=v4.5"/> </startup></configuration>
View Code
5.2 Add System.Configuration Reference (ASP. NET adds automatically)
5.3 Modify code, use ConfigurationManager to get the link string.
01 Creating a connection string
String connstr = configurationmanager.connectionstrings["ConnStr"]. ConnectionString;
Step by step -38-simple Login Form