VS2010连接SQL Server 2008并执行查询操作
2012-09-11 18:42
363 查看
VS2010连接SQL Server 2008并执行查询操作
作者:CambridgeacmVS2010连接SQL Server 2008并执行查询操作
先在SQL Server 2008中建一个Student数据库,含有一个表student,4个字段,分别为姓名(varchar)学号(varchar)性别(varchar)年龄(int),并指定一个用户登录该数据库,用户名为cam,密码为123456,注意要修改cam用户的权限
![](http://img.my.csdn.net/uploads/201209/11/1347340443_6171.jpg)
新建控制台应用程序,连接数据库,输出student表中的所有字段,并执行插入删除操作
using System; using System.Data; using System.Data.SqlClient; using System.Collections.Generic; using System.Linq; using System.Text; namespace 连接数据库 { class Program { public static int Insert(string name, string pwd,string sex,int age) { SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Student;Integrated Security=True");//Initial Catalog后面跟你数据库的名字 conn.Open(); string sql = "insert into student(姓名,学号,性别,年龄) values(@name,@pwd,@sex,@age)"; SqlCommand cmd = new SqlCommand(sql, conn); SqlParameter parn1 = new SqlParameter("@name", name); cmd.Parameters.Add(parn1); SqlParameter parn2 = new SqlParameter("@pwd", pwd); cmd.Parameters.Add(parn2); SqlParameter parn3 = new SqlParameter("@sex", sex); cmd.Parameters.Add(parn3); SqlParameter parn4 = new SqlParameter("@age", age); cmd.Parameters.Add(parn4); int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示添加成功 conn.Close(); cmd.Dispose(); return result; } public static int Update(string name) { SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Student;Integrated Security=True");//Initial Catalog后面跟你数据库的名字 conn.Open(); string sql = "delete from student where 姓名=@name"; SqlCommand cmd = new SqlCommand(sql, conn); SqlParameter parn = new SqlParameter("@name",name); cmd.Parameters.Add(parn); int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示删除成功 conn.Close(); cmd.Dispose(); return result; } static void Main(string[] args) { //指定Sql Server提供者的连接字符串 string connString = "server=CAMBRIDGE-PC;database =Student;uid=cam;pwd=123456"; //建立连接对象 SqlConnection Sqlconn = new SqlConnection(connString); //打开连接 Sqlconn.Open(); //为上面的连接指定Command对象 SqlCommand thiscommand = Sqlconn.CreateCommand(); thiscommand.CommandText = "select 姓名,学号,性别,年龄 from student"; //为指定的command对象执行DataReader SqlDataReader thisSqlDataReader = thiscommand.ExecuteReader(); while (thisSqlDataReader.Read()) { Console.WriteLine("{0} {1} {2} {3}", thisSqlDataReader["姓名"], thisSqlDataReader["学号"], thisSqlDataReader["性别"], thisSqlDataReader["年龄"]); } //关闭读取 thisSqlDataReader.Close(); int result = Insert("关羽", "E01014307", "男", 25); Console.WriteLine("影响的行数为:{0}", result); result = Update("关羽"); Console.WriteLine("影响的行数为:{0}", result); //关闭连接 Sqlconn.Close(); Console.ReadLine(); } } }
![](http://img.my.csdn.net/uploads/201209/11/1347340448_1045.jpg)
建Windows窗体应用程序也可以,在Form窗体中拖一个DataGridView控件,插入一个学生的信息,在DataGridView控件中显示所有学生的信息
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace cam { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static int Insert(string name, string pwd, string sex, int age) { SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Student;Integrated Security=True");//Initial Catalog后面跟你数据库的名字 conn.Open(); string sql = "insert into student(姓名,学号,性别,年龄) values(@name,@pwd,@sex,@age)"; SqlCommand cmd = new SqlCommand(sql, conn); SqlParameter parn1 = new SqlParameter("@name", name); cmd.Parameters.Add(parn1); SqlParameter parn2 = new SqlParameter("@pwd", pwd); cmd.Parameters.Add(parn2); SqlParameter parn3 = new SqlParameter("@sex", sex); cmd.Parameters.Add(parn3); SqlParameter parn4 = new SqlParameter("@age", age); cmd.Parameters.Add(parn4); int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示添加成功 conn.Close(); cmd.Dispose(); return result; } public static int Update(string name) { SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Student;Integrated Security=True");//Initial Catalog后面跟你数据库的名字 conn.Open(); string sql = "delete from student where 姓名=@name"; SqlCommand cmd = new SqlCommand(sql, conn); SqlParameter parn = new SqlParameter("@name", name); cmd.Parameters.Add(parn); int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示删除成功 conn.Close(); cmd.Dispose(); return result; } public DataTable sel() { SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Student;Integrated Security=True");//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=. conn.Open(); string sql = "select * from student"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); conn.Close(); cmd.Dispose(); return dt; } private void Form1_Load(object sender, EventArgs e) { Insert("关羽", "E01014307", "男", 25); dataGridView1.DataSource = sel(); } } }
![](http://img.my.csdn.net/uploads/201209/11/1347340453_5342.jpg)
相关文章推荐
- VS2010连接SQL Server 2008并执行查询操作
- VS2010连接SQL Server 2008并执行查询操作
- VS2010连接SQL Server 2008并执行查询操作
- VS2010连接SQL Server 2008并执行查询操作
- python连接sql server并执行查询操作
- VS2010连接SQL Server 2008操作与编程 (笔记)
- 使用VS2010连接SQL Server 2008数据库混合编程以实现对数据库的操作
- Visual C# 2008+SQL Server 2005 数据库与网络开发--11.2.2 LINQ的基本查询操作
- 在SQL Server 2005中连接Oracle,完成查询、插入操作
- 腾讯云图片鉴黄集成到C# SQL Server 怎么在分页获取数据的同时获取到总记录数 sqlserver 操作数据表语句模板 .NET MVC后台发送post请求 百度api查询多个地址的经纬度的问题 try{}里有一个 return 语句,那么紧跟在这个 try 后的 finally {}里的 code 会 不会被执行,什么时候被执行,在 return 前还是后? js获取某个日期
- VS2010 自带的SQL SERVER 2008 EXPRESS 如何连接?
- SQL SERVER 2008用Select操作处理数据(二)——查询语句的逻辑流
- Sql Server 函数的操作实例!(执行多条语句,返回Select查询后的临时表)
- 利用SQL Server 2008中的SSIS进行大规模的数据库查询操作 推荐
- c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程
- Java连接SQL Server 2008和My SQL数据库的简单操作(2)
- Spark 连接mysql 执行数据查询操作实战--多表查询
- VS2010中MFC用ADO方式连接 SQL SERVER 2008 EXPRESS的步骤。
- Java连接SQL Server 2008和My SQL数据库的简单操作(1)
- C#使用oledb连接excel执行Insert Into语句出现“操作必须使用一个可更新的查询”的解决办法