码迷,mamicode.com
首页 > 数据库 > 详细

C# ado.net基础 查询一个表中有多少行数据 在sqlsever中的一个表中

时间:2017-01-19 23:03:02      阅读:500      评论:0      收藏:0      [点我收藏+]

标签:执行   div   blog   资源   scala   视频   shu   ati   text   

 镇场诗:
    诚听如来语,顿舍世间名与利。愿做地藏徒,广演是经阎浮提。
    愿尽吾所学,成就一良心博客。愿诸后来人,重现智慧清净体。
——————————————————————————————————————————

1 show my sql:

技术分享

 

 


2 code

  first base .cs file

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace shujuku
 8 {
 9     class SqlInformation
10     {
11         /// <summary>
12         /// 服务器的名称
13         /// </summary>
14         public string DataSource;
15         /// <summary>
16         /// 数据库的名称
17         /// </summary>
18         public string InitialCatalog;
19         /// <summary>
20         /// true为使用windows验证
21         /// </summary>
22         public bool IntegratedSecurity;
23         /// <summary>
24         /// SqlServer身份验证所需要的用户名
25         /// </summary>
26         public string UserID;
27         /// <summary>
28         /// SqlServer身份验证所需要的密码
29         /// </summary>
30         public string Password;
31         /// <summary>
32         /// 返回连接字符串
33         /// </summary>
34         public SqlInformation()
35         {
36             IntegratedSecurity = false;
37         }
38         public string LoginInformation()
39         {
40             if (IntegratedSecurity)
41             {
42                 return string.Format(@"Data Source = {0}; Initial Catalog = {1}; Integrated Security = true", DataSource, InitialCatalog);
43             }
44             else
45             {
46                 return string.Format(@"Data Source = {0};Initial Catalog = {1};User ID = {2};Password = {3}", DataSource, InitialCatalog, UserID, Password);
47             }
48         }
49     }
50 }

 

  second .cs file

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Data.SqlClient;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace shujuku
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             SqlInformation test = new SqlInformation();
15             test.DataSource = "USER-20170116MG";
16             test.InitialCatalog = "helloworld";
17             test.IntegratedSecurity = true;
18             string conStr=test.LoginInformation();
19 
20             using (SqlConnection connection = new SqlConnection(conStr))
21             {
22                 //编写sql语句(可以在sql server中创建脚本尝试成功后,复制粘贴)
23                 string sql = "select count(*) from TeacherClass";
24                 //创建一个执行sql语句的对象
25                 using (var cmd =new SqlCommand())
26                 {
27                     cmd.CommandText = sql;
28                     cmd.Connection = connection;
29                     //打开连接
30                     connection.Open();
31                     Console.WriteLine("数据库连接成功");
32                     int count = Convert.ToInt32(cmd.ExecuteScalar());
33                     Console.WriteLine("表的数据共有:{0}行",count);
34                     Console.WriteLine("sql语句执行成功");
35                 }
36                 //关闭连接,释放资源.因为用了 using,所以这里不用写语句
37             }
38             Console.WriteLine("数据库断开成功");
39             Console.ReadKey();
40 
41         }
42     }
43 }

 

3 result:

  console:

技术分享

 


——————————————————————————————————————————
博文的精髓,在技术部分,更在镇场一诗。版本:VS2015 SqlServer2014 系统:Windows 7
C#是优秀的语言,值得努力学习。我是跟随 传智播客\黑马 的.Net视频教程学习的。
如果博文的内容有可以改进的地方,甚至有错误的地方,请留下评论,我一定努力改正,争取铸成一个良心博客。
注:此文仅作为科研学习,如果我无意中侵犯了您的权益,请务必及时告知,我会做出改正。

C# ado.net基础 查询一个表中有多少行数据 在sqlsever中的一个表中

标签:执行   div   blog   资源   scala   视频   shu   ati   text   

原文地址:http://www.cnblogs.com/jinlingzi/p/6308875.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!