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 ...
随机推荐
- WinForm触摸屏程序功能界面长时间不操作自动关闭回到主界面 z
操作者经常会在执行了某操作后,没有返还主界面就结束了操作然后离开了,程序应该关闭功能窗体自动回到主界面方便下一位操作者操作.那么对于WinForm程序怎么实现呢? 实现原理:拦截Application ...
- 跟我学机器视觉-HALCON学习例程中文详解-测量圆环脚宽间距
跟我学机器视觉-HALCON学习例程中文详解-测量圆环脚宽间距 This example program demonstrates the basic usage of a circular meas ...
- 利用 jQuery-photoClip插件 实现移动端裁剪功能并以Blob对象上传
最近客户要求实现论坛贴子附件裁剪功能,没有考虑js与ios.android容器交互解决方案,单纯用js去实现它的.由于本来附件上传用的别的插件实现的,所以是在此基础上费了不少劲,才把jQuery-ph ...
- FP树(附)
Apriori算法和FPTree算法都是数据挖掘中的关联规则挖掘算法,处理的都是最简单的单层单维布尔关联规则. 转自http://blog.csdn.net/sealyao/article/detai ...
- Mysql常用操作记录
在linux平台中相关的MySql操作 打开Mysql mysql -uroot -p //-u后边为用户名,-p后边为密码 1:使用SHOW语句找出在服务器上当前存在什么数据库:mysql& ...
- 简述configure、pkg-config、pkg_config_path三者的关系
简述configure.pkg-config.pkg_config_path三者的关系 一.什么是configure 源码安装过程中大多会用到configure这个程序,一般的configure都是一 ...
- flot中文说明文档 简版
Flot参考文档: 一.对绘图函数plot的调用:var plot=$.plot(placeholder,data,options) ----------- placeholder --------- ...
- 51nod1009(1的数目)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1009 题意:中文题诶- 思路:分别考虑各个数位上出现1的次数 ...
- JAVA基础入门
Java入门基础 1.IDE->Eclipse 新建程序步骤 1.创建一个Java项目 2.创建一个包(package) 也就相当于C#中的命名空间C++中的头文件 3.创建一个类 这样就完成了 ...
- 远程使用Gpupdate(Hash,哈希)
function Start-GPUpdate { param ( [String[]] $ComputerName ) $code ...