数据库第三章课后5题

这个博客展示了如何在Windows Forms应用程序中使用C#进行数据库操作,包括通过输入框查询数据并以列表视图展示结果。代码示例涉及SQL连接、查询以及数据读取,同时包含了错误处理和UI交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        
        private void Form2_Load(object sender, EventArgs e)
        {
 
        }


        private void button2_Click(object sender, EventArgs e)
        {
            this.listView1.View = View.SmallIcon;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.listView1.View = View.Details;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            chazhao();
           // this.listView1.View = View.LargeIcon;
        }


        public string CAPTION = "提示";
        DBHelper dbhelper = new DBHelper();

        public void BindGrade()
            {
                string sql = @"SELECT TOP 1000 [Id]
      ,[Name]
      ,[Type]
      ,[Number]
      ,[Price]
  FROM [MySchool].[dbo].[shop]
   where type like '%" + this.Box1.Text + "%'";
                SqlCommand command = new SqlCommand(sql, dbhelper.Connection);
                dbhelper.OpenConnection();
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    ListViewItem lvi1 = new ListViewItem(reader["Name"].ToString());
                    lvi1.SubItems.AddRange(new string[] { reader["Type"].ToString(), reader["Number"].ToString(), reader["Price"].ToString() });
                    this.listView1.Items.Add(lvi1);


                }
            }
        public void chazhao()
        {

            if (listView1.Items.Count > 0)
            {
                listView1.Items.Clear();
            }


            if (Box1.Text == null)
            {
                MessageBox.Show("输入内容为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                DBHelper dbhelper = new DBHelper();
                try
                {
                    dbhelper.OpenConnection();

                    string sql2 = @"SELECT TOP 1000 [Id]
      ,[Name]
      ,[Type]
      ,[Number]
      ,[Price]
  FROM [MySchool].[dbo].[shop] where Type like '%" + this.Box1.Text + "%'";
                    MessageBox.Show(sql2);

                    SqlCommand comm = new SqlCommand(sql2, dbhelper.Connection);
                    SqlDataReader reader = comm.ExecuteReader();
                  
                    while (reader.Read())
                    {
                        ListViewItem liv1 = new ListViewItem(reader["Name"].ToString());
                        liv1.SubItems.AddRange(new string[] { reader["Type"].ToString(), reader["Number"].ToString(), reader["Price"].ToString() });
                        this.listView1.Items.Add(liv1);
                       
                    }
                   

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    dbhelper.CloseConnection();
                }
            }

        }
    }
}
      

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form2());
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace WindowsFormsApplication1
{
    /// <summary>
    /// 此类维护数据库连接字符串,和 Connection 对象
    /// </summary>
  public  class DBHelper
    {
        // 数据库连接字符串
      private string connString = @"Data Source=.;Initial Catalog=MySchool;Integrated Security=True";

        // 数据库连接 Connection 对象
        private SqlConnection connection;

        /// <summary>
        /// Connection对象
        /// </summary>
        public SqlConnection Connection
        {
            get
            {
                if (connection == null)
                {
                    connection = new SqlConnection(connString);
                }
                return connection;
            }            
        }

        /// <summary>
        /// 打开数据库连接
        /// </summary>
        public void OpenConnection()
        {
            if (Connection.State == ConnectionState.Closed)
            {
                Connection.Open();
            }
            else if (Connection.State == ConnectionState.Broken)
            {
                Connection.Close();
                Connection.Open();
            }
        }

        /// <summary>
        /// 关闭数据库连接
        /// </summary>
        public void CloseConnection()
        {
            if (Connection.State == ConnectionState.Open || Connection.State == ConnectionState.Broken)
            {
                Connection.Close();
            }
        }
    }
}

### 关于数据库教程第二章课后练习目及答案 对于寻找特定章节如数据库教程第二章的课后练习目及其解答,通常这类资源会紧密跟随所选教材的具体版本和内容编排。然而,在提供的参考资料中,并未直接提及有关第二章的内容[^1]。 一般而言,数据库系统的第二章往往围绕关系模型基础展开讨论,包括但不限于: - **关系数据结构**:介绍实体间的关系表示方法。 - **关系操作**:讲解如何通过查询语句获取所需信息。 - **关系完整性约束**:探讨保持数据一致性的规则。 针对上述主,可以设计一系列理论与实践相结合的问帮助学习者巩固知识点。虽然无法提供确切来自指定书籍的原版习集,但可以根据这一常规框架构建具有代表性的练习如下: #### 练习一:理解概念 描述什么是关系模式?给出三个实际生活中的例子说明不同类型的联系(一对一、一对多、多对多)。 #### 练习二:SQL应用 编写一段 SQL 代码创建一个名为 `Students` 的表,该表应包含字段 StudentID, Name, Age 和 Major。并插入几条记录用于测试。 ```sql CREATE TABLE Students ( StudentID INT PRIMARY KEY, Name VARCHAR(50), Age INT, Major VARCHAR(100) ); INSERT INTO Students (StudentID, Name, Age, Major) VALUES (1,'Alice',20,'Computer Science'), (2,'Bob',22,'Mathematics'); ``` #### 练习三:查询优化 假设有一个大型的学生成绩管理数据库,请解释索引的作用以及何时应该考虑建立索引来提高性能?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值