测试1.书店的增删改查项目.链接数据库

Posted WuXuanKun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了测试1.书店的增删改查项目.链接数据库相关的知识,希望对你有一定的参考价值。

0.1创建数据库

0.2向数据库内添加数据

0.3创建一个辅助类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Library
 8 {
 9     public class SqlTools
10     {
11         public static string str = "Data Source=.;Initial Catalog=Library;Uid=sa";
12     }
13 }

0.4创建主窗体如图

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 using System.Data.SqlClient;
 11 
 12 namespace Library
 13 {
 14     public partial class Homepage : Form
 15     {
 16         public Homepage()
 17         {
 18             InitializeComponent();
 19         }
 20         DataSet ds = new DataSet();//连接数据集
 21         private void Homepage_Load(object sender, EventArgs e)
 22         {
 23            //主窗体
 24             Show();//调用所有书的方法
 25         }
 26         #region 显示所有的书的方法
 27         public void Show()
 28         {
 29             SqlConnection con = new SqlConnection(SqlTools.str);//创建 Connection对象
 30             //SQL语句
 31             string sql = "select * from Homepage";
 32             //创建DataAdapter数据适配器
 33             SqlDataAdapter da = new SqlDataAdapter(sql, con);
 34             //填充数据
 35             da.Fill(ds, "Homepage");
 36             //数据源
 37             dvgList.DataSource = ds.Tables["Homepage"];
 38 
 39         } 
 40         #endregion
 41 
 42         #region 查询判断的方法
 43         public void Type()
 44         {
 45             DataView dv = new DataView(ds.Tables["Homepage"]);
 46             if (!(txtBookName.Text.Trim().Equals("")) && (txtAuthor.Text.Trim().Equals("")))
 47             {
 48                 dv.RowFilter = "BookName like\'%" + txtBookName.Text + "%\'";
 49                 dvgList.DataSource = dv;
 50             }
 51             else if ((txtBookName.Text.Trim().Equals("")) && (!txtAuthor.Text.Trim().Equals("")))
 52             {
 53                 dv.RowFilter = "Author like\'%" + txtAuthor.Text + "%\'";
 54                 dvgList.DataSource = dv;
 55             }
 56             else
 57             {
 58                 MessageBox.Show("没有您查询的书!");
 59             }
 60 
 61         } 
 62         #endregion
 63 
 64         private void btnInquiry_Click(object sender, EventArgs e)
 65         {
 66             Type();//调用查询的方法
 67         }
 68 
 69         private void btnAdd_Click(object sender, EventArgs e)
 70         {
 71             //新增
 72             FrmMain  frm = new FrmMain ();
 73             frm.stu = this;
 74             frm.Show();
 75         }
 76 
 77         private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
 78         {
 79             //删除
 80             #region 删除
 81             if (dvgList.SelectedRows[0] != null)
 82             {
 83                 string name = dvgList.SelectedRows[0].Cells["BookName"].Value.ToString();
 84                 SqlConnection con = new SqlConnection(SqlTools.str);
 85                 string sql = "Delete from Homepage where BookName=\'" + name + "\'";
 86                 SqlCommand cmd = new SqlCommand(sql, con);
 87                 try
 88                 {
 89                     con.Open();
 90                     int result = Convert.ToInt32(cmd.ExecuteScalar());
 91                     if (result > 0)
 92                     {
 93                         MessageBox.Show("删除成功!");
 94                     }
 95                     else
 96                     {
 97                         MessageBox.Show("删除失败!");
 98                     }
 99 
100                 }
101                 catch (Exception)
102                 {
103                     MessageBox.Show("网络连接异常!");
104                     throw;
105                 }
106                 finally
107                 {
108                     con.Close();
109                 }
110             } 
111             #endregion
112         }
113         #region 刷新
114         public void input()
115         {
116             if (ds.Tables[0] != null)
117             {
118                 ds.Tables["Homepage"].Clear();
119             }
120             Show();
121         } 
122         #endregion
123         
124         private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
125         {
126             FrmMain frm = new FrmMain();
127             frm.Text = "修改";
128             if (dvgList .SelectedRows [0]!=null )
129             {
130                 string name = dvgList.SelectedRows[0].Cells["BookName"].Value.ToString();
131                 frm.BookName = name;
132             }
133             frm.stu = this;
134             frm.Show();
135         }
136 
137 
138 
139         
140 
141     }
142 }

0.5创建新增页面

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 using System.Data.SqlClient;
 11 
 12 namespace Library
 13 {
 14     public partial class FrmMain : Form
 15     {
 16         public FrmMain()
 17         {
 18             InitializeComponent();
 19             
 20         }
 21         public Homepage stu;
 22         public string BookName;
 23         
 24         private void btnCancel_Click(object sender, EventArgs e)
 25         {
 26 
 27 
 28         }
 29         SqlConnection con = new SqlConnection(SqlTools .str ); 
 30         private void btnConservation_Click(object sender, EventArgs e)
 31         {
 32             //保存
 33             if (this.Text .Trim ().Equals ("新增"))
 34             {
 35                 insert();  
 36             }
 37             else if (this.Text .Trim ().Equals ("修改"))
 38             {
 39                 update();
 40             }
 41         }
 42         public void insert()
 43         {
 44             con.Open();
 45             string sql = "insert into Homepage values(\'" + txtBookName.Text + "\',\'" + txtAuthor.Text + "\',\'" + txtPrice.Text + "\',\'" + txtQuantity .Text+ "\')";
 46             SqlCommand cmd = new SqlCommand(sql, con);
 47             try
 48             {
 49                 int retult = Convert.ToInt32(cmd.ExecuteScalar());
 50                 if (retult >= 0)
 51                 {
 52                     MessageBox.Show("增加成功!");
 53                 }
 54                 else
 55                 {
 56                     MessageBox.Show("增加失败!");
 57                 }
 58             }
 59             catch (Exception)
 60             {
 61                 MessageBox.Show("连接发生异常!");
 62                 throw;
 63             }
 64             finally
 65             {
 66                 con.Close();
 67             }
 68             stu.input();
 69         }
 70         public void update()
 71         {
 72             con.Open();
 73             string sql = "update Homepage set BookName=\'"+txtBookName.Text +"\',\'"+txtAuthor.Text+"\',\'"+txtPrice.Text+"\',\'"+txtQuantity.Text+"\'where BookName=\'"+BookName +"\'";
 74             SqlCommand cmd = new SqlCommand(sql,con );
 75             try
 76             {
 77                 int retult = Convert.ToInt32(cmd.ExecuteScalar());
 78                 if (retult >= 0)
 79                 {
 80                     MessageBox.Show("修改成功!");
 81                 }
 82                 else
 83                 {
 84                     MessageBox.Show("修改失败!");
 85                 }
 86 
 87             }
 88             catch (Exception)
 89             {
 90                 MessageBox.Show("连接异常!");
 91                 throw;
 92             }
 93             finally
 94             {
 95                 con.Close();
 96             }
 97             stu.input();
 98         }
 99 
100         private void FrmMain_Load(object sender, EventArgs e)
101         {
102             //主窗体
103             txtBookName.Text = BookName;
104         }
105        
106     }
107 }

0.6实现页面效果展示

 

 

以上是关于测试1.书店的增删改查项目.链接数据库的主要内容,如果未能解决你的问题,请参考以下文章

Java操作MongoDB的增删改查,完整项目分享

怎么创建SpringBoot项目并实现数据库的增删改查

数据的增删改查(三层)

CodeSmith生成的增删改查方法模板源码分享

软件测试人员必掌握的增删改查之简单查询

详细解析java JDBC实现增删改查