C#操作数据库总结

时间:2024.5.14

开发工具:Microsoft Visual Studio 20xx

数据库:Microsoft SQL Server 20xx

说明:这里建立的数据库名为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('20xx1000010201','张扬')

二、代码:

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

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

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();

}

(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);

}


第二篇:C#操作数据库总结


发工具:Microsoft Visual Studio 20xx

数据库:Microsoft SQL Server 20xx

说明:这里建立的数据库名为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('20xx1000010201','张扬')

二、代码:

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

(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);

}

更多相关推荐:
04-张全羚-数据库总结

SqlServer数据库原理期末总结姓名:张全羚学号:1304091004班级:13软件工程(1)班一、知识点分析(举例)1、存储过程简介SqlServer的存储过程是一个被命名的存储在服务器上的Transac…

数据库总结

承德石油高等专科学校计算机与信息工程系岗前实训总结报告实训方向或岗位:软件工程师实训地点或单位:文理楼B518所属班级:软件0901学号:29姓名:刘志聪指导教师:苏建华实训时间:20xx.10.10-20xx…

分布式数据库总结

分布式数据库介绍自学、整理、备忘它和集中式操作系统的区别在于资源管理、进程通信和系统结构等方面。分布式程序设计语言用于编写运行于分布式计算机系统上的分布式程序。一个分布式程序由若干个可以独立执行的程序模块组成,…

数据库总结

1、查询数据库,将查询出来的内容加入数值中加入内容。其中列名也会改变。将会用到连接符,如果是orcale将用||连接符。这样列名也会改变。selectprod_code||'wo'||'adf'fromtdcj…

数据库总结

(一)E-R图1.学校中有若干系,每个系有若干班级和教研室,每个教研室有若干教员,其中有的教授和副教授每人各带若干研究生。每个班有若干学生,每个学生选修若干课程,每门课可有若干学生选修。用E-R图画出此学校的信…

数据库总结1

论文类数据库(1)数据库EBSCO主要收录的是学术期刊文献,其中最主要的也是对我们有很大用处两个全文数据库是ASC(AcademicSourceComplete)和BSC(BusinessSourceCompl…

学生成绩管理数据库总结报告

项目总结报告项目总结报告.............................................................................................…

空间数据库总结

第一章:1.简述空间数据的结构特点及用传统商用关系数据库管理空间数据的局限性:答:1.结构特点:(1)从数据组织和管理角度看,空间数据与一般的事务数据相比具有非结构化特征(2)相对于一般的事务数据而言,空间数据…

数据库总结

数据库老师画的重点数据库:数据库是长期存储在计算机内、有组织、可共享的大数据集合。数据库操纵功能:查询select、插入insert、删除delete、修改update数据库系统的特点:1数据结构化2数据的共享…

数据库总结

数据库考试要点总结第一章:1.数据管理技术发展的三个阶段以及各自的特点?答:数据管理技术的发展可以大体归为三个阶段:人工管理阶段、文件系统阶段和数据库系统阶段。人工管理数据具有以下特点:数据不保存在计算机内;需…

数据库总结

数据库增删改查代码插入数据:Insert[into]表名(列名多个用,号分隔)values(值名)提取数据放到已存在的新表中:Insertinto插入的新表名(列名)Select(值名)From(原表名)提取数…

数据库总结

1DBMS系统的功能:数据定义功能,数据操纵,优化和执行,数据安全和完整性,数据恢复合并发,数据字典,性能2数据库系统的发展阶段A文件管理:编写应用程序不方便,数据冗余不可避免,应用程序依赖性,不支持对文件的并…

数据库总结(68篇)