数据库实验四 数据库的查询

  • 格式:doc
  • 大小:44.50 KB
  • 文档页数:5

实验四 数据库的查询
一.实验目的:
理解并掌握利用.net 和Delphi 、Java(JBuilder 或Eclipse)等编程平台进行数据库访问的基本过程,熟悉数据库访问方法。

二.实验属性:
设计性。

三.实验仪器设备及器材:
装有.net 和Delphi 7、.net 或JDK 编译器及SQL Server 的计算机。

四. 实验过程:
1、查询满足以下条件的元组:
1.输入姓名
2.选择条件
3.条件满足的范围(成绩)
Sql 查询语句:
1. 大于 "selectstudent.sno,student.sname,student.sdept, o,ame,sc.grade" +" from Student,SC, Course where Student.Sname= '"+ textBox1.Text + "'" + " and
Student.Sno=SC.SNO AND O=O and sc.grade>" + num;
2. 大于等于
"selectstudent.sno,student.sname,student.sdept, o,ame,sc.grade" +" from Student,SC, Course where Student.Sname= '"+ textBox1.Text + "'" + " and
Student.Sno=SC.SNO AND O=O and sc.grade>=" + 3、小于 选择条件
1、大于
2、大于等于 4、小于等于 5、 等于 6、不等于
num;
3. 小于
"selectstudent.sno,student.sname,student.sdept, o,ame,sc.grade" +" from Student,SC, Course where Student.Sname= '"+ textBox1.Text + "'" + " and
Student.Sno=SC.SNO AND O=O and sc.grade<" + num;
4. 小于等于
"selectstudent.sno,student.sname,student.sdept, o,ame,sc.grade" +" from Student,SC, Course where Student.Sname= '"+ textBox1.Text + "'" + " and
Student.Sno=SC.SNO AND O=O and sc.grade<="+ num;
5.等于
"selectstudent.sno,student.sname,student.sdept, o,ame,sc.grade" +" from Student,SC, Course where Student.Sname= '"+ textBox1.Text + "'" + " and
Student.Sno=SC.SNO AND O=O and sc.grade<="+ num;
6.不等于
"selectstudent.sno,student.sname,student.sdept, o,ame,sc.grade" +" from Student,SC, Course where Student.Sname= '"+ textBox1.Text + "'" + " and
Student.Sno=SC.SNO AND O=O and sc.grade<="+ num;
2、封装数据库的连接类(DbConnClass)
class DbConnClass
{
private String myConString;
private SqlConnection myconn;
private SqlCommand mycmd;
public DbConnClass()
{
this.myConString = (@"Data
Source=.\SQLEXPRESS;
Initial Catalog=Demo;
Integrated Security=true; User ID=nys; Password=123;"); myconn = new SqlConnection();
myconn.ConnectionString = myConString;
mycmd = new SqlCommand();
}
public bool isConnected()
{
this.myconn.Open();
if (myconn.State == ConnectionState.Open)
return true;
else
return false;
}
public String getPassWord(String str)
{
String str1;
String mysql = "";
mysql = "select UserPWD from Login where UserName = '" +str+ "'";
this.mycmd.Connection = myconn;
mandText = mysql;
str1 = mycmd.ExecuteScalar().ToString();
return str1;
}
public SqlCommand getSQLCommand()
{
return mycmd;
}
public SqlConnection getConnection()
{
return myconn;
}
}
3、数据的查询操作:
public partial class Form2 : Form
{
private String mystr;
public Form2()
{
InitializeComponent();
}
void setRedonly()
{
textBox1.ReadOnly = true;
textBox3.ReadOnly = true;
textBox1.Text = "";
textBox3.Text = "";
}
public String getMysql()根据不同的条件等到不同的sql查询语句
{
int num = int.Parse(textBox3.Text.Trim());
switch (comboBox1.SelectedIndex)
{
/*对mystr进行赋值*/
}
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == null || textBox3.Text == null || comboBox1.Text == null)
MessageBox.Show("请注意查询信息的完整性", "警告", MessageBoxButtons.OK);
DataSet myds = new DataSet();
DbConnClass myconn=new DbConnClass();
if (!myconn.isConnected())
MessageBox.Show("数据库连接失败,请检查数据库的连接", "提示", MessageBoxButtons.OK);
getMysql();
SqlDataAdapter myda = new
SqlDataAdapter(mystr, myconn.getConnection());
myda.Fill(myds, "Student");
dataGridView1.DataSource =
myds.Tables["Student"];
dataGridView1.GridColor = Color.RoyalBlue; dataGridView1.ScrollBars = ScrollBars.Both; dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Single;
}
}。