当前位置:  编程技术>.net/c#/asp.net

C#操作数据库总结(vs2005+sql2005)

    来源: 互联网  发布时间:2014-08-30

    本文导语:  开发工具:Microsoft Visual Studio 2005 数据库:Microsoft SQL Server 2005 说明:数据库名为Demo,有一个学生表Student,为操作方便起见,我只添加两个字段:studentnum和studentname。 一、SQL语句:   代码如下: --create database Demo use Demo create t...

开发工具: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)初始化连接字符串:
方式1 connectionstr="server=localhost;uid=sa;pwd=123456;database=Demo";
方式2 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);
}

    
 
 

您可能感兴趣的文章:

  • c#对象中两种copy操作:深拷贝(Deep Copy)与浅拷贝(Shallow Copy)
  • c#的时间日期操作示例分享(c#获取当前日期)
  • .NET下 c#通过COM组件操作并导出Excel实例代码
  • C#操作txt文件,进行清空添加操作的小例子
  • C#实现装箱与拆箱操作简单实例
  • 浅谈C#互操作的内存溢出问题
  • C# 中的??操作符浅谈
  • c#剪切板操作的简单实例
  • c# 调用Surfer软件,添加引用的具体操作方法
  • c#异步task示例分享(异步操作)
  • 网络技术 iis7站长之家
  • C#操作CLOB大对象的代码一例
  • c#判断操作系统位数实例代码
  • 一些关于c#与Sql的时间的操作
  • c#判断操作系统位数的示例分享
  • C#中的位操作小结
  • C# 操作符之三元操作符浅析
  • C# Dictionary操作范例(入门新手参考)
  • C#的WebBrowser操作frame实例解析
  • C# Winform 操作 INI 配置文件的实现代码
  • C#程序最小化到托盘图标操作步骤与实现代码
  • 把liunx 2005桌面型操作系统干掉,XP启动不起来啦
  • sql2005 create file遇到操作系统错误5拒绝访问 错误1802
  • SQL Server 2005降级到2000的正确操作步骤分享
  • C#操作数据库总结(vs2005+sql2005)
  • CMD命令操作MSSQL2005数据库(命令整理)
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • windows7操作系统介绍及各种使用技巧总结
  • JQuery对表格进行操作的常用技巧总结
  • jquery操作复选框(checkbox)的12个小技巧总结
  • java中的正则操作方法总结
  • 基于java中正则操作的方法总结
  • C/C++位操作实例总结
  • C#字符串常见操作总结详解
  • C#文件路径操作详细总结
  • Codeigniter操作数据库表的优化写法总结
  • shell数组操作简明总结
  • 总结的7个shell字符串操作方法和实例分享
  • MySQL的字符集操作命令总结
  • C# 对XML基本操作代码总结
  • jquery操作input值方法总结
  • JQuery对表单元素的基本操作使用总结
  • 深入C++中构造函数、拷贝构造函数、赋值操作符、析构函数的调用过程总结
  • 深入C#判断操作系统类型的总结详解
  • MySQ索引操作命令总结(创建、重建、查询和删除索引命令详解)
  • java对XML文件的解析、节点的增加、删除操作总结
  • mysql 操作总结 INSERT和REPLACE
  • C++中复制构造函数和重载赋值操作符总结
  • C++ Stacks(堆栈) 成员 操作:比较和分配堆栈
  • 谁有操作系统PV操作的例子???谁有操作系统PV操作的例子???谢谢!!
  • C++ Strings(字符串) 成员 Operators:操作符,用于字符串比较和赋值
  • 已安装了Windows操作系统,还想安装Linux。却还想在开机选择操作系统时由Windows引导,请问如何操作。在线等待
  • C++ I/O 成员 flags():操作flags
  • 请问LINUX操作系统是怎样对外围设备进行操作的
  • C++ I/O 成员 width():操作域宽度
  • 什么样的操作最耗费服务器的IO操作?
  • Xcode介绍及创建工程和工程依赖操作步骤
  • 无操作系统下对U盘的操作


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3