query.executeUpdate()
这样就可以用hibernate执行sql语句了 没有任何障碍。
我们用hql就是利用它的简单 便捷 如果为了利用它我们花更多的精力去转sql语句岂不是自找麻烦吗? hibernate的工作设计人员为我们考虑了这一点,所以我们就得以用最简单的方式执行sql了~~
9.Sql Server7.0/2000数据库#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
::CoInitialize(NULL)// 初始化OLE/COM库环境 ,为访问ADO接口做准备
_RecordsetPtr m_pRecordset("ADODB.Recordset")
_ConnectionPtr m_pConnection("ADODB.Connection")
_bstr_t bstrSQL("select * from stu_info")//查询语句
char * query_cmd = "DELETE FROM stu_info WHERE sname = ’本拉登’"
try
{
// 创建Connection对象
m_pConnection.CreateInstance("ADODB.Connection")
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
_bstr_t strConnect= "Provider=SQLOLEDBServer=(local)Database=studentuid=sapwd=123"
//若数据库在网络上则Server为形如(192.168.1.5,3340)
//用户sa和密码123只是针对我的库
m_pConnection->Open(strConnect,"","",adModeUnknown)
if(m_pConnection==NULL)
cerr<<"Lind data ERROR!\n"
// 创建记录集对象
m_pRecordset.CreateInstance(__uuidof(Recordset))
// 取得表中的记录
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockOptimistic,adCmdText)
_variant_t vsnum,vsname,vsage,vssex,vsmajor//对应库中的snum,sname,sage,ssex,smajor
cout <<"学号 姓名 年龄 姓别 专业"
cout <<"\n----------------------------------------------------------------\n"
while (!m_pRecordset->EndOfFile)
{
vsnum = m_pRecordset->GetCollect(_variant_t((long)0))//这儿给字段编号和字段名都可以
vsname = m_pRecordset->GetCollect("sname")
vsage = m_pRecordset->GetCollect("sage")
vssex = m_pRecordset->GetCollect("ssex")
vsmajor = m_pRecordset->GetCollect("smajor")
if (vsnum.vt != VT_NULL &&vsname.vt != VT_NULL &&vsage.vt != VT_NULL
&&vssex.vt != VT_NULL &&vsmajor.vt != VT_NULL)
{
cout.setf(ios::left)
cout <<setw(14) <<(char*)(_bstr_t)vsnum
cout <<setw(14) <<(char*)(_bstr_t)vsname
cout <<setw(8) <<vsage.lVal
cout <<setw(8) <<(char*)(_bstr_t)vssex
cout <<setw(20) <<(char*)(_bstr_t)vsmajor
cout.unsetf(ios::left)
cout <<endl
}
m_pRecordset->MoveNext()///移到下一条记录
}
cout <<"\n----------------------------------------------------------------\n"
cout <<"\n请输入你要添加的学生信息\n"
cout <<"学号:"
cin >>student.snum
cout <<"\n姓名:"
cin >>student.sname
cout <<"\n年龄:"
cin >>student.sage
cout <<"\n姓别:"
cin >>student.ssex
cout <<"\n专业:"
cin >>student.smajor
m_pRecordset->MoveFirst()//移动到第一条记录
m_pRecordset->AddNew()///添加新记录
m_pRecordset->PutCollect("snum",_variant_t(student.snum))
m_pRecordset->PutCollect("sname",_variant_t(student.sname))
m_pRecordset->PutCollect("sage",_variant_t(student.sage))
m_pRecordset->PutCollect("ssex",_variant_t(student.ssex))
m_pRecordset->PutCollect("smajor",_variant_t(student.smajor))
m_pRecordset->Update()
m_pConnection->Execute(query_cmd,NULL,1)//用Execute执行sql语句来删除
m_pRecordset->Close()// 关闭记录集
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
cerr <<"\nERROR:" <<(char*)e.Description()//抛出异常
}
if(m_pConnection->State)
m_pConnection->Close()
::CoUninitialize()
3.显示表格
/*
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
*/
CoInitialize(NULL)
_bstr_t varSource="Provider=Microsoft.Jet.OLEDB.4.0Data Source=*.mdb"
//_bstr_t varSource="Data Source=myServerAddressInitial Catalog=myDataBaseUser Id=myUsernamePassword=myPassword"
_ConnectionPtr m_pConnection(_uuidof(Connection))
m_pConnection->Open(varSource,"","",adModeUnknow)
//打开属性为默认(adModeRead(只读),adModeWrite(可写),adModeReadWrite(可读写)等)
_RecordsetPtr m_pSet(_uuid(Recordset))
try {
HRESULT hr=m_pSet->Open(%%1,m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockPessimistic,adCmdText)
}
catch(_com_error *e){
AfxMessageBox(e->ErrorMessage())
}
if(SUCCESSED(hr))
{
//表打开成功
}
FieldsPtr p_fields=m_pSet->Fields
FieldPtr p_field
_variant_t var_index
LPCSTR field_name
int index=0
_bstr_t bstr_field_name
int countfields=p_fields->GetCount()
CString *Column=new CString[countfields]
CListCtrl *pList=(CListCtrl*)GetDlgItem(%%1)//IDC_LIST_TABLEDATA
VERIFY(pList)
pList->DeleteAllItems()
for(index=0index<countfieldsindex++)
{
var_index.vt=VT_I4
var_index.IVal=index
p_field=p_fields->Item[var_index]
bstr_field_name=p_field->GetName()
field_name=(LPCSTR)bstr_field_name
Column[index]=field_name
int ColumnWidth=Column[index].GetLength()*15
pList->InsertColumn(index,field_name,LVCFMT_CENTER,ColumnWidth)
}
int i=0
_bstr_t vCol
//pList->SetTextBkColor(RGB(122,200,122))
//pList->SetTextColor(RGB(0,0,200))
while(!m_pSet->adoEOF)
{
pList->Insert(i,atoi(i))
for(int j=0j<countfieldsj++)
{
vCol=m_pSet->GetCollect((long)j)
pList->SetItemText(i,j,vCol)
}
m_pSet->MoveNext()
i++
}
CoUninitialize(NULL)
4. *** 作表格
/*
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
*/
CoInitialize(NULL)
_bstr_t varSource="Provider=Microsoft.Jet.OLEDB.4.0Data Source=*.mdb"
//_bstr_t varSource="Data Source=myServerAddressInitial Catalog=myDataBaseUser Id=myUsernamePassword=myPassword"
_ConnectionPtr m_pConnection(_uuidof(Connection))
m_pConnection->Open(varSource,"","",adModeUnknow)
//打开属性为默认(adModeRead(只读),adModeWrite(可写),adModeReadWrite(可读写)等)
_RecordsetPtr m_pSet(_uuid(Recordset))
try {
HRESULT hr=m_pSet->Open(%%1,m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockPessimistic,adCmdText)
}
catch(_com_error *e){
AfxMessageBox(e->ErrorMessage())
}
if(SUCCESSED(hr))
{
//表打开成功
}
FieldsPtr p_fields=m_pSet->Fields
FieldPtr p_field
_variant_t var_index
LPCSTR field_name
int index=0
_bstr_t bstr_field_name
int countfields=p_fields->GetCount()
CString *Column=new CString[countfields]
CListCtrl *pList=(CListCtrl*)GetDlgItem(%%1)//IDC_LIST_TABLEDATA
VERIFY(pList)
pList->DeleteAllItems()
for(index=0index<countfieldsindex++)
{
var_index.vt=VT_I4
var_index.IVal=index
p_field=p_fields->Item[var_index]
bstr_field_name=p_field->GetName()
field_name=(LPCSTR)bstr_field_name
Column[index]=field_name
int ColumnWidth=Column[index].GetLength()*15
pList->InsertColumn(index,field_name,LVCFMT_CENTER,ColumnWidth)
}
int i=0
_bstr_t vCol
//pList->SetTextBkColor(RGB(122,200,122))
//pList->SetTextColor(RGB(0,0,200))
while(!m_pSet->adoEOF)
{
pList->Insert(i,atoi(i))
for(int j=0j<countfieldsj++)
{
vCol=m_pSet->GetCollect((long)j)
pList->SetItemText(i,j,vCol)
}
m_pSet->MoveNext()
i++
}
CoUninitialize(NULL)
6.关闭时断开连接
::ExitInstance()
{
if (adStateOpen == ADOConn->State)
ADOConn->Close()
ADOConn.Release()
if(adStateOpen == m_pADOset->State)
m_pADOset->Close()
m_pADOset.Release()
return CWinApp::ExitInstance()
}
22.批量执行SQL和存储过程
22.1、如果用ODBC访问数据库的话,可参考下列代码:
CDatabase * pDatabase = new CDatabase
TRY
{
pDatabase->OpenEx( _T("DSN=ODBCNameUID=%%1PWD=%%2"), CDatabase::noOdbcDialog)
}
CATCH (CDBException, e)
{
delete pDatabase
return
}END_CATCH
SQL.Format("exec sp_Name")//有参数的话直接写再后面
pDatabase->ExecuteSQL(SQL)
pDatabase->Close()
delete pDatabase
22.2、用ADO调用存储过程
_ConnectionPtr Conn = NULL
_RecordsetPtr Rs = NULL
_CommandPtr Cmd = NULL
CoInitialize(NULL)
Conn.CreateInstance ( __uuidof(Connection))
Rs.CreateInstance (__uuidof(Recordset))
Cmd.CreateInstance (__uuidof(Command))
Conn->Open(L"db", L"sa", L"", adOpenUnspecified)//打开正常
//如下为_CommandPtr对象参数的赋值和调用
Cmd->ActiveConnection = Conn
Cmd->CommandText = "SP_TEST"
//数据库中实际存在这个测试存储过程,select * from atable,不传递参数
Cmd->CommandType = adCmdStoredProc
Cmd->Parameters->Refresh()
Rs = Cmd->Execute( NULL,NULL, adCmdUnknown ) //COM出错。
//注释掉的代码为直接的SQL语句提交,运行正确。
//Cmd->ActiveConnection=Conn
//Cmd->CommandText = "select * from atable"
//Cmd->CommandType = adCmdText
//Cmd->Parameters->Refresh()
//Rs = Cmd->Execute(NULL,NULL,adCmdUnknown)
Rs->Close()
Conn->Close()
CoUninitialize()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)