using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
class DB
{
private SqlConnection con;
private SqlCommand cmd;
private string ConnectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\Ramon\\documents\\visual studio 2015\\Projects\\WindowsFormsApplication1\\WindowsFormsApplication1\\myTest.mdf;Integrated Security=True;Connect Timeout=30";

public DB()
{


}
//
// insert
//
public void InsertData(Form1 obj)
{
string id = obj.idTextBox.Text;
bool cover = false; ;
if (exist(id))
{
DeleteRow(id);
obj.statusLabel.Text = "状态:已覆盖原数据";
cover = true;
}
this.con = new SqlConnection();
con.ConnectionString = ConnectionString;
con.Open();

cmd = new SqlCommand("INSERT INTO Goods (Id,Name,Num,Price) VALUES (@Id,@Name,@Num,@Price)", con);

//cmd = new SqlCommand("INSERT INTO Goods (Id,Name,Num,Price) VALUES (2,12,123,1234)", con);
cmd.Parameters.AddWithValue("@Id", obj.idTextBox.Text);
cmd.Parameters.AddWithValue("@Name", obj.nameTextBox.Text);
cmd.Parameters.AddWithValue("@Num", obj.numTextBox.Text);
cmd.Parameters.AddWithValue("@Price", obj.priceTextBox.Text);
cmd.ExecuteNonQuery();
con.Close();
if (!cover) {
obj.statusLabel.Text = "状态:录入完成";
}
}
//
// search
//
public void GetData(Form1 obj)
{
string id = obj.idTextBox.Text;
SelectData(id);
//最后让label显示检索到的字段的值.
obj.nameTextBox.Text = Goods.name;
obj.numTextBox.Text = Goods.num;
obj.priceTextBox.Text = Goods.price;
obj.statusLabel.Text = Goods.status;
if (!Goods.exist)
{
obj.statusLabel.Text = "未查询到该商品";
obj.nameTextBox.Text = "";
obj.numTextBox.Text = "";
obj.priceTextBox.Text = "";
}
}

public void DeleteRow(string id)
{
this.con = new SqlConnection();
con.ConnectionString = ConnectionString;
con.Open();

string cmdText = "Delete FROM Goods WHERE Id='" + id + "'";
cmd = new SqlCommand(cmdText, con);

SqlDataReader reader = cmd.ExecuteReader();
}

public void SelectData(string id)
{
this.con = new SqlConnection();
con.ConnectionString = ConnectionString;
con.Open();

string cmdText = "SELECT * FROM Goods WHERE Id='" + id + "'";
cmd = new SqlCommand(cmdText, con);

SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
Goods.name = reader["Name"].ToString();
Goods.num = reader["Num"].ToString();
Goods.price = reader["Price"].ToString();
Goods.status = "状态:查询完成";
Goods.exist = true;
}
con.Close();
}

public bool exist(string id)
{
Goods.exist = false;
SelectData(id);
return Goods.exist;
}
}
}

C#: DataBase的更多相关文章

  1. sqlite 报错:database is locked

    在sqlite批量添加数据时,报错:database is locked. 解决办法:将db路径由相对路径设置为绝对路径.

  2. Sql性能检测工具:Sql server profiler和优化工具:Database Engine Tuning Advisor

    原文:Sql性能检测工具:Sql server profiler和优化工具:Database Engine Tuning Advisor 一.工具概要     数据库应用系统性能低下,需要对其进行优化 ...

  3. Django(博客系统):按照时间分层筛选“/blog/article/?create_time__year=2017”,出现问题:Database returned an invalid datetime value. Are time zone definitions for your database installed?

    问题背景 添加文章时间没问题,但为了设定博客文章按照时间分层筛选(创建时间的年份.年月&月份来搜索文章),我在blog这个django app的admin.py的ArticleAdmin类中做 ...

  4. Android 4 学习(16):Database and Content Providers

    参考<Professional Android 4 Development> Database and Content Providers Android Database简介 Andro ...

  5. Yii2系列教程三:Database And Gii

    上一篇文章我们理了一下Yii2的MVC,Forms和Layouts,这篇文章就直接按照约定来说说Yii2与数据库相关的一些事情,如果你觉得不够的话,不急,更具体的用法我会在后续的教程给出,并且这里也会 ...

  6. Influx Sql系列教程一:database 数据库

    对于influxdb而言,database和我们更熟悉的mysql中的dababse没有什么特别的区别,可以将数据库简单理解为一堆表(measurement)的集合,接下来我们将看一下在influxd ...

  7. 安卓初級教程(1):@Database(1)

    package com.example.android.db01; import android.app.Activity; import android.content.ContentValues; ...

  8. 例子:Database - Linq to sql

    DataContext类型(数据上下文)是System.Data.Linq命名空间下的重要类型,用于把查询句法翻译成SQL语句,以及把数据从数据库返回给调用方和把实体的修改写入数据库. DataCon ...

  9. Entity Framework 6.0 Tutorials(4):Database Command Logging

    Database Command Logging: In this section, you will learn how to log commands & queries sent to ...

随机推荐

  1. linux命令行中单个减号表示标准输入输出流

    tar cvf - /src/myprojectsrc 这里本来是要输出到一个.tar文件的,目标.tar文件应该放在上面命令的减号处,因此这里的减号表示标准输出流. 减号单独作为命令行参数时,是标准 ...

  2. js 简体中文拼音对应表

    https://github.com/silaLi/pinyin js 拼音对象,包涵大部分文字

  3. Yii2中多表关联查询(join、joinwith)

    我们用实例来说明这一部分 表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer   (id  customer_name) 订单表Order      (id  order_name ...

  4. Java设计模式(三)——观察者模式和监听器

    为了实现多个模块之间的联动,最好的方法是使用观察者模式.网上介绍的资料也比较多,今天我就从另一个方面谈谈自己对观察者模式的理解.从JDK提供的支持库里,我们能够找到四个对象:Observable.Ob ...

  5. ==与equals 的使用比较

    1. == 是一个运算符. 2.Equals则是string对象的方法 我们通常是两种类型的比较 1.基本数据类型比较 2.引用对象比较 其中 1.基本数据类型比较 ==和Equals都比较两个值是否 ...

  6. asp.net-枚举绑定控件

    1.DropDownList asp.net页面: <asp:DropDownList ID="drpTemplateType" runat="server&quo ...

  7. angularjs $broadcast 和 $on 的使用及其注意事项

    下面是demo: <div ng-controller="ParentCtrl"> www.111cn.net //父级 <div ng-controller=& ...

  8. [JAVA] java class 基本定义 Note

    java class 基本定义 Note 1 package abeen.note; 2 import java.util.*; 3 4 5 /* 6 java calss 基本 7 */ 8 pub ...

  9. centos6.x开机卡死

    问题描述: centos6.x开机卡死在进度条处 远程登录,查看系统日志,看看卡在哪儿 通过远程连接,tail -fn 20 /var/log/messages 发现报错: init: prefdm ...

  10. selenium-JS点击(项目应用)

    public static JavascriptExecutor jse; 声明一个js public LogoutWebElements(WebDriver driver){        Logo ...