C#: DataBase
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的更多相关文章
- sqlite 报错:database is locked
在sqlite批量添加数据时,报错:database is locked. 解决办法:将db路径由相对路径设置为绝对路径.
- Sql性能检测工具:Sql server profiler和优化工具:Database Engine Tuning Advisor
原文:Sql性能检测工具:Sql server profiler和优化工具:Database Engine Tuning Advisor 一.工具概要 数据库应用系统性能低下,需要对其进行优化 ...
- 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类中做 ...
- Android 4 学习(16):Database and Content Providers
参考<Professional Android 4 Development> Database and Content Providers Android Database简介 Andro ...
- Yii2系列教程三:Database And Gii
上一篇文章我们理了一下Yii2的MVC,Forms和Layouts,这篇文章就直接按照约定来说说Yii2与数据库相关的一些事情,如果你觉得不够的话,不急,更具体的用法我会在后续的教程给出,并且这里也会 ...
- Influx Sql系列教程一:database 数据库
对于influxdb而言,database和我们更熟悉的mysql中的dababse没有什么特别的区别,可以将数据库简单理解为一堆表(measurement)的集合,接下来我们将看一下在influxd ...
- 安卓初級教程(1):@Database(1)
package com.example.android.db01; import android.app.Activity; import android.content.ContentValues; ...
- 例子:Database - Linq to sql
DataContext类型(数据上下文)是System.Data.Linq命名空间下的重要类型,用于把查询句法翻译成SQL语句,以及把数据从数据库返回给调用方和把实体的修改写入数据库. DataCon ...
- 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 ...
随机推荐
- 夺命雷公狗-----React_native---3---react-native-cli的安装
我们用npm安装下react-native-cli,并用-g来全局安装,我们用命令 npm install -g react-native-cli 如果和下图一样即表示已安装成功:
- 微信聊天记录查看器(程序+源码) - iOS版
本文版权归cxun所有,如有转载请注明出处与本文链接,谢谢!原文地址:http://www.cnblogs.com/cxun/p/4338643.html Updates [2016.10.14]感谢 ...
- iOS socket 笔记
ios 客服端: 下载 AsyncSocket 开发框架,拖到项目中 //建立 #import "ViewController.h" #import <sys/socket. ...
- 【笔记】css hover 伪类控制其他元素
最近在模仿一个网站的项目 当中有一个效果需要利用到hover效果因为不太想写jq脚本所以百度了一下css hover的运用发现原来hover也可以控制其他元素的变化的 但是这有一个要求 就是添加hov ...
- 写个shell脚本
以前更新网站程序都是手动噼里啪啦敲代码,即麻烦又慢,还神经紧张.终于忍不住写个shell脚本. cd /usr/local/tomcat7/apache-tomcat-9.0.0.M4/ bin/ ...
- IIS的配置
一.首先是安装IIS.打开控制面板,找到“程序与功能”,点进去 二.点击左侧“打开或关闭Windows功能” 三.找到“Internet 信息服务”,按照下图打勾即可 等待安装完成 四.安装完成后,再 ...
- vim基本命令之剪切复制粘贴替换
首先是剪切(删除): 剪切其实也就顺带删除了所选择的内容,所以既可以当剪切命令用,也可以当删除命令使用. 1 首先,可以在命令模式下输入v进入自由选取模式,选择需要剪切的文字后,按下d就可以进行剪切了 ...
- nes 红白机模拟器 第3篇 游戏手柄测试 51 STM32
手柄使用的是 CD4021 ,datasheet 上说支持 3V - 15V . 因为手柄是 5V 供电,2440 开发板上是GPIO 3.3V 电平,STM32 GPIO 也是 3.3V (也兼容5 ...
- loadrunner总体使用篇
为什么要进行性能测试呢? 有些问题是只有在大并发或者压力测试下才会暴露出来的,在平常的公司内部测试中,感觉一切都是正常的,但是把服务放到生产线上,例如某个时刻突然有很多的用户要向我们的服务发送请求, ...
- Kafka 0.9+Zookeeper3.4.6集群搭建、配置,新Client API的使用要点,高可用性测试,以及各种坑 (转载)
Kafka 0.9版本对java client的api做出了较大调整,本文主要总结了Kafka 0.9在集群搭建.高可用性.新API方面的相关过程和细节,以及本人在安装调试过程中踩出的各种坑. 关于K ...