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. GpsNet2020 车联网平台

    车联网产业是汽车.电子.信息通信.道路交通运输等行业深度融合的新型产业,是全球创新热点和未来发展制高点.车企通过部署车联网系统,为车主提供更好的出行服务体验,增加产品竞争力.依托华为云.边.端协同优势 ...

  2. String Distance and Transform Process

    http://acm.hdu.edu.cn/showproblem.php?pid=1516 Problem Description String Distance is a non-negative ...

  3. spark-shell使用指南. - 韩禹的博客

    在2.0版本之前,Spark的主要编程接口是RDD(弹性分布式数据集),在2.0之后,则主推Dataset,他与RDD一样是强类型,但更加优化.RDD接口仍然支持,但为了更优性能考虑还是用Datase ...

  4. Linux下查找Nginx配置文件位置

    1.查看Nginx进程 命令: ps -aux | grep nginx 圈出的就是Nginx的二进制文件 2.测试Nginx配置文件 /usr/sbin/nginx -t 可以看到nginx配置文件 ...

  5. OrderValidator

    package org.linlinjava.litemall.core.validator; import javax.validation.Constraint; import javax.val ...

  6. Eclipse中配置使用本地schema或dtd文件

    问题:在设备不能正常联网的情况下,无法获取到网络上的 dtd 或 schema,编写配置文件的 XML 文档就没有了提示功能. 一般情况下,下载的 Jar 包或者 Zip 包中都会包含需要的 sche ...

  7. nginx做正向代理搭建bugfree

    下载地址: Nginx下载地址:http://download.csdn.net/detail/terrly88/9099117 bugfree下载地址:http://download.csdn.ne ...

  8. Hibernate学习之hql 与sql

    Hibernate中查询: createQuery( String qlString)使用的是HQL语句: createNativeQuery (String sqlString)使用的是SQL语句: ...

  9. PHP调试工具PHP DEBUG TOOLS 使用方法

    一.安装篇安装前的准备环境:必须得先装X-Debug,至于怎样安装X-Debug请看http://www.xdebug.org/docs/install 1. 从http://www.xdebug.o ...

  10. MySQL数据库优化、设计与高级应用

    MySQL数据库优化主要涉及两个方面,一方面是对SQL语句优化,另一方面是对数据库服务器和数据库配置的优化. 数据库优化 SQL语句优化 为了更好的看到SQL语句执行效率的差异,建议创建几个结构复杂的 ...