基本信息
源码名称:图书管理(c#数据库操作入门级示例代码)
源码大小:0.10M
文件格式:.zip
开发语言:C#
更新时间:2016-05-26
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace BookManage
{
    public partial class add : Form
    {
        public add()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
        private void add_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=BookManage;Integrated Security=True");
            SqlDataAdapter adp = new SqlDataAdapter("select * from BookType", con);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            comboBox1.DataSource = dt;
            comboBox1.DisplayMember = "typeName";
            comboBox1.ValueMember = "typeID";

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                double d = Convert.ToDouble(textBox1.Text.Trim());
            }
            catch (Exception)
            {
                MessageBox.Show("图书编号只能是正整数!");
                textBox1.Text = "";
                textBox1.Focus();
                return;
            }
            try
            {
                double d = Convert.ToDouble(textBox4.Text.Trim());
            }
            catch (Exception)
            {
                MessageBox.Show("图书价格只能是正整数!");
                textBox4.Text = "";
                textBox4.Focus();
                return;
            }
            if (textBox1 .Text=="")
            {
                MessageBox.Show("图书编号不能为空!");
                textBox1.Focus();
                return;  
            }
            if (textBox2.Text == "")
            {
                MessageBox.Show("图书名称不能为空!");
                 textBox2.Focus();
                return;

            }
            if (textBox3.Text == "")
            {
                MessageBox.Show("图书作者不能为空!");
                 textBox3.Focus();
                return;

            }
            if (textBox4.Text == "")
            {
                MessageBox.Show("图书价格不能为空!");
                textBox4.Focus();
                return;

            }

            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=BookManage;Integrated Security=True");
            SqlDataAdapter adp = new SqlDataAdapter("select * from BookInfo where bookCode='" textBox1 .Text  "'", con);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            if (dt .Rows .Count >0)
            {
                MessageBox.Show("图书编号已存在!");
                return;
            }
            string Sql = "insert into BookInfo values('{0}','{1}','{2}','{3}','{4}',0)";
            Sql = string.Format(Sql,textBox1 .Text ,textBox2.Text ,comboBox1.SelectedValue .ToString (),textBox3 .Text ,textBox4 .Text );
            SqlCommand cmd = new SqlCommand(Sql,con);
            con.Open();
            int i = cmd.ExecuteNonQuery();
            con.Close();
            if (i > 0)
            {
                MessageBox.Show("图书添加成功!");
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
            }
            else
            {
                MessageBox.Show("添加失败!");
            }
        }

        
    }
}