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

编程入门 行业动态 更新时间:2024-10-23 02:11:18

VS2010连接SQL Server 2008并执行查询<a href=https://www.elefans.com/category/jswz/34/1770947.html style=操作"/>

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

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

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

 

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

 

[csharp] view plain copy print ?
  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. }  
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对象执行DataReaderSqlDataReader 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();}}
}

 

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

[csharp] view plain copy print ?
  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 = new SqlCommand(sql, conn);  
  24.             SqlParameter parn1 = new SqlParameter("@name", name);  
  25.             cmd.Parameters.Add(parn1);  
  26.             SqlParameter parn2 = new SqlParameter("@pwd", pwd);  
  27.             cmd.Parameters.Add(parn2);  
  28.             SqlParameter parn3 = new SqlParameter("@sex", sex);  
  29.             cmd.Parameters.Add(parn3);  
  30.             SqlParameter parn4 = new SqlParameter("@age", age);  
  31.             cmd.Parameters.Add(parn4);  
  32.             int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示添加成功   
  33.             conn.Close();  
  34.             cmd.Dispose();  
  35.             return result;  
  36.         }  
  37.         public static int Update(string name)  
  38.         {  
  39.             SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Student;Integrated Security=True");//Initial Catalog后面跟你数据库的名字   
  40.             conn.Open();  
  41.             string sql = "delete from student where 姓名=@name";  
  42.             SqlCommand cmd = new SqlCommand(sql, conn);  
  43.             SqlParameter parn = new SqlParameter("@name", name);  
  44.             cmd.Parameters.Add(parn);  
  45.             int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示删除成功   
  46.             conn.Close();  
  47.             cmd.Dispose();  
  48.             return result;  
  49.         }  
  50.         public DataTable sel()  
  51.         {  
  52.             SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Student;Integrated Security=True");//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.   
  53.             conn.Open();  
  54.             string sql = "select * from student";  
  55.             SqlCommand cmd = new SqlCommand(sql, conn);  
  56.             SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  57.             DataTable dt = new DataTable();  
  58.             sda.Fill(dt);  
  59.             conn.Close();  
  60.             cmd.Dispose();  
  61.             return dt;  
  62.         }   
  63.         private void Form1_Load(object sender, EventArgs e)  
  64.         {  
  65.             Insert("关羽", "E01014307", "男", 25);  
  66.             dataGridView1.DataSource = sel();  
  67.         }  
  68.     }  
  69. }  
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();}}
}

转载于:.html

更多推荐

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

本文发布于:2024-02-25 09:15:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1698483.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:操作   SQL   Server

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!