SQL Server 数据库在C#编程中经常用到,如何实现在具体项目中数据库和具体应用的结合是我们经常遇到的问题,我们这次主要针对如何使用SQL Server 数据库展开,下面是具体的操作以及简单的代码实现,希望对大家有帮助 。
环境:
Windows 7 旗舰版 x86
Visual Studio 2010 旗舰版
SQL Server 2008数据库
.NET Framework 4
步骤一:打开Visual Studio 2010,新建项目,选择Windows应用程序,取名
smSQLServerTest,点击确定,建立项目。
步骤二:为Form1重命名为MainForm,界面设计如下:

步骤三:右键设计窗体进入代码区,在开始部分添加命名空间的引用
using System.Data.SqlClient;
步骤四:定义数据库连接对象为全局变量,代码位置与连接,查询等函数并列
SqlConnection myconnection;//定义一个数据库连接对象
第一部分 SQL Server数据库的连接
下面我们开始实现SQL Server 数据库的连接功能 。
在连接按钮的Click事件里添加代码:
//数据库连接
private void btConnect_Click(objectsender, EventArgs e)
{try{myconnection= new SqlConnection("Integrated Security=SSPI;Initial Catalog=人事管理系统;Data Source=SHAOYONG-PC\\MYSQLSERVER;User ID=sa;Password=gis123");
myconnection.Open();//打开数据库
label1.Text= "数据库连接成功!";
}catch(Exception ee)
{
MessageBox.Show("数据库连接失败!" +ee.ToString());
}
}
第二部分 SQL Server数据库的查询
在查询按钮的Click事件里添加代码:
private void btQueryAll_Click(objectsender, EventArgs e)
{try{string SQL = "select * From 部门表";
SqlDataAdapter objDataAdpter= newSqlDataAdapter();
objDataAdpter.SelectCommand= newSqlCommand(SQL, myconnection);
DataSet ds= newDataSet();
objDataAdpter.Fill(ds,"部门表");
dataGridView1.DataSource= ds.Tables[0];
}catch(Exception ee)
{
MessageBox.Show("查询失败!" +ee.ToString());
}
}
第三部分 SQL Server数据库中记录的插入
在插入按钮的Click事件里添加代码:
private void btInsertToDatabase_Click(objectsender, EventArgs e)
{try{string strSQL1 = "insert into 部门表(部门名,部门号,管理者) values('销售部',97003,'李四')";
SqlDataAdapter objDataAdpter= newSqlDataAdapter();
SqlCommand thisCommand= newSqlCommand(strSQL1, myconnection);
thisCommand.ExecuteNonQuery();string strSQL2 = "select * From 部门表";
SqlDataAdapter objDataAdpter1= newSqlDataAdapter();
objDataAdpter1.SelectCommand= newSqlCommand(strSQL2, myconnection);
DataSet ds= newDataSet();
objDataAdpter1.Fill(ds,"部门表");
dataGridView1.DataSource= ds.Tables[0];
}catch(Exception ee)
{
MessageBox.Show("插入数据失败!" +ee.ToString());
}
}
第四部分 SQL Server数据库中记录的修改
在修改按钮的Click事件里添加代码:
private void btModifiFeildValue_Click(objectsender, EventArgs e)
{try{string strSQL1 = "update 部门表 set 管理者='张五' where 部门号=97002";
SqlCommand thisCommand= newSqlCommand(strSQL1, myconnection);
thisCommand.ExecuteNonQuery();string strSQL2 = "select * From 部门表";
SqlDataAdapter objDataAdpter1= newSqlDataAdapter();
objDataAdpter1.SelectCommand= newSqlCommand(strSQL2, myconnection);
DataSet ds= newDataSet();
objDataAdpter1.Fill(ds,"部门表");
dataGridView1.DataSource= ds.Tables[0];
}catch(Exception ee)
{
MessageBox.Show("更新数据失败!" +ee.ToString());
}
}
第五部分 SQL Server数据库中记录的删除
在删除按钮的Click事件里添加代码:
private void btDeleteFeildValue_Click(objectsender, EventArgs e)
{try{string strSQL1 = "delete from 部门表 where 部门号=97002";
SqlCommand thisCommand= newSqlCommand(strSQL1, myconnection);
thisCommand.ExecuteNonQuery();string strSQL2 = "select * From 部门表";
SqlDataAdapter objDataAdpter1= newSqlDataAdapter();
objDataAdpter1.SelectCommand= newSqlCommand(strSQL2, myconnection);
DataSet ds= newDataSet();
objDataAdpter1.Fill(ds,"部门表");
dataGridView1.DataSource= ds.Tables[0];
}catch(Exception ee)
{
MessageBox.Show("删除数据失败!" +ee.ToString());
}
}
第六部分 SQL Server数据库的关闭
在类里添加函数 string DisConnect(),代码如下:
///
//断开与SQL Server数据库的连接
///
public stringDisConnect()
{stringResult;try{
myconnection.Close();
Result= "数据连接已断开!";
}catch(Exception e)
{
MessageBox.Show("数据库断开失败!" +e.ToString());
Result= "连接成功!";
}returnResult;
}
在断开连接按钮的Click事件里添加代码:
private void btDisConnect_Click(objectsender, EventArgs e)
{
label1.Text=DisConnect();
}
本文详细介绍了如何在C#环境下使用Visual Studio 2010与SQL Server 2008进行数据库编程,包括数据库连接、查询、插入、修改和删除操作,提供了具体的代码示例,旨在帮助开发者实现数据库与应用的紧密结合。

8866

被折叠的 条评论
为什么被折叠?



