public string constr = "數據庫連接字符串";
//登陸
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.寫BLL層。給BLL層里面寫登陸的方法和添加的方法
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.創建一個主窗體,右擊–》添加–》windows窗體–》FrmMain.–》拉四個Button,分別是添加信息,查詢信息,修改信息,刪除信息 13.雙擊添加信息進去,寫代碼: //打開添加的窗體 addCity ac = new addcity(); ac.Show(); this.Hide(); 14.拉一個添加的窗體。雙擊添加按鈕進去寫代碼:
//獲取值City city = new City();city.Name = this.name.Text;city.Password = this.Password.Text;city.Address = this.Address.Text;city.Phone = this.Phone.Text;//調用bll里面添加方法int rel = cbll.AddCity(city);if(rel>0){MessageBox.Show("添加成功");}else{MessageBox.Show("添加失敗");}