SQLServer 在Visual Studio的连接方法
一、Sql Server 在Visual Studio的连接有两种方法:
(1)本地计算机连接;
- string s = "Data Source=计算机名称;initial Catalog=数据库名称;integrated Security=True";
(2)windows身份验证方式连接;
- string cc="Data Source = 计算机名称; Initial Catalog = 数据库名称; User ID = sa; Password = 你的密码";
二、在Visual Studio中使用:
例1:查询数据库中的数据并且显示出来
- string s = "Data Source=计算机名称;Initial Catalog=数据库名称;Integrated Security=True"; //此处使用本地计算机连接方式
- SqlConnection conn = new SqlConnection(s); //创建连接
- conn.Open(); //打开连接
- SqlCommand cmd = conn.CreateCommand();
- cmd.CommandText = "select * from T_User"; //使用命令
- SqlDataAdapter adapter=new SqlDataAdapter(cmd);
- DataTable dt=new DataTable();
- adapter.Fill(dt);
- conn.Dispose(); //释放所以资源
- cmd.Dispose();
- conn.Close(); //关闭连接
- string realname="";
- string username="";
- string mobile="";
- string address="";
- for (int i=0;i<dt.Rows.Count;i++)
- {
- realname=dt.Rows[i][3].ToString();
- username=dt.Rows[i][1].ToString();
- mobile=dt.Rows[i][4].ToString();
- address=dt.Rows[i][5].ToString();
- Console.WriteLine("姓名为{0},用户名为{1},手机为{2},地址为{3}", realname, username, mobile, address);
- }
- Console.ReadKey();
例2:删除表中数据
- string cc="Data Source = 计算机名称; Initial Catalog = 数据库名称; User ID = sa; Password = 你的密码"; //使用windows身份验证
- SqlConnection conn = new SqlConnection(s);
- conn.Open();
- SqlCommand cmd = conn.CreateCommand();
- cmd.CommandText = "delete from T_User where Id=5";
- cmd.ExecuteNonQuery();
- cmd.Dispose();
- conn.Close();
- Console.WriteLine("删除成功");
- Console.ReadKey();
例3:修改表中数据
- string s = "Data Source=计算机名称;initial Catalog=数据库名称;integrated Security=True";
- SqlConnection conn = new SqlConnection(s);
- conn.Open();
- SqlCommand cmd = conn.CreateCommand();
- cmd.CommandText = "update T_User set Card=@card where ID=3";
- cmd.Parameters.AddWithValue("@card", "13000000000000");
- cmd.ExecuteNonQuery();
- cmd.Dispose();
- conn.Close();
- conn.Dispose();
- Console.WriteLine("修改成功!");
- Console.ReadKey();
例4:向表中插入数据
- string s = "data source=计算机名称;initial catalog=数据库名称;integrated security=true";
- SqlConnection conn = new SqlConnection(s);
- conn.Open();
- SqlCommand cmd = conn.CreateCommand();
- cmd.CommandText = "insert into T_User(UserName,Password,RealName,Mobile,Address) values(@username,@password,@realname,@mobile,@address)";
- cmd.Parameters.AddWithValue("@username", "xingxing");
- cmd.Parameters.AddWithValue("@password", "77777");
- cmd.Parameters.AddWithValue("@realname", "星星");
- cmd.Parameters.AddWithValue("@mobile", 1300000000);
- cmd.Parameters.AddWithValue("@address", "河北省北京市");
- cmd.ExecuteNonQuery();
- cmd.Dispose();
- conn.Close();
- conn.Dispose();
- Console.WriteLine("成功插入一行");
- Console.ReadKey();
SQLServer 在Visual Studio的连接方法的更多相关文章
- SQLServer 在Visual Studio的2种连接方法
一.Sql Server 在Visual Studio的连接有两种方法: (1)本地计算机连接; string s = "Data Source=计算机名称;initial Catalog= ...
- win7兼容visual studio 2005 的方法
http://blog.sina.com.cn/s/blog_74d572890100xv7p.html 今天花了4个小时,结合网上的介绍,本人终于找到了一个可以在win7环境下运行visual st ...
- 安装 Visual Studio,连接中国区 Azure
中国数据中心 目前,中国区 Azure 有两个数据中心,在位置字段中显示为“中国北部”和“中国东部”. 在 Azure 上创建应用程序的区别 在中国区 Azure 上开发应用程序与在境外 Azure ...
- Visual Studio 2019连接MySQL数据库详细教程
前言 如果要在 Visual Studio 2019中使用MySQL数据库,首先需要下载MySQL的驱动 Visual Studio默认只显示微软自己的SQL Server数据源,点击其它也是微软自己 ...
- 64 位win 7或windows 8下的visual studio不能连接Oracle数据库调试网站的问题
在64 位win 7或windows 8系统下,visual studio直接F5运行网站调试,你会发现不能连接Oracle数据库,会报一个“ORA-06413: Connection not ope ...
- visual studio 2013连接Oracle 11g并获取数据:(一:环境搭建)
C# WinForm案例: 目标: visual studio 中点击按钮,就可获取到Oracle中数据表的内容 1.安装Visual Studio 2013 ,推荐如下网址,下载ISO镜像,一路ne ...
- Visual Studio 2017 连接Oracle
VS 2017 连接 Oracle 12 因为Visual Studio自带的数据文件已经不能支持超过10g以上的了,所以需要另外 下载插件 本机环境 宿主机的环境:win7,Visual Studi ...
- 关于Visual studio 2017安装方法的若干问题
因为忙于生活,好几年没有看关于编程方面的书了,这几天闲,就准备在电脑上装上VS的最新版本,查了查,最新版是VS2017,.搜了下网上安装后大小,还真不小.下载离线安装包,完全下载居然需要25G左右,无 ...
- 分享:扩展Visual Studio 的简单方法
作为 MS 阵营的码农,相信Visual Studio 肯定是大家的主要武器了,但不知道大家有没有扩展Visual Studio 的需求. 最近我需要做一个工具,发现最好是实现在VS里面,于是,Goo ...
随机推荐
- linux进程的几种状态
Linux是一个多用户,多任务的系统,可以同时运行多个用户的多个程序,就必然会产生很多的进程,而每个进程会有不同的状态. Linux进程状态:R (TASK_RUNNING),可执行状态. 只有在该状 ...
- 如何在Azure上动态配置IP地址
微软最近对 Windows Azure 网站进行了升级,并启用了IIS8的动态 IP 限制模块.现在,开发人员可以为其网站启用并配置动态 IP 限制功能(或简称 DIPR). 可以通过以下链接查看此 ...
- Esper系列(十)NamedWindow语法delete、Select+Delete、Update
On-Delete With Named Windows 功能:在Named Windows中删除事件. 格式: 1 , 4 field_b = win.field_a, 5 field_ ...
- Exception in thread "main" java.lang.ClassNotFoundException: 解决方法
[root@h1 ~]# hadoop jar W1.jar hdfs://h1:9000/hello hdfs://h1:9000/cmd Exception in thread "mai ...
- HW3.11
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- Codeforces149E - Martian Strings(KMP)
题目大意 给定一个字符串T,接下来有n个字符串,对于每个字符串S,判断是否存在T[a-b]+T[c-d]=S(1 ≤ a ≤ b < c ≤ d ≤ length(T)) 题解 对于每个字符串S ...
- MVC client validation after PartialView loaded via Ajax MVC3中 弹出 Dialog时候 提交的时候 使用 Jquery 不验证 form表单 的解决办法
I came across this scenario whereby my main View uses Ajax posts to retrieve PartialViews and delive ...
- php的fread函数的一个巨大的坑
先看看fread的manual,如下: http://php.net/manual/en/function.fread.php fread() reads up to length bytes fro ...
- mysql 5.7新数据库sys解析(一)
mysql5.7增加了sys 系统数据库,通过这个库可以快速的了解系统的元数据信息 这个库确实可以方便DBA发现数据库的很多信息,解决性能瓶颈都提供了巨大帮助 这个库在mysql5.7中是默认存在 ...
- 找出数组前N大的数
这个题也是个比较有名的面试题.当然有很多变种. 题目意思基本是:从一个数据量很大的数组里找前N大的元素.不允许排序. 这个题有两个比较好的思路: 思路一:用快速排序的思想,是思想,不是要排序; 思路二 ...