1、Ado.net

Ado.net是一组由微软提供的使用C#操作数据库的类库

2、连接

首先引入:

using System.Data.SqlClient;

需要使用连接字符串进行连接

using System;
using System.Data.SqlClient;
using System.Text; namespace Demo
{ class Program
{ static void Main()
{
// 连接字符串
string connStr = "server=127.0.0.1;uid=sa;pwd=123456;database=Demo";
SqlConnection conn = new SqlConnection(connStr); // 连接数据库
conn.Open();
Console.WriteLine("连接完成"); // 关闭连接
conn.Close();
conn.Dispose();
Console.WriteLine("数据库关闭");
Console.ReadLine();
} } }

上面所示的是最简单的连接字符串,包括服务地址,账号及密码,数据库名称

使用#region可以划分代码块

3、执行语句

using System;
using System.Data.SqlClient;
using System.Text; namespace Demo
{ class Program
{ static void Main()
{
// 连接字符串
string connStr = "server=127.0.0.1;uid=sa;pwd=123456;database=Demo";
SqlConnection conn = new SqlConnection(connStr); conn.Open();
Console.WriteLine("连接完成"); // 创建sql命令对象
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
// 书写SQL脚本
cmd.CommandText = "insert into [dbo].[user](name,age,ClassId) values(N'徐若瑄',66,103)";
// 非查询语句执行,返回受影响的行数
cmd.ExecuteNonQuery();
Console.WriteLine("语句执行完成");        conn.Close();
Console.ReadLine();
} } }

自动释放连接的语法糖

3、连接池

在连接字符串中有一个属性叫pooling,默认为true,意为启用连接池

进程池是一个对象池,当我们第一次连接数据库时直接向数据库

请求连接,同时在连接池内生成一个innerConnection对象,当我们通过close释放

连接后,池内的对象不会释放,直接从连接池内取出来,节省我们需要频繁使用数据库的开销。

当连接池内的对象有空闲的,直接使用空闲的;当没有空闲的内连接对象时,未达到连接池上限时,直接向数据库创立一个,

达到上限时需要等待

4、制作一个连接字符串创建器

5、制作一个登陆窗体

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace UserLogin
{
public partial class UserLoginFrm : Form
{
public UserLoginFrm()
{
InitializeComponent();
} private void UserLoginFrm_Load(object sender, EventArgs e)
{ } private void LoginBtn_Click(object sender, EventArgs e)
{
//取走窗体数据进行校验
if (string.IsNullOrEmpty(UsernameBox.Text.Trim())||
string.IsNullOrEmpty(PwdBox.Text.Trim()))
{
MessageBox.Show("用户名或密码不可为空");
return;
} //连接数据库进行查询
string connStr = "server=127.0.0.1;uid=sa;pwd=123456;database=Demo";
using (SqlConnection conn=new SqlConnection(connStr))
{
using (SqlCommand cmd = conn.CreateCommand())
{
conn.Open(); string sql = string.Format("select count(1) from LoginUser where Username='{0}' and Password='{1}'",
UsernameBox.Text, PwdBox.Text);
cmd.CommandText = sql;
// 执行查询语句,并只返回查询出来的第一个单元格的值,返回的数据格式也不清楚
object result = cmd.ExecuteScalar();
int rows = int.Parse(result.ToString());
if (rows >= )
{
MessageBox.Show("成功登录!");
}
else
{
MessageBox.Show("登录失败!");
}
}
}
}
}
}

6、配置文件配置连接字符串

在App.config中进行配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="sql" connectionString="server=127.0.0.1;uid=sa;pwd=123456;database=Demo"/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>

在专门的一个类中进行引入:

using System.Configuration;

namespace UserLogin
{
class ConnectionStringHelper
{
public static string GetCurrentConnectionString()
{
return ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
}
}
}

在引用中引入:System.Configuration

之后使用上述类的静态方法即可

------------恢复内容结束------------

sql05的更多相关文章

  1. pivot 与 unpivot 函数是SQL05新提供的2个函数

    pivot 与 unpivot 函数是SQL05新提供的2个函数   ----------------------------------------------------------------- ...

  2. winserver 08 64位安装sql05 64位提示asp版本注册

    将打开 安装IIS 6.0的就可以了,然后重启下

  3. pivot 与 unpivot 函数是SQL05新提供的2个函数 灰常灰常的实用

    转自:http://blog.sina.com.cn/s/blog_5ef755720100cyo3.html pivot函数: create table test(id int,name varch ...

  4. oracle根据某个字段去重实例

    if not object_id('Tempdb..#T') is null drop table #T Go Create table #T([ID] int,[Name] nvarchar(1), ...

  5. 经典.net面试题目

    1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private :   私有成员, 在类的内部才可以访问. protected : 保 ...

  6. net面试题

    简述 private. protected. public. internal 修饰符的访问权限.答 . private :   私有成员, 在类的内部才可以访问.   protected : 保护成 ...

  7. mysql 联合索引和唯一索引

    一般来说.如果有where a=? and b=? and c=? 的语句. 如果表也有DML, 我一般只在a 上建索引.  这也是代价平衡的结果. 一方面 只在a 上建索引那么是 index ran ...

  8. SQL如何本地数据库连接服务器的数据库

    当我们本地数据库的数据作为测试的时候,需要服务器上的数据,但是每次都远程服务器打开数据库查看数据是很麻烦的,那么我们如何让本地的数据库连接服务器上的数据库.前提是你本地的数据库的版本必须和服务器上的数 ...

  9. 经典.net试题

    经典.net面试题目 1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private :   私有成员, 在类的内部才可以访问. pr ...

随机推荐

  1. Python_运维中常用的20个库和模块

    1.psutil是一个跨平台库(https://github.com/giampaolo/psutil)能够实现获取系统运行的进程和系统利用率(内存,CPU,磁盘,网络等),主要用于系统监控,分析和系 ...

  2. Nginx_安全1

    Nginx 安全 nginx隐藏版本号 # 在Nginx的配置文件中进行修改,增加下面这个. server_tokens on; nginx对IP和目录限速 # Nginx可以通过HTTPLimitZ ...

  3. BaseAdapter教程(2) BaseAdapter的notifyDataSetChanged动态刷新

    遇到了这麽一个需求,ListView滑到最底,然后会自动在底部加入新的Cell,实现动态刷新. 1. 首先,为ListView加上setOnScrollListener. lvHomePostItem ...

  4. μC/OS-II中使用软件定时器

    在试着将μC/OS-II移植到ARM7芯片(LPC2138)上的过程中,发现使用OSTmrCreate创建的OSTmr始终都不能执行CallbackFunction,OS版本是v2.85,最后是这么解 ...

  5. cannot be found on object of type xx.CacheExpressionRootObject

    0 环境 系统环境:win10 编辑器:IDEA 1 前言->环境搭建 1-1 pom依赖 <?xml version="1.0" encoding="UTF ...

  6. 吴裕雄--天生自然python学习笔记:pandas模块读取 Data Frame 数据

    读取行数据 读取一个列数据的语法为: 例如,读取所有学生自然科目的成绩 : import pandas as pd datas = [[65,92,78,83,70], [90,72,76,93,56 ...

  7. 限制IP每分钟访问10次

    转载:https://www.jianshu.com/p/d1326ab657ff IP请求限制,之前用过redis的set设置时间戳一分钟过期:也用过nginx的IP限流配置.前者,没法解决“用户在 ...

  8. Kinect_V1在Debian testing的配置指北

    在Linux下驱动Kinect V1现在有两种方式,一种是使用OpenNI + SensorKinect + Nite的方案,一种是使用OpenNI2 + libfreenect的方案,第一种我没有尝 ...

  9. 数据结构中的顺序表和链表(Python语言)

    转载:https://blog.csdn.net/weixin_43187669/article/details/96426362 算法是为了解决实际问题而设计的,数据结构是算法需要处理的问题载体. ...

  10. OpenSSL之X509系列

    OpenSSL之X509系列之1---引言和X509概述 [引言]    X509是系列的函数在我们开发与PKI相关的应用的时候我们都会用到,但是OpenSSL中对X509的描述并不是很多,鉴于些,我 ...