第二周 学习笔记--ADO.Net中SqlConnection、Sqlcommand的应用

@[一.知识点
1.SqlConnection
(1)使用SqlConnection类可以连接到SQL Server数据库。SqlConnection对象的主要属性和方法如下:
——属性:ConnectionString(连接字符串)
——方法:Open(打开数据库连接)
Close(关闭数据库连接)
(2)连接数据库主要分以下三步:
——定义字符串
——创建SqlConnection对象,代码如下:
SqlConnection sqlConnection = new SqlConnection();
——打开数据库连接,代码如下:
sqlConnection.Open();

2、SqlCommand

(1)SqlCommand对象用于执行具体的SQL语句,如增加、删除、修改、查找。SqlCommand对象的使用步骤如下。
——创建SqlConnection对象。
——定义SQL语句。
——创建SqlCommand对象。
——调用SqlCommand对象的某个方法,执行SQL语句。

二.思维导图
在这里插入图片描述
三.范例
1.声明并实例化SQL连接,并在字符串变量sqlConnection中,描述连接字符串所需的服务器地址、数据库名称、集成安全性。
SqlConnection sqlConnection = new SqlConnection(); 2SqlConnection.ConnectionString =“Server=(local);Database=EduBaseDemo;Integrated Security=sspi”;
2. Sqlcommand—Connection、CommandText
SqlCommand sqlCommand = new SqlCommand(); sqlCommand.Connection = sqlConnection; sqlCommand.CommandText = “SELECT COUNT(1) FROM tb_User”

  • " WHERE No=’" + this.txb_UserNo.Text.Trim() + “’” + " AND Password=HASHBYTES(‘MD5’,’" + this.txb_Password.Text.Trim() + “’);”;
    3.SqlCommand—ExecuteScalar

sqlConnection.Open(); int rowCount = (int)sqlCommand.ExecuteScalar();
object
sqlConnection.Close();
if (rowCount == 1)
{
MessageBox.Show(“登录成功。”);
}
else
{
MessageBox.Show(“用户号/密码有误,请重新输入!”); this.txb_Password.Focus(); this.txb_Password.SelectAll();
} TOC](这里写自定义目录标题)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值