private void LbLogin_MouseDown(object sender, MouseButtonEventArgs e)
{
//MessageBox.Show("登陆");
using (SqlConnection conn = new SqlConnection(@"Data Source=192.168.1.130; Initial Catalog=HealthCard;User ID=sa;Password=stevev"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
//1.执行非查询语句
//cmd.CommandText = "Insert into T_Users(U_UserName,U_Password,U_Name) values('test','test','test')";
//cmd.ExecuteNonQuery();////2.ExecuteScalar()获得插入的自增字段值
//cmd.CommandText = "insert into T_Users(U_UserName,U_Password) output inserted.U_ID values('test','test')";
////cmd.ExecuteScalar()用于只有一行一列的返回值获取。
//long i = (long)cmd.ExecuteScalar();
//MessageBox.Show(i.ToString());////3.SqlDataReader()查询多条语句结果。
//cmd.CommandText = "select * from T_Users";
//using (SqlDataReader reader = cmd.ExecuteReader())
//{
// while (reader.Read())
// {
// string name=reader.GetString(1);
// MessageBox.Show(name);
// }
//}
////4.参数化查询避免注入
////当仅仅是用sql语句做拼接字符串的,输入1'or'1'='1 会显示出所有数据。
//cmd.CommandText = "select U_ID from T_Users where U_UserName=@name";
//cmd.Parameters.Add(new SqlParameter("@name", TbUserName.Text));
//using (SqlDataReader reader = cmd.ExecuteReader())
//{
// while (reader.Read())
// {
// long ID = reader.GetInt64(0);
// MessageBox.Show(ID.ToString());
// }
//}
//5.DataSet离线数据集-优点:降低服务器压力,编程也简单了。但数据量大对客户端内存压力大。cmd.CommandText = "select * from T_Users";
//SqlDateAdapter是一个帮助我们把SqlCommand查询结果填充到DataSet中的类
SqlDataAdapter adapter =new SqlDataAdapter(cmd);
//DataSet相当于本地的数据集合
DataSet dataset =new DataSet();
//把SqlCommand查询结果填充到DataSet
adapter.Fill(dataset);DataTable table=dataset.Tables[0];
DataRowCollection rows=table.Rows;
for(int i =0;i<rows.Count;i++)
{
DataRow row = rows[i];
long ID = (long)row["U_ID"];
string name = (string)row["U_UserName"];
MessageBox.Show(ID + "," + name);}
}
}
//MessageBox.Show("添加成功!");
}
评论回复