// 连接服务器以及数据库,
string sql = "Data Source=祭司;Initial Catalog=Silence;User ID=sa;Password=********";
SqlConnection conn = new SqlConnection(sql);
//打开数据库
conn.Open();
SqlCommand cmd = conn.CreateCommand(); //也可以写为 SqlCommand cmd = new SqlCommand("select * from T_User"; ,conn);
//定义向数据库中发送的 语句 此处为查询语句cmd.CommandText = "select * from T_User";
//此处为 修改数据库 资料的语句
// cmd.CommandText = "update T_User set Mobile=@mobile where Id=3";
//将 语句 中的占位符换成实际值
//cmd.Parameters.AddWithValue("@mobile","13534685486");
//此处为 删除语句
// SqlCommand cmd = new SqlCommand("delete from T_User where Id=@id",conn);// 等价于 SqlCommand cmd = conn.CreateCommand();
// cmd.Parameters.AddWithValue("@id","3");
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable data = new DataTable();adapter.Fill(data);
//断开连接
conn.Close();
//清除链接
conn.Dispose();
cmd.Dispose();