标签:blog http io ar 使用 sp on 数据 div
今天在云和学院学习了ADO.Net
DataReader:数据读取器,只读、只进的结果集,一条一条读取数据


string connstring = "Data Source=.;Initial Catalog=db_buiness;Integrated Security=True";
using(SqlConnection conn = new SqlConnection(connstring))
{
conn.Open();
string conntext = "insert into 职工(职工号,仓库号,姓名,性别,工资) values(‘zg19‘,‘wh5‘,‘王丽‘,‘女‘,1200)";
using(SqlCommand comm=new SqlCommand(conntext,conn))
{
int num = comm.ExecuteNonQuery();
if(num>0)
{
Console.WriteLine("插入成功");
}
else
{
Console.WriteLine("插入失败");
}
}
}
Console.ReadKey();

更新数据
string connstring = "Data Source=.;Initial Catalog=db_buiness;Integrated Security=True";
using (SqlConnection conn = new SqlConnection(connstring))
{
conn.Open();
string conntext = "update 职工 set 职工号=‘zg20‘ where 职工ID=19";
using (SqlCommand comm = new SqlCommand(conntext, conn))
{
int num = comm.ExecuteNonQuery();
if (num > 0)
{
Console.WriteLine("更新成功");
}
else
{
Console.WriteLine("更新失败");
}
}
}
Console.ReadKey();

删除数据
string connstring = "Data Source=.;Initial Catalog=db_buiness;Integrated Security=True";
using (SqlConnection conn = new SqlConnection(connstring))
{
conn.Open();
string conntext = "delete from 职工 where 职工ID=19";
using (SqlCommand comm = new SqlCommand(conntext, conn))
{
int num = comm.ExecuteNonQuery();
if (num > 0)
{
Console.WriteLine("删除成功");
}
else
{
Console.WriteLine("删除失败");
}
}
}
Console.ReadKey();

查询操作
private void btnlogin_Click(object sender, EventArgs e)
{
string name = this.txtname.Text;
string pwd = this.txtpwd.Text;
string connectstring = "Data Source=.;Initial Catalog=db_buiness;Integrated Security=True";
using(SqlConnection conn=new SqlConnection(connectstring))
{
conn.Open();
string sql = "select name,pwd from 用户 where name=@name and pwd=@pwd";
SqlParameter parms = new SqlParameter("@name",name);
SqlParameter parms1 = new SqlParameter("@pwd",pwd);
using(SqlCommand cmd=new SqlCommand(sql,conn))
{
cmd.Parameters.Add(parms);
cmd.Parameters.Add(parms1);
DataTable dt = new DataTable();
using(SqlDataAdapter sda=new SqlDataAdapter(cmd))
{
sda.Fill(dt);
}
if(dt.Rows.Count>0)
{
MessageBox.Show("登陆成功");
}
else
{
MessageBox.Show("登陆失败");
}
}
}
}

查找出表中所有记录
protected void Page_Load(object sender, EventArgs e)
{
string connectstring = "Data Source=.;Initial Catalog=db_buiness;Integrated Security=True";
using(SqlConnection conn=new SqlConnection(connectstring))
{
conn.Open();
string sql = "select * from 职工";
using(SqlCommand cmd=new SqlCommand(sql,conn))
{
DataTable dt = new DataTable();
using(SqlDataAdapter sda=new SqlDataAdapter(cmd))
{
sda.Fill(dt);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}

标签:blog http io ar 使用 sp on 数据 div
原文地址:http://www.cnblogs.com/songfang/p/4156263.html