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 ...
随机推荐
- win2012,oracle11g,sqlplus切换实例的方法
问题环境:windows 2012 r2 64位 ,oracle 11.2.0.4,多个实例. 在这种情况下, sqlplus "/as sysdba" 默认登录的是系统后面安装 ...
- .net之工作流工程展示及代码分享(二)工作流引擎
在介绍完表单类的时候,接下来介绍工作流引擎,主要由四个类组成,分别是流程.流程步骤.流程实例.流程步骤实例类. 流程类: [Serializable] public class Flow { [Xml ...
- asp.net XMLHttpRequest 进度条以及lengthComputable always false的解决办法
一直用ajax好长时间了,对其原理也有一些了解,最近由于项目需要,使用ajax异步进度条的效果,就研究了一下,用原生的XMLHttpRequest实现进度条函数,XMLHttpRequest有以下函数 ...
- 加密connectionStrings
开始菜单>>所有程序>>Microsoft visual studio 2010 >> Visual Studio Tools >> Visual St ...
- Timus 2068. Game of Nuts 解题报告
1.题目描述: 2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still i ...
- 写个shell脚本
以前更新网站程序都是手动噼里啪啦敲代码,即麻烦又慢,还神经紧张.终于忍不住写个shell脚本. cd /usr/local/tomcat7/apache-tomcat-9.0.0.M4/ bin/ ...
- Dynamics AX 2012 R2 业务系列-销售业务流程
在博文Dynamics AX R2 业务系列中,Reinhard对这个系列做了一个规划,下面我们就按照规划开始说业务吧. 1.销售的主要职责 其实这里说的职责主要是针对销售文员,并非整天外面满世界跑业 ...
- sys,os,模块-正则表达式
# *__conding:utf-8__* """"我是注释""" sys,os模块 import sysimport os pr ...
- python 列表排序
转自http://www.iplaypython.com/jinjie/jj114.html reverse()方法 将列表中元素反转排序,比如下面这样>>> x = [1,5,2, ...
- 《利用python进行数据分析》读书笔记--第五章 pandas入门
http://www.cnblogs.com/batteryhp/p/5006274.html pandas是本书后续内容的首选库.pandas可以满足以下需求: 具备按轴自动或显式数据对齐功能的数据 ...