现在的位置: 首页 > 综合 > 正文

VS2010连接SQL Server 2008并执行查询操作

2013年08月22日 ⁄ 综合 ⁄ 共 4168字 ⁄ 字号 评论关闭

VS2010连接SQL Server 2008并执行查询操作

先在SQL Server 2008中建一个Student数据库,含有一个表student,4个字段,分别为姓名(varchar)学号(varchar)性别(varchar)年龄(int),并指定一个用户登录该数据库,用户名为cam,密码为123456,注意要修改cam用户的权限

VS2010

 

新建控制台应用程序,连接数据库,输出student表中的所有字段,并执行插入删除操作

 

  1. using System;  

  2. using System.Data;  

  3. using System.Data.SqlClient;  

  4. using System.Collections.Generic;  

  5. using System.Linq;  

  6. using System.Text;  

  7. namespace 连接数据库  

  8. {  

  9.     class Program  

  10.     {  

  11.         public static int Insert(string name, string pwd,string sex,int age)  

  12.         {  

  13.             SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Student;Integrated Security=True");//Initial Catalog后面跟你数据库的名字   

  14.             conn.Open();  

  15.             string sql = "insert into student(姓名,学号,性别,年龄) values(@name,@pwd,@sex,@age)";  

  16.             SqlCommand cmd = new SqlCommand(sql, conn);  

  17.             SqlParameter parn1 = new SqlParameter("@name", name);  

  18.             cmd.Parameters.Add(parn1);  

  19.             SqlParameter parn2 = new SqlParameter("@pwd", pwd);  

  20.             cmd.Parameters.Add(parn2);  

  21.             SqlParameter parn3 = new SqlParameter("@sex", sex);  

  22.             cmd.Parameters.Add(parn3);  

  23.             SqlParameter parn4 = new SqlParameter("@age", age);  

  24.             cmd.Parameters.Add(parn4);  

  25.             int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示添加成功   

  26.             conn.Close();  

  27.             cmd.Dispose();  

  28.             return result;  

  29.         }  

  30.         public static int Update(string name)  

  31.         {  

  32.             SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Student;Integrated Security=True");//Initial Catalog后面跟你数据库的名字   

  33.             conn.Open();  

  34.             string sql = "delete from student where 姓名=@name";  

  35.             SqlCommand cmd = new SqlCommand(sql, conn);  

  36.             SqlParameter parn = new SqlParameter("@name",name);  

  37.             cmd.Parameters.Add(parn);  

  38.             int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示删除成功   

  39.             conn.Close();  

  40.             cmd.Dispose();  

  41.             return result;  

  42.         }  

  43.         static void Main(string[] args)  

  44.         {  

  45.             //指定Sql Server提供者的连接字符串   

  46.             string connString = "server=CAMBRIDGE-PC;database =Student;uid=cam;pwd=123456";  

  47.             //建立连接对象   

  48.             SqlConnection Sqlconn = new SqlConnection(connString);  

  49.             //打开连接   

  50.             Sqlconn.Open();  

  51.             //为上面的连接指定Command对象   

  52.             SqlCommand thiscommand = Sqlconn.CreateCommand();  

  53.             thiscommand.CommandText = "select 姓名,学号,性别,年龄 from student";  

  54.             //为指定的command对象执行DataReader   

  55.             SqlDataReader thisSqlDataReader = thiscommand.ExecuteReader();  

  56.             while (thisSqlDataReader.Read())  

  57.             {  

  58.                 Console.WriteLine("{0} {1} {2} {3}", thisSqlDataReader["姓名"], thisSqlDataReader["学号"], thisSqlDataReader["性别"], thisSqlDataReader["年龄"]);  

  59.             }  

  60.             //关闭读取   

  61.             thisSqlDataReader.Close();  

  62.             int result = Insert("关羽""E01014307""男", 25);  

  63.             Console.WriteLine("影响的行数为:{0}", result);  

  64.             result = Update("关羽");  

  65.             Console.WriteLine("影响的行数为:{0}", result);  

  66.             //关闭连接   

  67.             Sqlconn.Close();  

  68.             Console.ReadLine();  

  69.         }  

  70.     }  

  71. }  

 

VS2010

 

建Windows窗体应用程序也可以,在Form窗体中拖一个DataGridView控件,插入一个学生的信息,在DataGridView控件中显示所有学生的信息

  1. using System;  

  2. using System.Collections.Generic;  

  3. using System.ComponentModel;  

  4. using System.Data;  

  5. using System.Data.SqlClient;  

  6. using System.Drawing;  

  7. using System.Linq;  

  8. using System.Text;  

  9. using System.Windows.Forms;  

  10. namespace cam  

  11. {  

  12.     public partial class Form1 : Form  

  13.     {  

  14.         public Form1()  

  15.         {  

  16.             InitializeComponent();  

  17.         }  

  18.         public static int Insert(string name, string pwd, string sex, int age)  

  19.         {  

  20.             SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Student;Integrated Security=True");//Initial Catalog后面跟你数据库的名字   

  21.             conn.Open();  

  22.             string sql = "insert into student(姓名,学号,性别,年龄) values(@name,@pwd,@sex,@age)";  

  23.             SqlCommand cmd = 

抱歉!评论已关闭.