1.1 介绍
关系型数据库,SQLServer是由微软公司开发的一种关系型据库管理系统,它已广泛用于电子商务、银行、保险、电力等行业。SQLServer提供了对XML和Internet标准的支持,具有强大的、灵活的、基于Web的应用程序管理功能。
1.2 优缺点
优点:
1>通过创建唯一性索引,可以保证数据库表中每一行数据的唯一性。
2>通过创建索引可以大大加快数据的检索速度
3>通过使用索引,可以在查询的过程中,使用优化隐藏器,提高系统的性能。
4>在使用分组和排序子句进行数据检索时,同样可以显著减少查询中分组和排序的时间
缺点:
1>只能运行在windows平台,平台单一不具备开放性
2>并行实施与共存模型不成熟,难以处理大规模的数据,伸缩性有限
3>当用户连接多时性能会变得很差并且不稳定
4>客户端支持应用模式,仅支持C/S模式
1.3 服务器安装
1.3.1 安装软件:
1> 下载sql server 2019 下载地址:https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads
2> 选择自定义类型进行安装:
3> 选择语言和安装位置,点击安装
4>安装成功后出现以下界面,选择安装,选择全新的SQLServer 独立安装或向现有安装添加功能
5>选择指定可用版本Developer,点击下一步
6>更新选择,可点可不点
7>安装执行完毕后继续下一步
8>功能选择中,可以不勾选R、Python、java,其他都勾选后,点击下一步
9>选择默认实例,点击下一步,下一步
10>选择混合模式,设置密码,和添加当前用户,然后点击下一步
11>选择表格模式,添加当前用户后,点击下一步
12>默认配置无需更改,点击下一步,下一步
13>Distributed Replay的访问权限,选择的是当前用户,点击下一步,下一步
14>脱机安装Microsoft机器学习服务器组件
将页面4个蓝色链接分别复制到浏览器,下载4个压缩包,将4个压缩包放在同一个文件下,无需解压,然后把每个压缩包的名字里的1033替换成2052
以上操作都ok后,接下来就可以回到sql sever安装界面中选择包含4个压缩包的文件目录了,就会出现下一步按钮了,我们就可以继续点击下一步
15>进入准备安装界面,点击安装,耐心等待安装完成
17>下载SQL Server Management Studio软件,用于数据库的连接与管理,下载地址:https://aka.ms/ssmsfullsetup,安装后,进入连接
18>连接后,右键弹出界面点击属性
19>在属性页面里,选择安全性,选择SQL Server和windows身份验证模式,然后点击确定
20>在安全性-》登录名-sa,右键点击,弹出属性界面,在常规界面上确认密码
21>属性界面,在状态栏,上勾选授予和启用
22>设置允许远程连接,打开sql server congfiguration Manager,启用TCP/IP
23>右键TCP/IP可以打开属性界面,检查和配置IP、端口
24>都设置好后,可以在SQL Server Management Studio重启,也可以在sql server congfiguration Manager的重启第三个服务
1.4 解决数据库远程连接不上问题
如出现这个问题为防火墙问题,解决办法如下:
另外,修改了防火墙也可能导致其他主机ping不通当前主机,这是因为本机的防火墙关闭了ICMP回显功能,只要把这回显功能打开就行了,解决办法是:
以管理员身份运行CMD,输入以下命令 netsh firewall set icmpsetting 8 注: netsh firewall set icmpsetting 8 – 开启ICMP回显
1.5 简单使用
1>使用navicat连接数据库:
2> 连接后,右键新建数据库,在新的数据库上新建表格
3>客户端连接数据库代码
database.h
/*
* Filename: DataBase.h
* Modify: ybLin
* Description: database
* *******************************************************/
#ifndef __EXSKY_DATA_BASE_H__
#define __EXSKY_DATA_BASE_H__
#include <basic/myThread.h>
#import "..\WinLib\Others\msado15.dll" no_namespace rename("EOF","adoEOF")
typedef struct {
char sMateriel[256];
char sOrderNo[256];
char sModel[256];
char sLens[256];
char sSNCode[256];
char sMacId[256];
char sAddTime[64];
} _mac_info;
class CDataBase
{
public:
CDataBase(const char* hostIP = NULL);
~CDataBase();
public:
bool m_nCheckModel;
void UpdateDB();
bool InitDB();
bool Init(const char* hostIP = "192.168.8.121",
const char* dataBase = "ManageDB", int hostPort = 1433,
const char* user = "sa", const char* pwd = "123456");
bool Free();
//use
int InsertMacInfo(_mac_info* _info);
int GetMacInfo(_mac_info* _info)
private:
bool ADOExecute(const char* sql);
int DataWrite(const char* sql);
private:
bool m_bInitOk;
my_mutex_t* m_pDbMutex;
char m_sHostIp[256];
int m_nHostPort;
char m_sDataBase[256];
char m_sAccount[256];
char m_sPwd[256];
BOOL m_bTest;
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pADOSet;
_RecordsetPtr m_pADOSetAuto;
};
#endif
database.cpp文章来源:https://www.toymoban.com/news/detail-443168.html
/*
* Filename: DataBase.cpp
* Modify: ybLin
* Description: database
* *******************************************************/
#ifdef WIN32
#include <time.h>
#include <Windows.h>
#include <io.h>
#endif
#include "DataBase.h"
#include <time.h>
#include <stdio.h>
#include <Windows.h>
#include <time.h>
#include <basic/myThread.h>
int SwitchPath(char* target, const char* src)
{
while (*src != '\0') {
if (*src == '\\')
*target++ = *src;
*target++ = *src++;
}
*target = '\0';
return 0;
}
CDataBase::CDataBase(const char* hostIP)
{
m_nCheckModel = false;
m_pDbMutex = my_mutex_init();
memset(m_sDataBase, 0, sizeof(m_sDataBase));
memset(m_sHostIp, 0, sizeof(m_sHostIp));
m_nHostPort = 0;
memset(m_sAccount, 0, sizeof(m_sAccount));
memset(m_sPwd, 0, sizeof(m_sPwd));
m_bInitOk = false;
}
CDataBase::~CDataBase()
{
if (m_bInitOk)
Free();
my_mutex_destroy(m_pDbMutex);
}
void CDataBase::UpdateDB()
{
char sql[1024] = {0};
snprintf(sql, 1023, "if not exists(select * from syscolumns where id=object_id('sninfo') and name='model') ALTER table sninfo add model varchar(30)");
DataWrite(sql);
char sql2[1024] = {0};
snprintf(sql2, 1023, "if not exists(select * from syscolumns where id=object_id('sninfo') and name='model2') ALTER table sninfo add model2 varchar(30)");
DataWrite(sql2);
}
bool CDataBase::Init(const char* hostIP, const char* dataBase, int hostPort,
const char* user, const char* pwd)
{
snprintf(m_sHostIp, 255, "%s", hostIP);
m_nHostPort = hostPort;
snprintf(m_sDataBase, 255, "%s", dataBase);
snprintf(m_sAccount, 255, "%s", user);
snprintf(m_sPwd, 255, "%s", pwd);
return InitDB();
}
bool CDataBase::InitDB()
{
if (m_bInitOk)
return false;
HRESULT hr;
try
{
my_print("CDataBase::InitDB Start.");
char sConnectStr[256] = {0};
snprintf(sConnectStr, sizeof(sConnectStr)-1, "pConn->ConnectionString = "driver={SQL Server};Server=%s,%d;DATABASE=%s;",
m_sHostIp, m_nHostPort, m_sDataBase);
hr = m_pConnection.CreateInstance("ADODB.Connection");
if(SUCCEEDED(hr))
{
m_pConnection->ConnectionTimeout = 5;
hr = m_pConnection->Open(sConnectStr, m_sAccount, m_sPwd, 0);
}
my_print("yibin test CDataBase::InitDB Success.");
}
catch(_com_error e)
{
char sErrorMsg[256];
snprintf(sErrorMsg, sizeof(sErrorMsg)-1, "数据库连接错误:%s", e.Description());
my_print(sErrorMsg);
return FALSE;
}
m_pADOSet.CreateInstance(__uuidof(Recordset));
m_pADOSetAuto.CreateInstance(__uuidof(Recordset));
m_bInitOk = true;
return m_bInitOk;
}
bool CDataBase::Free()
{
if(m_bInitOk)
{
if(adStateOpen == m_pConnection->State)
m_pConnection->Close();
m_pConnection.Release();
if(adStateOpen == m_pADOSet->State)
m_pADOSet->Close();
m_pADOSet.Release();
if(adStateOpen == m_pADOSetAuto->State)
m_pADOSetAuto->Close();
m_pADOSetAuto.Release();
m_bInitOk = false;
}
else
{
return false;
}
return true;
}
bool CDataBase::ADOExecute(const char* sql)
{
if ( m_pADOSet->State)
{
m_pADOSet->Close();
}
try
{
m_pADOSet->Open(sql, m_pConnection.GetInterfacePtr(), adOpenStatic,
adLockOptimistic, adCmdUnknown);
return true;
}
catch(_com_error &e)
{
char sErrorMsg[256];
snprintf(sErrorMsg, sizeof(sErrorMsg)-1, "Execute SQL:%s ERROR:%s",
sql, e.ErrorMessage());
my_print(sErrorMsg);
return false;
}
return true;
}
int CDataBase::DataWrite(const char* sql)
{
int nRet = 0;
char str[1024] = {0};
my_mutex_lock(m_pDbMutex);
if (!m_bInitOk)
{
my_mutex_unlock(m_pDbMutex);
return -1;
}
SwitchPath(str, sql);
if (ADOExecute(str) != true)
{
Free();
InitDB();
if (m_bInitOk && ADOExecute(str) == true) {
}
else
{
nRet = -1;
}
}
my_mutex_unlock(m_pDbMutex);
return nRet;
}
//use
int CDataBase::InsertMacInfo(_mac_info* _info)
{
char sql[1024] = {0};
snprintf(sql, 1023, "select Mac,SNCode,AddTime from IPCInfo WHERE Mac = '%s'", _info->sMacId);
DataWrite(sql);
if(!m_pADOSet->adoEOF)
return -10;
//addTime
SYSTEMTIME sysTime = {0};
GetLocalTime(&sysTime);
_snprintf(_info->sAddTime, sizeof(_info->sAddTime)-1, "%d-%02d-%02d %02d:%02d:%02d",
sysTime.wYear, sysTime.wMonth, sysTime.wDay, sysTime.wHour, sysTime.wMinute, sysTime.wSecond);
snprintf(sql, 1023, "INSERT INTO IPCInfo (Materiel, OrderNo, Model, Lens, SNCode, Mac, AddTime) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s')",
_info->sMateriel, _info->sOrderNo, _info->sModel, _info->sLens, _info->sSNCode, _info->sMacId, _info->sAddTime);
return DataWrite(sql);
}
int CDataBase::GetMaxMacInfo(_mac_info* _info)
{
char sql[1024] = {0};
snprintf(sql, 1023, "select Mac,SNCode,AddTime from IPCInfo order by Mac desc");
unsigned mac_calc[6] = {0};
if (DataWrite(sql) != 0)
{
my_print("DataWrite Fail \n");
return -2;
}
else
{
_variant_t Holder;
if(!m_pADOSet->adoEOF)
{
Holder = m_pADOSet->GetCollect("Mac");
snprintf(_info->sMacId,
sizeof(_info->sMacId)-1, Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder);
Holder = m_pADOSet->GetCollect("SNCode");
snprintf(_info->sSNCode,
sizeof(_info->sSNCode)-1, Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder);
Holder = m_pADOSet->GetCollect("AddTime");
snprintf(_info->sAddTime,
sizeof(_info->sAddTime)-1, Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder);
}
}
if(sscanf(_info->sMacId, "%02X%02X%02X%02X%02X%02X", &mac_calc[5], &mac_calc[4],
&mac_calc[3], &mac_calc[2], &mac_calc[1], &mac_calc[0]) != 6)
{
my_print("sscanf Fail \n");
return -3;
}
//addTime
char szTime[32] = {0};
char AddDate[32] = {0};
char AddTime[32] = {0};
SYSTEMTIME sysTime = {0};
GetLocalTime(&sysTime);
snprintf(szTime, sizeof(szTime)-1, "%d-%02d-%02d",
sysTime.wYear, sysTime.wMonth, sysTime.wDay);
time_t iTime = time(NULL);
char sTime[32] = {0};
struct tm* ptm = localtime(&iTime);
_snprintf(sTime, sizeof(sTime)-1, "%4d-%02d-%02d", ptm->tm_year + 1900,
ptm->tm_mon + 1, ptm->tm_mday);
sscanf(_info->sAddTime, "%s %s", AddDate, AddTime);
char SNPre[32] = {0};
char SNDate[32] = {0};
if(strcmp(szTime, AddDate) == 0)
{
strncpy (SNPre, _info->sSNCode, 3);
int year;
int month;
int day;
char date[32] = {0};
sscanf(AddDate, "%d-%2d-%2d", &year, &month, &day);
_snprintf(date, sizeof(date)-1, "%d%02d%02d", year, month, day);
int snNum = atof(_info->sSNCode+11)+1;
memset(_info->sSNCode, 0, sizeof(_info->sSNCode));
_snprintf(_info->sSNCode, sizeof(_info->sSNCode)-1, "%s%s%04d", SNPre, date, snNum);
}
else
{
strncpy (SNPre, _info->sSNCode, 3);
snprintf(SNDate, sizeof(szTime)-1, "%d%02d%02d", ptm->tm_year + 1900,
ptm->tm_mon + 1, ptm->tm_mday);
snprintf(_info->sSNCode, sizeof(_info->sSNCode), "%s%s%s",
SNPre, SNDate, "0001");
}
_snprintf(_info->sMacId, sizeof(_info->sMacId)-1, "%02X%02X%02X%02X%02X%02X",
mac_calc[5], mac_calc[4], mac_calc[3], mac_calc[2], mac_calc[1], mac_calc[0]);
return 0;
}
1.6 结果展示
文章来源地址https://www.toymoban.com/news/detail-443168.html
到了这里,关于常用数据库之sql server的使用和搭建的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!