C#操作数据库总结(vs2005+sql2005)
发布时间:2024-01-20 22:20:29
开发工具:Microsoft Visual Studio 2005
数据库:Microsoft SQL Server 2005
说明:这里建立的数据库名为Demo,有一个学生表Student,为操作方便起见,我只添加两个字段:studentnum和studentname.
一、SQL语句:
--create database Demo
use Demo
create table Student
(
studentnum char(14) primary key,
studentname varchar(30) not null
)
insert into Student values('20041000010201','张扬')
二、代码:
1.引入名称空间:using System.Data.SqlClient;
2.定义连接字符串,连接对象,命令对象:
private String connectionstr;
private SqlConnection connection;
private SqlCommand command;
3.在构造函数中初始化连接字符串,连接对象,命令对象
(1)初始化连接字符串:
方式① connectionstr="server=localhost;uid=sa;pwd=123456;database=Demo";
方式② connectionstr="server=127.0.0.1";Integrade Security=SSPI;database=Demo";
其中,SIMS是我要连接的数据库名.(1)中的uid 和pwd是你登录数据库的登录名和密码
注:这种连接是连接本地的数据库,若要连接局域网内其它机子上的数据库,可将方式①的"server=localhost;"改为"server=数据库所在机子的IP;"
// 连接字符串:String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=product.mdb";
// 建立连接:OleDbConnection connection = new OleDbConnection(connectionString);
// 使用OleDbCommand类来执行Sql语句:
// OleDbCommand cmd = new OleDbCommand(sql, connection);
// connection.Open();
// cmd.ExecuteNonQuery();
#endregion
#region 连接字符串
//string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\程序书籍软件\c#程序代码\access数据库操作\addressList.mdb"; //绝对路径
// string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Environment.CurrentDirectory+"\\addressList.mdb"; //相对路径
(2)初始化连接对象
connection = new SqlConnection(connectionstr);
(3)初始化命令对象
command =new SqlCommand();
command .Connection =connection ;
4.操作数据库中的数据
(1)查询数据库中的数据
方法一:
string snum=tBstudentnum .Text .Trim ();
string str = "select * from Student where studentnum='" + snum + "'";
command .CommandText =str;
connection.Open();
if (command.ExecuteScalar() == null)
{
MessageBox.Show("您输入的学号对应的学生不存在!", "错误", MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else
{
SqlDataReader sdr = command.ExecuteReader();
while (sdr.Read())
{
tBstudentnum .Text = sdr["studentnum"].ToString();
tBstudentname.Text = sdr["studentname"].ToString();
}
sdr.Close();
}
connection.Close();
方法二:
string snum=tBstudentnum .Text .Trim ();
string str = "select * from Student where studentnum='" + snum + "'";
command .CommandText =str;
connection.Open();
if (command.ExecuteScalar() == null)
{
MessageBox.Show("您输入的学号对应的学生不存在!", "错误", MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else
{
SqlDataAdapter sda = new SqlDataAdapter(str,connection );
DataSet ds = new DataSet();
sda.Fill(ds, "Student");
DataTable dt = ds.Tables["Student"];
tBstudentnum.Text = dt.Rows[0]["studentnum"].ToString();
tBstudentname.Text = dt.Rows[0]["studentname"].ToString();
}
connection.Close();
(2)向数据库中添加数据
方法一:
string snum = tBstudentnum.Text.Trim ();
string sname = tBstudentname.Text.Trim();
if (snum == "" || sname == "")
{
MessageBox.Show("学生学号或姓名不能为空!", "错误", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
string insertstr="insert into Student values('"+snum +"','"+sname +"')";
command.CommandText = insertstr;
connection.Open();
command.ExecuteNonQuery();
MessageBox.Show("学生添加成功!", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Information);
connection.Close();
}
方法二:
string str = "select * from Student";
string insertstr = "insert into Student values('" + snum + "','" + sname + "')";
SqlDataAdapter sda = new SqlDataAdapter(str, connection);
DataSet ds = new DataSet();
sda.Fill(ds, "Student");
DataTable dt = ds.Tables["Student"];
DataRow dr = dt.NewRow();
dr["studentnum"] = snum;
dr["studentname"] = sname;
dt.Rows.Add(dr);
sda.InsertCommand = new SqlCommand(insertstr, connection);
sda.Update(ds, "Student");
MessageBox.Show("学生添加成功!", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Information);
(3)修改数据库中的数据
方法一:
string snum = tBstudentnum.Text.Trim();
string sname = tBstudentname.Text.Trim();
if (snum == "" || sname == "")
{
MessageBox.Show("学生学号或姓名不能为空!", "错误", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
string modifystr = "update Student set studentname='" + sname +
"' where studentnum='" + snum + "'";
command.CommandText = modifystr;
connection.Open();
command.ExecuteNonQuery();
MessageBox.Show("学生的姓名修改成功!", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Information );
connection.Close();
方法二:
string snum = tBstudentnum.Text.Trim();
string sname = tBstudentname.Text.Trim();
if (snum == "" || sname == "")
{
MessageBox.Show("学生学号或姓名不能为空!", "错误", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
string str = "select * from Student where studentnum='" + snum + "'"; ;
string updatestr = "update Student set studentname='" + sname +
"' where studentnum='" + snum + "'";
SqlDataAdapter sda = new SqlDataAdapter(str, connection);
DataSet ds = new DataSet();
sda.Fill(ds, "Student");
DataTable dt = ds.Tables["Student"];
dt.Rows[0]["studentname"] = sname;
sda.UpdateCommand = new SqlCommand(updatestr , connection);
sda.Update(ds, "Student");
MessageBox.Show("学生姓名修改成功!", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
(4)删除数据库中的数据
方法一:
string snum = tBstudentnum.Text.Trim();
if (snum == "")
{
MessageBox.Show("学生学号不能为空!", "错误", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
string str = "select * from Student where studentnum='" + snum + "'";
string deletestr = "delete from Student where studentnum='" + snum + "'";
command.CommandText =str ;
connection.Open();
if (command.ExecuteScalar() == null)
{
MessageBox.Show("此学号对应的学生不存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
command.CommandText = deletestr;
command.ExecuteNonQuery();
MessageBox.Show("学生的信息删除成功!", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
connection.Close();
方二:
string str = "select * from Student where studentnum='" + snum + "'";
string deletestr = "delete from Student where studentnum='" + snum + "'";
SqlDataAdapter sda = new SqlDataAdapter(str, connection);
DataSet ds = new DataSet();
sda.Fill(ds, "Student");
DataTable dt = ds.Tables["Student"];
if (dt.Rows.Count > 0)
{
dt.Rows[0].Delete();
sda.DeleteCommand = new SqlCommand(deletestr, connection);
sda.Update(ds, "Student");
MessageBox.Show("学生信息删除成功!", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
MessageBox.Show("此学号对应的学生不存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}


猜你喜欢
- PHP hebrev() 函数实例反向显示希伯来字符:<?php echo hebrev("á çù&
- call_user_func函数类似于一种特别的调用函数的方法,使用方法如下: function a($b,$c) { echo $b; e
- 15分钟学会vue项目改造成SSRPs:网上看了好多服务器渲染的例子,基本都是从0开始的,用Nuxt或者vue官网推荐的ssr方案(vue-
- 前言之前看到一个有意思的开源项目,主要是可以将一张照片变成卡通漫画的风格。下面给大家放几张官方给出的部分效果图。看到这个效果图,还是非常经验
- OpenCV简介OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux、Windows、Android和Ma
- 一, 创建用户: 命令:CREATE USER 'usern
- 使用模块requests方式代码如下:import requests url_string="https://******&quo
- 这里是WMP的版本ClassID,从WMP7后ID就成了clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6
- Tensorboard:如何更直观的观察数据在神经网络中的变化,或是已经构建的神经网络的结构。上一篇文章说到,可以使用matplotlib第
- 在《javascript设计模式》中对这种方法作了比较详细的描述,实现方法的链式调用,只须让在原型中定义的方法都返回调用这些方法的实例对象的
- 什么是装饰器从字面意思上来看,装饰器是用来装饰其他东西的工具。在python中装饰器分为函数装饰器和类装饰器。简而言之,函数装饰器是用来装饰
- 这篇文章主要介绍了pandas 空数据处理方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可
- 环境:python3.5,pycharm2017.2.3目录结构a.pyt=5b.pyfrom a import tprint(t)平台显示
- 在我们的日常生活工作中,经常会遇到需要上传日志的场景,比如多台机器运行同一个程序,并且需要记录每台机器程序产生的日志,根据相关关键词告警,或
- 具体流程:① 导入相应的包,下载训练集和测试集对应需要的图像数据。②进行图像数据的变换,使图像数据转化成pytorch可识别并计算的张量数据
- 本文实例讲述了Go语言结构体定义和使用方法。分享给大家供大家参考。具体分析如下:一个结构体(struct)就是一个字段的集合。(而 type
- 前言在JavaScript中,数据类型分为两大类,一种是基础数据类型,另一种则是复杂数据类型,又叫引用数据类型基础数据类型:数字Number
- 不管是写自定义标签还是过滤器,第一件要做的事是创建模板库(Django能够导入的基本结构)。创建一个模板库分两步走:
- 最基本的MMM安装必须至少需要2个数据库服务器和一个监控服务器下面要配置的MySQL Cluster环境包含四台数据库服务器和一台监控服务器
- 一、避免Firefox 背景图不显示的兼容问题,定义background 属性,先后顺序不能随意变动。background : backgr