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. MySQL_学习资料

    https://mp.weixin.qq.com/s/qOmyaEEpVJTUMZYfomp3ug

  2. DjangoBlog部署教程

    本篇文章将会手把手教你如何部署DjangoBlog项目,首先介绍下我这里的基本环境,请大家仔细阅读此部分,下面的教程都会使用这些约定来介绍: 系统是ubuntu 18.04 LTS 假设你的域名是ww ...

  3. 查看linux系统安装的服务

    如何查看linux系统安装了哪些服务呢,因不同版本的操作系统可能使用的命令不一样或者有些命令在某些操作系统不可用,现列举一些常用查看命令(基于我的linux版本). 我的操作系统版本如下: 1.ser ...

  4. java中的赋值

    java中的赋值使用符号“=”. 按照java编程思想的解释:它的意思是“取等号右边的值,把它复制给左边”. 当然左边必须是一个明确的,已命名的变量. 基本类型: int a=2; int b=3; ...

  5. iOS 仿看了吗应用、指南针测网速等常用工具、自定义弹出视图框架、图片裁剪、内容扩展等源码

    iOS精选源码 扩展内容的cell - folding-cell 一个近乎完整的可识别中国身份证信息的Demo 可自动快速... JPImageresizerView 仿微信的图片裁剪 带年月和至今以 ...

  6. JS替换变量中的文字字母

    var text='Hello world, Hello world'; var b= text.replace('world','zhengxiaoya'); // 找到字符串中的第一个'world ...

  7. http接口与webservice接口的区别

    常见的API接口有两类:http接口和webservice接口. http接口走http协议,通过路径来区分调用方法,请求报文一般是key-value形式的,返回报文一般是json串,常用的是get和 ...

  8. [LC] 55. Jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. Ubuntu全方位美化,定制教程

    Ubuntu全方位美化,定制教程 上一篇随笔聊了聊Linux图形界面的各种名词及其关系,解释了何为xserver,何为xclient,linux的图形界面是如何工作的,Linux图形软件的多样性.li ...

  10. jprofiler监控tomcat

    jprofiler监控tomcat https://www.cnblogs.com/yjd_hycf_space/p/7727757.html https://www.jianshu.com/p/c3 ...