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. 6.Redis集群

    redis-cluster[集群]架构图 redis-cluster投票:容错 搭建Ruby环境 集群的搭建过程 连接集群 查看集群的命令 1.1 redis-cluster[集群]架构图 架构细节: ...

  2. 一个简单的jQuery回调函数例子

    jQuery回调函数简单使用 比如说,我们想要点击某个按钮后触发事件, 先把一些指定内容给隐藏掉, 然后跳出相关信息的对话框. 如果使用普通的方法, 不用回调函数的话, 会有怎么样的效果呢? 效果是先 ...

  3. day48-线程-信号量

    #1.信号量,用来保证多个线程不会互相冲突. #2.迷你唱吧:每次只能有两人在里面唱k: from threading import Thread from threading import Sema ...

  4. Estimating Gene Frequencies| method of maximum likelihood|point estimate

    I.11 Estimating Gene Frequencies 在小样本上计算基因A的概率PA,举例如下: 通过加大样本会将通过观察值得到的数趋近于真实数据,所以该问题转化为了统计学上利用大量观察值 ...

  5. Java面试题2-附答案

    JVM的内存结构 根据 JVM 规范,JVM 内存共分为虚拟机栈.堆.方法区.程序计数器.本地方法栈五个部分. 1.Java虚拟机栈: 线程私有:每个方法在执行的时候会创建一个栈帧,存储了局部变量表, ...

  6. OpenCV 使用FLANN进行特征点匹配

    #include <stdio.h> #include <iostream> #include "opencv2/core/core.hpp" #inclu ...

  7. DSU On Tree——Codeforces 600E(E. Lomsat gelral)

    有这么一类问题,要求统计一棵树上与子树相关的某些信息,比如:在一棵所有节点被染色的树上,统计每棵子树上出现次数最多的颜色编号之和. 很自然的可以想到用DFS序+主席树去求解,但是编码复杂度很高: 然后 ...

  8. AngularJS前端以ArrayBuffer类型请求后端数据以生成文件时,出现异常的处理

    .error(function(error){ var decodedString = String.fromCharCode.apply(null, new Uint8Array(error)); ...

  9. [LC] 8. String to Integer (atoi)

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  10. Java过滤器Filter的原理及配置_学习笔记

    Filter中文意思为过滤器.顾名思义,过滤器可在浏览器以及目标资源之间起到一个过滤的作用.例如:水净化器,可以看成是生活中的一个过滤器,他可以将污水中的杂质过滤,从而使进入的污水变成净水. 对于WE ...