使用SqlConnection连接到SQL Server2019

发布时间:2022-07-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了使用SqlConnection连接到SQL Server2019脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

且返回表的行数。

1、界面设计

使用SqlConnection连接到SQL Server2019

 

使用SqlConnection连接到SQL Server2019

 

 

 

 2、代码设计

使用SqlConnection连接到SQL Server2019

 1 using System;
 2 using System.Collections.Generic;
 3 using System.componentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 using System.Data.SQLClient;
11 
12 namespace Chapter13
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InITializeComponent();
19         }
20 
21         PRivate void Form1_Load(object sender, Eventargs e)
22         {
23             //SqlConnection conn = new SqlConnection(@"server=.SQLEXPress;integrated security=true;database=MyBookInfo");
24 
25             //SqlCommand cmd = new SqlCommand();
26 
27             //try
28             //{
29             //    conn.OPEn();
30 
31             //    txtComandText.AppendText("Connection opened n");
32             //    txtComandText.AppendText("Command created.n");
33 
34             //    cmd.COMmandText = @"select * From BookInfo ";
35 
36             //    txtComandText.AppendText("Ready to execute SQL statement:nttt" + cmd.CommandText);
37             //}
38             //catch(SqlException ex)
39             //{
40             //    MessageBox.Show(ex.Message + ex.StackTrace, "Exception Details");
41             //}
42             //finally
43             //{
44             //    conn.Close();
45             //    txtComandText.AppendText("nConnection Closed.");
46             //}
47             
48         }
49 
50         private void BTnrowcount_Click(object sender, EventArgs e)
51         {
52             SqlConnection conn = new SqlConnection(@"server=.SQLExpRESS;integrated security=true;database=MyBookInfo");
53             
54             //创建连接字符串,conn
55 
56             string sql = @"select count(*) from BookInfo";
57             
58             //sql连接语句
59 
60             SqlCommand cmd = new SqlCommand(sql, conn);
61             
62             //执行连接命令
63 
64             txtScalar.AppendText("Command created and connected.n");
65 
66             try
67             {
68                 conn.Open();
69                 txtScalar.AppendText("Number of Books is :");
70 
71                 txtScalar.AppendText(cmd.ExecuteScalar().ToString());
72                 txtScalar.AppendText("n");
73 
74             }
75             catch(SqlException ex)
76             {
77                 MessageBox.Show(ex.ToString());
78             }
79             finally
80             {
81                 conn.Close();
82                 txtScalar.AppendText("nConnection Closed.");
83             }
84         }
85     }
86 }

使用SqlConnection连接到SQL Server2019

3、详细说明

1、设计好界面以后,点击‘Count Rows’ 生成第21行代码。

2、在21行代码内填写连接所需代码。

3、代码剖析:

 

 

使用SqlConnection连接到SQL Server2019

 

 

该语句中@“server=.”中的  .(点) 表示本地服务器。而  (斜线) 后面表示运行在数据库服务器上的实例名称。

.SQLEXPRESS 也可以写成 localhostSQLEXPRESS 或者 (local)SQLEXPRESS

intergrated security=true 则指定要通过Windows 身份验证来登录。

database=MyBookInfo 则是制定数据库名称的语句。这里的数据库的名称是MyBookInfo

常用的可以添加在连接字符串里的参数:

 

 

名称 别名 默认值 允许值 说明
Data Source

server 、Address、Addr、

Network、Address

None 服务器名或网络地址 目标SQL Server 实例的名称
Initial CataLOG Database None 服务器上的任何数据库 数据库名
Connect Timeout Connection Timeout 15

0~32767

连接的等待时间(秒)
Integrated Security Trusted_Connection false Ture 、false、Yes、no 、sspi 指定身份验证模式

 

 

使用SqlConnection连接到SQL Server2019

 

 

 创建SQL语句。语句功能:数表格的行数。

可以根据需要创建不同功能的语句。

使用SqlConnection连接到SQL Server2019

 

 

 

使用SqlConnection连接到SQL Server2019

 

 txtScalar 是文本输入框的名字。

使用SqlConnection连接到SQL Server2019

 

 try catch 语句。

cmd.ExecuteScalar() 可以理解为在SQL中执行你写好的 SQL语句,然后把结果返回。

AppendText 则是把返回的数据显示出来。

 

 参考书籍:《C# 2012 数据库编程入门经典》(第五版)【美】Vidya Vrat Agarwal   清华大学出版社

脚本宝典总结

以上是脚本宝典为你收集整理的使用SqlConnection连接到SQL Server2019全部内容,希望文章能够帮你解决使用SqlConnection连接到SQL Server2019所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。