您的位置:首页 > 数据库

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

2016-04-06 21:48 411 查看

0.1创建数据库

0.2向数据库内添加数据

0.3创建一个辅助类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Library
{
public class SqlTools
{
public static string str = "Data Source=.;Initial Catalog=Library;Uid=sa";
}
}

0.4创建主窗体如图

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 Library
{
public partial class Homepage : Form
{
public Homepage()
{
InitializeComponent();
}
DataSet ds = new DataSet();//连接数据集
private void Homepage_Load(object sender, EventArgs e)
{
//主窗体
Show();//调用所有书的方法
}
#region 显示所有的书的方法
public void Show()
{
SqlConnection con = new SqlConnection(SqlTools.str);//创建 Connection对象
//SQL语句
string sql = "select * from Homepage";
//创建DataAdapter数据适配器
SqlDataAdapter da = new SqlDataAdapter(sql, con);
//填充数据
da.Fill(ds, "Homepage");
//数据源
dvgList.DataSource = ds.Tables["Homepage"];

}
#endregion

#region 查询判断的方法
public void Type()
{
DataView dv = new DataView(ds.Tables["Homepage"]);
if (!(txtBookName.Text.Trim().Equals("")) && (txtAuthor.Text.Trim().Equals("")))
{
dv.RowFilter = "BookName like'%" + txtBookName.Text + "%'";
dvgList.DataSource = dv;
}
else if ((txtBookName.Text.Trim().Equals("")) && (!txtAuthor.Text.Trim().Equals("")))
{
dv.RowFilter = "Author like'%" + txtAuthor.Text + "%'";
dvgList.DataSource = dv;
}
else
{
MessageBox.Show("没有您查询的书!");
}

}
#endregion

private void btnInquiry_Click(object sender, EventArgs e)
{
Type();//调用查询的方法
}

private void btnAdd_Click(object sender, EventArgs e)
{
//新增
FrmMain  frm = new FrmMain ();
frm.stu = this;
frm.Show();
}

private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
//删除
#region 删除
if (dvgList.SelectedRows[0] != null)
{
string name = dvgList.SelectedRows[0].Cells["BookName"].Value.ToString();
SqlConnection con = new SqlConnection(SqlTools.str);
string sql = "Delete from Homepage where BookName='" + name + "'";
SqlCommand cmd = new SqlCommand(sql, con);
try
{
con.Open();
int result = Convert.ToInt32(cmd.ExecuteScalar());
if (result > 0)
{
MessageBox.Show("删除成功!");
}
else
{
MessageBox.Show("删除失败!");
}

}
catch (Exception)
{
MessageBox.Show("网络连接异常!");
throw;
}
finally
{
con.Close();
}
}
#endregion
}
#region 刷新
public void input()
{
if (ds.Tables[0] != null)
{
ds.Tables["Homepage"].Clear();
}
Show();
}
#endregion

private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
{
FrmMain frm = new FrmMain();
frm.Text = "修改";
if (dvgList .SelectedRows [0]!=null )
{
string name = dvgList.SelectedRows[0].Cells["BookName"].Value.ToString();
frm.BookName = name;
}
frm.stu = this;
frm.Show();
}

}
}

0.5创建新增页面

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 Library
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();

}
public Homepage stu;
public string BookName;

private void btnCancel_Click(object sender, EventArgs e)
{

}
SqlConnection con = new SqlConnection(SqlTools .str );
private void btnConservation_Click(object sender, EventArgs e)
{
//保存
if (this.Text .Trim ().Equals ("新增"))
{
insert();
}
else if (this.Text .Trim ().Equals ("修改"))
{
update();
}
}
public void insert()
{
con.Open();
string sql = "insert into Homepage values('" + txtBookName.Text + "','" + txtAuthor.Text + "','" + txtPrice.Text + "','" + txtQuantity .Text+ "')";
SqlCommand cmd = new SqlCommand(sql, con);
try
{
int retult = Convert.ToInt32(cmd.ExecuteScalar());
if (retult >= 0)
{
MessageBox.Show("增加成功!");
}
else
{
MessageBox.Show("增加失败!");
}
}
catch (Exception)
{
MessageBox.Show("连接发生异常!");
throw;
}
finally
{
con.Close();
}
stu.input();
}
public void update()
{
con.Open();
string sql = "update Homepage set BookName='"+txtBookName.Text +"','"+txtAuthor.Text+"','"+txtPrice.Text+"','"+txtQuantity.Text+"'where BookName='"+BookName +"'";
SqlCommand cmd = new SqlCommand(sql,con );
try
{
int retult = Convert.ToInt32(cmd.ExecuteScalar());
if (retult >= 0)
{
MessageBox.Show("修改成功!");
}
else
{
MessageBox.Show("修改失败!");
}

}
catch (Exception)
{
MessageBox.Show("连接异常!");
throw;
}
finally
{
con.Close();
}
stu.input();
}

private void FrmMain_Load(object sender, EventArgs e)
{
//主窗体
txtBookName.Text = BookName;
}

}
}

0.6实现页面效果展示

 

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: