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;
}
}
}

时间: 2024-10-22 14:15:09

C#: DataBase的相关文章

Django Ubuntu:Database returned an invalid value in QuerySet.dates(). 错误的解决方法

运行Windows下创建的Django项目时,发生此错误! Database returned an invalid value in QuerySet.dates(). Are time zone definitions and pytz installed? 按提示先安装pytz 1.先安装easy_install: sudo apt-get install python-setuptools 2.进入官网,获取下载地址,使用wget命令下载: wget https://pypi.pytho

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

原文:Sql性能检测工具:Sql server profiler和优化工具:Database Engine Tuning Advisor 一.工具概要 ? ? 数据库应用系统性能低下,需要对其进行优化, ? ? 如果不知道问题出在哪里,可以使用性能检测工具sql server profiler. ? ? 如果知道问题出在哪里,却不知道如何解决,可以使用数据库引擎优化顾问Database Engine Tuning Advisor 二.sql server profiler 功能:检测到数据库中的

Yii2系列教程三:Database And Gii

上一篇文章我们理了一下Yii2的MVC,Forms和Layouts,这篇文章就直接按照约定来说说Yii2与数据库相关的一些事情,如果你觉得不够的话,不急,更具体的用法我会在后续的教程给出,并且这里也会介绍Yii2的代码生成工具:强大的Gii. 你可以直接到Github下载项目源码:https://github.com/JellyBool/helloYii,这样你就可以直接跟上我的进度了,每一次我写完一个教程,我都会将代码push到Github,所以,你想偷懒的话,这是一个不错的方法. 接着上一篇

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类中做了如下设置: date_hierarchy = 'create_time' # 详细时间分层筛选 models.Article中create_time定义如下: create_time = models.DateTimeField(u'创建时间', auto_now_add=True) 设置后,后台呈现效果

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

对于influxdb而言,database和我们更熟悉的mysql中的dababse没有什么特别的区别,可以将数据库简单理解为一堆表(measurement)的集合,接下来我们将看一下在influxdb中,database的常规操作 1. 查看当前数据库 如果需要查询当前有哪些数据库,可以通过show语句来实现 show database 上面的_internal是内置的数据库 2. 创建数据库 create database yhh 创建一个名为yhh的数据库 3. 使用数据库 如果需要查询某

Entity Framework Code-First(17):Database Initialization Strategy

Database Initialization Strategies in Code-First: You already created a database after running your Code-First application the first time, but what about the second time onwards?? Will it create a new database every time you run the application? What

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 the database by Entity Framework. Prior to EF 6, we used the database tracing tool or third party tracing utility to trace database queries and commands s

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

1 package com.example.android.db01; 2 3 import android.app.Activity; 4 import android.content.ContentValues; 5 import android.content.Context; 6 import android.database.Cursor; 7 import android.database.sqlite.SQLiteDatabase; 8 import android.databas

翻译:MariaDB DATABASE()

html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,header,hgroup,main,nav,section,summary { display: block } audio,canvas,progress,video { display: inline-block; vertical-align: baseline } audio:not([co