Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 296|回复: 0

[默认分类] VS2010连接SQL Server 2008并执行查询操作

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-6-8 10:42:20 | 显示全部楼层 |阅读模式


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


      
    新建控制台应用程序,连接数据库,输出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. }
    复制代码



    建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 = 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. }
    复制代码







    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-24 09:59 , Processed in 0.532374 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表