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();
}
}
}
}