一、Sql Server 在Visual Studio的连接有两种方法:

(1)本地计算机连接;

[c#] view plaincopy

 
 
  1. string s = "Data Source=计算机名称;initial Catalog=数据库名称;integrated Security=True";

(2)windows身份验证方式连接;

[c#] view plaincopy

 
 
  1. string cc="Data Source = 计算机名称; Initial Catalog = 数据库名称; User ID = sa; Password = 你的密码";

二、在Visual Studio中使用:

例1:查询数据库中的数据并且显示出来

[c#] view plaincopy

 
 
  1. string s = "Data Source=计算机名称;Initial Catalog=数据库名称;Integrated Security=True";  //此处使用本地计算机连接方式
  2. SqlConnection conn = new SqlConnection(s);   //创建连接
  3. conn.Open();    //打开连接
  4. SqlCommand cmd = conn.CreateCommand();
  5. cmd.CommandText = "select * from T_User";   //使用命令
  6. SqlDataAdapter adapter=new SqlDataAdapter(cmd);
  7. DataTable dt=new DataTable();
  8. adapter.Fill(dt);
  9. conn.Dispose();  //释放所以资源
  10. cmd.Dispose();
  11. conn.Close();  //关闭连接
  12. string realname="";
  13. string username="";
  14. string mobile="";
  15. string address="";
  16. for (int i=0;i<dt.Rows.Count;i++)
  17. {
  18. realname=dt.Rows[i][3].ToString();
  19. username=dt.Rows[i][1].ToString();
  20. mobile=dt.Rows[i][4].ToString();
  21. address=dt.Rows[i][5].ToString();
  22. Console.WriteLine("姓名为{0},用户名为{1},手机为{2},地址为{3}", realname, username, mobile, address);
  23. }
  24. Console.ReadKey();

例2:删除表中数据

[c#] view plaincopy

 
 
  1. string cc="Data Source = 计算机名称; Initial Catalog = 数据库名称; User ID = sa; Password = 你的密码";   //使用windows身份验证
  2. SqlConnection conn = new SqlConnection(s);
  3. conn.Open();
  4. SqlCommand cmd = conn.CreateCommand();
  5. cmd.CommandText = "delete from T_User where Id=5";
  6. cmd.ExecuteNonQuery();
  7. cmd.Dispose();
  8. conn.Close();
  9. Console.WriteLine("删除成功");
  10. Console.ReadKey();

例3:修改表中数据

[c#] view plaincopy

 
 
  1. string s = "Data Source=计算机名称;initial Catalog=数据库名称;integrated Security=True";
  2. SqlConnection conn = new SqlConnection(s);
  3. conn.Open();
  4. SqlCommand cmd = conn.CreateCommand();
  5. cmd.CommandText = "update T_User set Card=@card where ID=3";
  6. cmd.Parameters.AddWithValue("@card", "13000000000000");
  7. cmd.ExecuteNonQuery();
  8. cmd.Dispose();
  9. conn.Close();
  10. conn.Dispose();
  11. Console.WriteLine("修改成功!");
  12. Console.ReadKey();

例4:向表中插入数据

 
[c#] view plaincopy

 
 
    1. string s = "data source=计算机名称;initial catalog=数据库名称;integrated security=true";
    2. SqlConnection conn = new SqlConnection(s);
    3. conn.Open();
    4. SqlCommand cmd = conn.CreateCommand();
    5. cmd.CommandText = "insert into T_User(UserName,Password,RealName,Mobile,Address) values(@username,@password,@realname,@mobile,@address)";
    6. cmd.Parameters.AddWithValue("@username", "xingxing");
    7. cmd.Parameters.AddWithValue("@password", "77777");
    8. cmd.Parameters.AddWithValue("@realname", "星星");
    9. cmd.Parameters.AddWithValue("@mobile", 1300000000);
    10. cmd.Parameters.AddWithValue("@address", "河北省北京市");
    11. cmd.ExecuteNonQuery();
    12. cmd.Dispose();
    13. conn.Close();
    14. conn.Dispose();
    15. Console.WriteLine("成功插入一行");
    16. Console.ReadKey();

SQLServer 在Visual Studio的连接方法的更多相关文章

  1. SQLServer 在Visual Studio的2种连接方法

    一.Sql Server 在Visual Studio的连接有两种方法: (1)本地计算机连接; string s = "Data Source=计算机名称;initial Catalog= ...

  2. win7兼容visual studio 2005 的方法

    http://blog.sina.com.cn/s/blog_74d572890100xv7p.html 今天花了4个小时,结合网上的介绍,本人终于找到了一个可以在win7环境下运行visual st ...

  3. 安装 Visual Studio,连接中国区 Azure

    中国数据中心 目前,中国区 Azure 有两个数据中心,在位置字段中显示为“中国北部”和“中国东部”. 在 Azure 上创建应用程序的区别 在中国区 Azure 上开发应用程序与在境外 Azure ...

  4. Visual Studio 2019连接MySQL数据库详细教程

    前言 如果要在 Visual Studio 2019中使用MySQL数据库,首先需要下载MySQL的驱动 Visual Studio默认只显示微软自己的SQL Server数据源,点击其它也是微软自己 ...

  5. 64 位win 7或windows 8下的visual studio不能连接Oracle数据库调试网站的问题

    在64 位win 7或windows 8系统下,visual studio直接F5运行网站调试,你会发现不能连接Oracle数据库,会报一个“ORA-06413: Connection not ope ...

  6. visual studio 2013连接Oracle 11g并获取数据:(一:环境搭建)

    C# WinForm案例: 目标: visual studio 中点击按钮,就可获取到Oracle中数据表的内容 1.安装Visual Studio 2013 ,推荐如下网址,下载ISO镜像,一路ne ...

  7. Visual Studio 2017 连接Oracle

    VS 2017 连接 Oracle 12 因为Visual Studio自带的数据文件已经不能支持超过10g以上的了,所以需要另外 下载插件 本机环境 宿主机的环境:win7,Visual Studi ...

  8. 关于Visual studio 2017安装方法的若干问题

    因为忙于生活,好几年没有看关于编程方面的书了,这几天闲,就准备在电脑上装上VS的最新版本,查了查,最新版是VS2017,.搜了下网上安装后大小,还真不小.下载离线安装包,完全下载居然需要25G左右,无 ...

  9. 分享:扩展Visual Studio 的简单方法

    作为 MS 阵营的码农,相信Visual Studio 肯定是大家的主要武器了,但不知道大家有没有扩展Visual Studio 的需求. 最近我需要做一个工具,发现最好是实现在VS里面,于是,Goo ...

随机推荐

  1. Spring Timer 两种实现

    有两种流行Spring定时器配置:Java的Timer类和OpenSymphony的Quartz.1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 impo ...

  2. C#.NET 打印连续纸高度动态变化(基于长江支流的金质打印通)

    问题是这样的,打印机使用的是卷筒的连续纸,要打印的内容因为数据行数不同,高度会有变化.这时如果能在打印时动态改变纸张大小(其实只改变高度即可)当然是最好的选择. 我使用了网上久负盛名的[长江支流]的“ ...

  3. BZOJ2301: [HAOI2011]Problem b 莫比乌斯反演

    分析:对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 然后对于求这样单个的gcd(x,y)=k的, ...

  4. 关于this的小总结

    关于对this目前有几点小理解 ①this指向调用它的元素.就是谁调用它,它就指向谁. ②可以通过call()和apply()改变this的指向 ③setInterval和setTimeout中的th ...

  5. codeforce 621D - Rat Kwesh and Cheese

    题意:求表达式中最大的值. long double 128位 有效数字18-19 范围正负1.2*10^4932 注意取对数! #include<iostream> #include< ...

  6. 关于Windows Azure的常见问题-一般问题FAQ

    一般问题 什么是Windows Azure? Windows Azure 是一个灵活而开放的云平台,通过该平台,您可以在数据中心快速生成.部署和管理应用程序.Windows Azure 支持所有主流操 ...

  7. MTRR内存类型范围寄存器

    1.MTRR的概念 内存类型范围寄存器(MTRRs,翻译过来真别扭,后面都以MTRR直接来说了)提供了一种机制,这种机制其实就是确定在系统内存中物理一段内存的类型.这个类型其实是正对CPU来说的,见图 ...

  8. 【Java基础】Java多线程小结

    在说多线程之前,首先要清楚为啥要提出多线程,这就要明白线程和进程间的区别了. 线程和进程间的区别 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立单 ...

  9. Codeforces Round #313 (Div. 2) D.Equivalent Strings (字符串)

    感觉题意不太好懂 = =# 给两个字符串 问是否等价等价的定义(满足其中一个条件):1.两个字符串相等 2.字符串均分成两个子串,子串分别等价 因为超时加了ok函数剪枝,93ms过的. #includ ...

  10. Activity 的启动模式

    好久没用过那几种activity的启动模式了,如今看到singletop竟然傻了眼,完全忘记了这几种启动模式的区别!隧将两年前的总结翻出来,通读一遍那晦涩难懂的记录,又理解了一遍,在以前记录的基础上, ...