本周课上主要学习了ADO.Net中SqlConnection,SqlCommand的应用方法和原理。
在ADO.Net的体系中,SqlConnection,SqlCommand是ADO.Net的分支,主管的是sqlserver与Microsoft Visual Studio 2008窗体的连接。SqlConnection是 主要是用来作为sql server与Microsoft Visual Studio 2008的连接字符串,在Microsoft Visual Studio 2008中其自带有SqlConnection的属性,只需定义一个字符用来存放连接的字符串,但这一个属性必须定义为SqlConnection。
SqlCommand主要是用来允许使用人在数据库上执行操作的类型,例如可以对数据库中的数据进行select,insert,modify,delete等命令。SqlCommand的commandtext属性是用来体现sql语句需要执行什么功能。
例:
使用windows验证
SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
sqlConnection.ConnectionString = "Server=(Local);Database=EduBaseDemo;Integrated Security=sspi";
//在字符串变量中,描述连接字符串所需的服务器地址、数据库名称、集成安全性
使用sql连接验证
SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
sqlConnection.ConnectionString = "Server=(Local);Database=EduBaseDemo;Integrated Security=false;user id=sa;password=sa";
//在字符串变量中,描述连接字符串所需的服务器地址、数据库名称、集成安全性
SqlCommand sqlCommand = new SqlCommand(); //声明并实例化SQL命令;
sqlCommand.Connection = sqlConnection; //将SQL命令的属性Connection指向SQL连接;
sqlCommand.CommandText = //指定SQL命令的命令文本;命令文本由字符串拼接而成;
"SELECT COUNT(1) FROM tb_User"
+ " WHERE No=‘" + this.txb_No.Text.Trim() + "‘" //将文本框的文本清除首尾的空格后,拼接至命令文本中;
+ " AND Password=HASHBYTES(‘MD7‘,‘" + this.txb_Pas.Text.Trim() + "‘);";
第一周学习笔记之ADO.Net之SqlConnection,SqlCommand的应用
原文:https://www.cnblogs.com/linyanfang1/p/9655317.html