⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysqlop.h

📁 vc6连接mysql 显示数据库登陆框
💻 H
字号:
#ifndef MYSQLOP_H_H
#define MYSQLOP_H_H
class mysqlOP
{
public:
 _ConnectionPtr m_pConnection;
 _variant_t dvat;
//1.连接mysql数据库:
BOOL mysqlConnect()
{	
	::CoInitialize(NULL);	  
	 bstr_t strConnect;
try
	{
     m_pConnection.CreateInstance("ADODB.Connection"); 
	 
  	 CString resultStr;
	 TCHAR strPath[MAX_PATH];
	 CString strTemp;
	 memset(strPath,0,MAX_PATH);
	 GetCurrentDirectory(MAX_PATH,strPath);     
	 strTemp="\\myconfig.ini";    
     strTemp=(LPCTSTR)strPath+strTemp;	

	 GetPrivateProfileString("baseSet","dbname",NULL,resultStr.GetBuffer(MAX_PATH),MAX_PATH,strTemp);
  	 
	 
	 strConnect= "Driver={MySQL ODBC 3.51 Driver};Server=localhost;Port=3306;Database=" + (bstr_t)resultStr + ";USER=root;PASSWORD=cycs;Option=3;";
	 m_pConnection->Open(strConnect,"","",adModeUnknown);

	 return TRUE;
	}
	catch(_com_error e)
	{   		
		
		//MessageBox("数据库连接失败!");	
	    return FALSE;   
	}  
}
//2.断开mysql数据库:
BOOL mysqlDisConnect()
{
  try
	{
      if (m_pConnection->State ==adStateClosed)
		  exit(0);
	  else	  
	      m_pConnection->Close();
	  return TRUE;
	}
  catch(_com_error e)
	{   	
		//MessageBox("数据库断开连接失败!");	
	    return FALSE;   
	} 
}
//3.mysql数据库Select方法:
int mysqlSelect(_RecordsetPtr rs,bstr_t strTable,bstr_t strSelect,bstr_t strWhere,
				bstr_t strOrder,bstr_t strGroup,bstr_t strOther)
{
	bstr_t strSQL;
    strSQL="set names gbk";
	m_pConnection->Execute(strSQL,&dvat,adCmdText);
  try
	{
    strSQL = "Select " + strSelect + " From " + strTable;
    if (!strcmp(strWhere,"")==0)
        strSQL = strSQL + " Where " + strWhere;
    
    if (!strcmp(strGroup,"")==0)
        strSQL = strSQL + " Group By  " + strGroup;
   
    if (!strcmp(strOrder,"")==0)
        strSQL = strSQL + " Order By  " + strOrder;
    
    if (!strcmp(strOther,"")==0) 
        strSQL = strSQL + " " + strOther;
	rs->CursorType =adOpenStatic;
	rs->CursorLocation =adUseClient;
    rs->Open((_variant_t)(strSQL),m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
    if (rs->adoEOF)
		{
		   return -1;
		   exit(0);
		}
    else
		{
		   return 0;
		}	
	}
  catch(_com_error e)
	{   				
		//MessageBox("数据库表选择记录失败!");
	    return -2;  		
	}  
}

//4.mysql数据库Insert方法:
int mysqlInsert(bstr_t strTable,bstr_t strInsertVal,bstr_t strInsertField)
{
	bstr_t strSQL;
    strSQL="set names gbk";
	m_pConnection->Execute(strSQL,&dvat,adCmdText);
    
    strSQL = "Insert Into " + strTable;
 try
	{
    if (!strcmp(strInsertField,"")==0)
        strSQL = strSQL + "( " + strInsertField + " )";
	if (!strcmp(strInsertVal,"")==0)
        strSQL = strSQL + " Values( " + strInsertVal + " )";
		
    m_pConnection->Execute(strSQL,&dvat,adCmdText);
        return 0;
	}
  catch(_com_error e)
	{   				
		//MessageBox("数据库表插入失败!");	
	    return -1;   
	}  
}

//5.mysql数据库Update方法:
int mysqlUpdate(bstr_t strTable,bstr_t strUpdateVal,bstr_t strWhere)
{
	bstr_t strSQL;
    strSQL="set names gbk";
	m_pConnection->Execute(strSQL,&dvat,adCmdText);
  try
	{
		strSQL = "Update " + strTable + " Set " + strUpdateVal;
		if (!strcmp(strWhere,"")==0)
			strSQL = strSQL + " Where " + strWhere;
		m_pConnection->Execute(strSQL,&dvat,adCmdText);

        return 0;
	}
  catch(_com_error e)
	{   				
		//MessageBox("数据库更新表失败!");	
	    return -1;   
	}  
}

//6.mysql数据库Delete方法:
int mysqlDelete(bstr_t strTable,bstr_t strWhere)
{
	bstr_t strSQL;
    strSQL="set names gbk";
	m_pConnection->Execute(strSQL,&dvat,adCmdText);
  try
	{
		strSQL = "Delete From  " + strTable;
		if (!strcmp(strWhere,"")==0)
			strSQL = strSQL + " Where " + strWhere;
		m_pConnection->Execute(strSQL,&dvat,adCmdText);

        return 0;
	}
  catch(_com_error e)
	{   				
		//MessageBox("数据库删除指定表指定条件的记录失败!");	
	    return -1;   
	}  
}

//7.mysql数据库DeleteAll方法:
int mysqlDeleteAll(bstr_t strTable)
{
	bstr_t strSQL;
    strSQL="set names gbk";
	m_pConnection->Execute(strSQL,&dvat,adCmdText);
  try
	{
		strSQL = "Truncate table " + strTable;
		m_pConnection->Execute(strSQL,&dvat,adCmdText);

        return 0;
	}
  catch(_com_error e)
	{   				
		//MessageBox("数据库删除指定表的所有记录失败!");	
	    return -1;   
	}  
}

//8.mysql数据库CreateTB方法:
int mysqlCreateTB(bstr_t strTable,bstr_t strMKDB)
{
	bstr_t strSQL;
    strSQL="set names gbk";
	m_pConnection->Execute(strSQL,&dvat,adCmdText);
  try
	{
		strSQL = "Create table " + strTable;
		strSQL = strSQL + strMKDB;
		m_pConnection->Execute(strSQL,&dvat,adCmdText);

        return 0;
	}
  catch(_com_error e)
	{   				
		//MessageBox("数据库建立表格失败!");	
	    return -1;   
	}  
}

//9.mysql数据库CreateIndex方法:
int mysqlCreateIndex(bstr_t indexName,bstr_t strDBName,bstr_t strTableName,bstr_t strFieldsName)
{
	bstr_t strSQL;
    strSQL="set names gbk";
	m_pConnection->Execute(strSQL,&dvat,adCmdText);
  try
	{
		strSQL = "CREATE INDEX " + indexName + " ON " + strDBName + "." + strTableName + strFieldsName;	
		m_pConnection->Execute(strSQL,&dvat,adCmdText);

        return 0;
	}
  catch(_com_error e)
	{   				
		//MessageBox("数据库建立索引失败!");	
	    return -1;   
	}  
}

//10.mysql数据库AddFields方法:
int mysqlAddFields(bstr_t strTable,bstr_t strFields)
{
	bstr_t strSQL;
    strSQL="set names gbk";
	m_pConnection->Execute(strSQL,&dvat,adCmdText);
  try
	{
		strSQL = "alter table " + strTable;	
		strSQL = strSQL + " add column " + strFields;
		m_pConnection->Execute(strSQL,&dvat,adCmdText);

        return 0;
	}
  catch(_com_error e)
	{   				
		//MessageBox("数据库添加字段失败!");	
	    return -1;   
	}  
}

//11.mysql数据库DropFields方法:
int mysqlDropFields(bstr_t strTable,bstr_t strFields)
{
	bstr_t strSQL;
    strSQL="set names gbk";
	m_pConnection->Execute(strSQL,&dvat,adCmdText);
  try
	{
		strSQL = "alter table " + strTable;	
		strSQL = strSQL + " drop column " + strFields;
		m_pConnection->Execute(strSQL,&dvat,adCmdText);

        return 0;
	}
  catch(_com_error e)
	{   				
		//MessageBox("数据库删除字段失败!");	
	    return -1;   
	}  
}

//12.mysql数据库BeginTrans方法:
int mysqlBeginTrans()
{	
  try
	{
        m_pConnection->BeginTrans();
        return 0;
	}
  catch(_com_error e)
	{   				
		//MessageBox("数据库建立事务失败!");	
	    return -1;   
	}  
}

//13.mysql数据库Commit方法:
int mysqlCommit()
{	
  try
	{
        m_pConnection->CommitTrans();
        return 0;
	}
  catch(_com_error e)
	{   				
		//MessageBox("数据库建立事务失败!");	
	    return -1;   
	}  
}

//14.mysql数据库Rollback方法:
int mysqlRollback()
{	
  try
	{
        m_pConnection->RollbackTrans();
        return 0;
	}
  catch(_com_error e)
	{   				
		//MessageBox("数据库建立事务失败!");	
	    return -1;   
	}  
}
};

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -