public string constr = "數(shù)據(jù)庫(kù)連接字符串";
//登陸
public bool Login(string name,string password){string sql = "select * from city where name = @name and password = @password";SqlParameter [] param = new Sqlparameter[]{new Sqlparameter("@name",name),new SqlParameter("@password",password)};using(SqlConnection conn = new SqlConnection(constr)){SqlCommand cmd = new SqlCommand (sql,conn);cmd.Parameters.AddRange(param);conn.Open();SqlDataReader dr = cmd.ExecuteReader();if(dr.Read()){dr.Close();conn.Close();return true;}else{dr.Close();conn.Close();return false;}}
}
//添加的方法
public int AddCity(City city ){string sql="insert into city values(@name,@password,@address,@phone)";SqlParameter [] param = new Sqlparameter[]{
new Sqlparameter("@name",city.name),
new SqlParameter("@password",city.password),
new SqlParameter("@address",city.address),
new SqlParameter("@phone",city.phone)};using(SqlConnection conn = new SqlConnection(constr)){SqlCommand cmd = new SqlCommand(sql,conn);cmd.Parameters.AddRange(param );conn.Open();return cmd.ExecuteNonQuery();}
}
9.寫(xiě)B(tài)LL層。給BLL層里面寫(xiě)登陸的方法和添加的方法
public CityDLL cdll = new CityDLL();
//登陸
public bool Login(string name ,string password){return cdll.Login(name,password);
}
//添加
public int AddCity(City city){return cdll.AddCity(city);
}
public CityBLL cbll = new CityBLL();//獲取值
string name = this.Name.Text;
string password = this.Password.Text;
if(cbll.Login(name,password)){MessageBox.Show("登陸成功");FrmMain fm = new FrmMain();fm.Show();this.Hide();
} else{MessageBox.Show("登陸失敗");
}
12.創(chuàng)建一個(gè)主窗體,右擊–》添加–》windows窗體–》FrmMain.–》拉四個(gè)Button,分別是添加信息,查詢(xún)信息,修改信息,刪除信息 13.雙擊添加信息進(jìn)去,寫(xiě)代碼: //打開(kāi)添加的窗體 addCity ac = new addcity(); ac.Show(); this.Hide(); 14.拉一個(gè)添加的窗體。雙擊添加按鈕進(jìn)去寫(xiě)代碼:
//獲取值City city = new City();city.Name = this.name.Text;city.Password = this.Password.Text;city.Address = this.Address.Text;city.Phone = this.Phone.Text;//調(diào)用bll里面添加方法int rel = cbll.AddCity(city);if(rel>0){MessageBox.Show("添加成功");}else{MessageBox.Show("添加失敗");}