一、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种连接方法的更多相关文章

  1. Visual Studio 2010下载 + 附破解方法

    Visual Studio 2010下载 + 附破解方法 1.Microsoft Visual Studio 2010下载 旗舰版(Ultimate) http://download.microsof ...

  2. android studio gradle 两种更新方法更新

    android studio gradle 两种更新方法更新 第一种.Android studio更新 第一步:在你所在项目文件夹下:你项目根目录gradlewrappergradle-wrapper ...

  3. SQLServer 在Visual Studio的连接方法

    一.Sql Server 在Visual Studio的连接有两种方法: (1)本地计算机连接; [c#] view plaincopy     string s = "Data Sourc ...

  4. Visual Studio 2017使用ODT 连接Oracle 数据库出现异常

    2019.5.23 更新 突然发现原来是是sqlnet.ora在搞鬼,只要将SQLNET.AUTHENTICATION_SERVICES=(nts)  改为 SQLNET.AUTHENTICATION ...

  5. 禁止Visual Studio启动时自动连接TFS服务器

    在默认设置情况下,Visual Studio启动时,会自动连接上次打开过的TFS服务器.这种设计能够提高开发人员的工作效率,避免每次手动连接TFS服务器. 但是在某些情景中,也会给人造成不必要的麻烦, ...

  6. Visual Studio 6 (VC6)连接Team Foundation Server (TFS 2018),实现源代码的版本管理

    1. 概述 Visual Studio 6(VB6, VC6, Foxpro-)是微软公司在1998年推出的一款基于Windows平台的软件开发工具,也是微软推出.NET开发框架之前的最后一个IDE工 ...

  7. Visual studio 2015 与 mysql 连接

    Visual Studio 2015 Community连接到MySQL,步骤很简单,但刚弄的时候一脸. 这个学期开了一门课程,老师教的是visual studio 2010来开发.net的,但是我自 ...

  8. 打开Visual Studio 2012的解决方案 连接 Dynamics CRM 2011 的Connect to Dynamics CRM Server 在其工具下没有显示

    一.使用TFS 代码管理,发现Visual Studio 2012 菜单栏 工具下的Connect to Dynamics CRM Server 没有显示. 平常打开VS下的工具都会出现Connect ...

  9. Visual Studio 2008破解激活升级方法

    声明:本文中涉及到的序列号及更新方法均来自互联网,请支持正版. 微软为业余爱好者.热衷者和学生提供了免费版——Express Edition (轻型.易学.易用的开发工具). 如不想支付任何费用,建议 ...

随机推荐

  1. 使用kbmmw smart service 属性时的一个注意事项

    kbmmw 5.0 以后支持smart service, 这个用起来非常方便,kbmmw 通过 定制属性来简化编程,可以参考我以前的文章.但是这个意味着使用单元引用一定要小心, 否则出了问题,都不知道 ...

  2. MyBatis中log4j 和 参数 和 分页和别名 功能

    1.配置全局文件,注意各个配置标签的顺序 properties?, settings?, typeAliases?, typeHandlers?, objectFactory?,   objectWr ...

  3. 8个开发必备的PHP功能--转(柒捌玖零)

    做过PHP开发的程序员应该清楚,PHP中有很多内置的功能,掌握了它们,可以帮助你在做PHP开发时更加得心应手,本文将分享8个开发必备的PHP功能,个个都非常实用,希望各位PHP开发者能够掌握. 1.传 ...

  4. TCP/IP协议(5):传输层之TCP

    一.TCP报文 上图为TCP报文的格式,可以看到TCP头部占20个字节,其中红色圆圈中每一项占一位,表示TCP报文的类型,置1表示该项有效. SYN表示建立连接.    FIN表示关闭连接.    A ...

  5. Transforming Real-Time Task Graphs to Improve Schedulability

    Basic idea:  insert artificial delays to the release times of certain vertices of a task graph to ge ...

  6. 第04章:MongoDB基本概念

    ① 数据库 MongoDB的一个实例可以拥有一个或多个相互独立的数据库,每个数据库都有自己的集合   集合 集合可以看作是拥有动态模式的表   文档 文档是MongoDB中基本的数据单元,类似于RDB ...

  7. app.json

    APP.JSON 文件来对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.设置多 TAB 等.相当于iOS开发中的AppDelegate 注意该文件不可添加任何注释. {   ...

  8. 准备用有人云平台和tlink.io云平台和电脑做云转发

    初步想的是用有人做国网电表转发,用tlink.io做综合采集模块转发,耗时一天 然后用tlink.io的做二次前端开发,耗时两天 用有人做二次前端开发,耗时两天 最后可以试试用常见的OPC公网转发到这 ...

  9. python迭代器实例

    1. 迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,知道所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退. 1.1 使用迭代 ...

  10. hihocode 1336 Matrix Sum 【二维树状数组】

    题目 两个操作: 1. Add x y value: Add value to the element Axy. (Subscripts starts from 0 2. Sum x1 y1 x2 y ...