SQLServer 在Visual Studio的2种连接方法
一、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的2种连接方法的更多相关文章
- Visual Studio 2010下载 + 附破解方法
Visual Studio 2010下载 + 附破解方法 1.Microsoft Visual Studio 2010下载 旗舰版(Ultimate) http://download.microsof ...
- android studio gradle 两种更新方法更新
android studio gradle 两种更新方法更新 第一种.Android studio更新 第一步:在你所在项目文件夹下:你项目根目录gradlewrappergradle-wrapper ...
- SQLServer 在Visual Studio的连接方法
一.Sql Server 在Visual Studio的连接有两种方法: (1)本地计算机连接; [c#] view plaincopy string s = "Data Sourc ...
- Visual Studio 2017使用ODT 连接Oracle 数据库出现异常
2019.5.23 更新 突然发现原来是是sqlnet.ora在搞鬼,只要将SQLNET.AUTHENTICATION_SERVICES=(nts) 改为 SQLNET.AUTHENTICATION ...
- 禁止Visual Studio启动时自动连接TFS服务器
在默认设置情况下,Visual Studio启动时,会自动连接上次打开过的TFS服务器.这种设计能够提高开发人员的工作效率,避免每次手动连接TFS服务器. 但是在某些情景中,也会给人造成不必要的麻烦, ...
- Visual Studio 6 (VC6)连接Team Foundation Server (TFS 2018),实现源代码的版本管理
1. 概述 Visual Studio 6(VB6, VC6, Foxpro-)是微软公司在1998年推出的一款基于Windows平台的软件开发工具,也是微软推出.NET开发框架之前的最后一个IDE工 ...
- Visual studio 2015 与 mysql 连接
Visual Studio 2015 Community连接到MySQL,步骤很简单,但刚弄的时候一脸. 这个学期开了一门课程,老师教的是visual studio 2010来开发.net的,但是我自 ...
- 打开Visual Studio 2012的解决方案 连接 Dynamics CRM 2011 的Connect to Dynamics CRM Server 在其工具下没有显示
一.使用TFS 代码管理,发现Visual Studio 2012 菜单栏 工具下的Connect to Dynamics CRM Server 没有显示. 平常打开VS下的工具都会出现Connect ...
- Visual Studio 2008破解激活升级方法
声明:本文中涉及到的序列号及更新方法均来自互联网,请支持正版. 微软为业余爱好者.热衷者和学生提供了免费版——Express Edition (轻型.易学.易用的开发工具). 如不想支付任何费用,建议 ...
随机推荐
- windows、linux下通过ftp上传文件小脚本
一.windows @echo off #open ip 将要上传文件的IP地址echo open IP>ftp.up #用户名echo ninic>>ftp.up #密码echo ...
- vue的饿了么写作感受
1.在重复使用的组件中,要把重复使用组件的需要在数据的请求的地方设置为在富组件中,通过props船只到子组件,这样在重复调用次组件的时候既可以很好的避免组件的值的冲突 2.getData的使用,把所有 ...
- web程序打包详解
重要更新:鉴于很多小伙伴们说看不到图,我这边换了几个浏览器看了下,都看得到的,估计是网速问题,请耐心等待,另外,为了更好的方便大家学习,特此提供源码以及一个word文档,word文档就是本文内容 ...
- 学python之路前的一些话
为什么学python: 这些年一直从事运维相关的工作.但做下来感觉都是些很基础的东西,无非就是对一些命令或者问题处理很熟练而已,混的都是经验.曾很羡慕会写shell脚本,会自动化安装程序的运维组组长, ...
- matlab画无向图,基于坐标的无向图联系作者
%函数名netplot %使用方法输入请help netplot %无返回值 %函数只能处理无向图 %作者:tiandsp %最后修改: function netplot(A,flag) %调用方法输 ...
- (最短路 弗洛伊德) Til the Cows Come Home -- POJ --2387
#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> ...
- concurrent.future
concurrent.future module provides a high-level interface for asynchronously executing callables. Bas ...
- android 屏幕旋转 不重新加载oncreate
当手机设定了使用横屏或者竖屏的时候,还想要使用重力感应,可以设置activity属性 android:screenOrientation="sensor" 但是每次翻转屏幕,都会重 ...
- PAT甲级 1128. N Queens Puzzle (20)
1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...
- springJDBC和SpringJDBCTemplate解决方案探究
先来看一个纯JDBC的例子,体会一下springJDBC和SpringJDBCTemplate两者的区别 一个Customer类 package com.mkyong.customer.model; ...