利用ADO,我在原有库中动态建了一张表,但打开的时候出问题了,用对象指针始终打不开,但同样的代码,又能打开之前在库中自己建的表,不知问题出来哪里,是不是在动态建表的时候要设置什么打开方式啊?望有经验的人指教一下!没什么分了!见谅!
代码如下:
建表代码:
.................
CString strsql;
strsql.Format("CREATE TABLE %s(id INT,frdnum VARCHAR(20),port VARCHAR(4),name VARCHAR(20))",str);
CoInitialize(NULL);
_ConnectionPtr m_conn(__uuidof(Connection));
_RecordsetPtr m_rs(__uuidof(Recordset));
_variant_t RecordsAffected;
m_conn->ConnectionString="Provider=SQLOLEDB.1;Password=winmainjyl;Persist Security Info=TRUE;User ID=sa;Initial Catalog=chat";
try
{
m_conn->Open("","","",-1);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败!");
exit(0);
}
try
{
m_conn->Execute(_bstr_t(strsql),&RecordsAffected,adCmdText);
}
catch(_com_error e)
{
AfxMessageBox("数据库打开失败!");
exit(0);
} //m_rs->Close();
m_conn->Close();
m_rs.Release();
m_conn.Release();
CoUninitialize();
..................
开表代码:
.......................
CoInitialize(NULL);
_ConnectionPtr m_conn(__uuidof(Connection));
_RecordsetPtr m_rec(__uuidof(Recordset));
m_conn->ConnectionString="Provider=SQLOLEDB.1;Server=127.0.0.1;Password=winmainjyl;Persist Security Info=TRUE;User ID=sa;Initial Catalog=chat";
try
{
m_conn->Open("","","",-1);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败!");
exit(0);
}
try
{
m_rec->Open((LPCTSTR)strconnet,m_conn.GetInterfacePtr(),adOpenDynamic,
adLockOptimistic,adCmdText);
}
catch(_com_error e)
{
AfxMessageBox("数据库打开失败!");
}
...........
代码如下:
建表代码:
.................
CString strsql;
strsql.Format("CREATE TABLE %s(id INT,frdnum VARCHAR(20),port VARCHAR(4),name VARCHAR(20))",str);
CoInitialize(NULL);
_ConnectionPtr m_conn(__uuidof(Connection));
_RecordsetPtr m_rs(__uuidof(Recordset));
_variant_t RecordsAffected;
m_conn->ConnectionString="Provider=SQLOLEDB.1;Password=winmainjyl;Persist Security Info=TRUE;User ID=sa;Initial Catalog=chat";
try
{
m_conn->Open("","","",-1);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败!");
exit(0);
}
try
{
m_conn->Execute(_bstr_t(strsql),&RecordsAffected,adCmdText);
}
catch(_com_error e)
{
AfxMessageBox("数据库打开失败!");
exit(0);
} //m_rs->Close();
m_conn->Close();
m_rs.Release();
m_conn.Release();
CoUninitialize();
..................
开表代码:
.......................
CoInitialize(NULL);
_ConnectionPtr m_conn(__uuidof(Connection));
_RecordsetPtr m_rec(__uuidof(Recordset));
m_conn->ConnectionString="Provider=SQLOLEDB.1;Server=127.0.0.1;Password=winmainjyl;Persist Security Info=TRUE;User ID=sa;Initial Catalog=chat";
try
{
m_conn->Open("","","",-1);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败!");
exit(0);
}
try
{
m_rec->Open((LPCTSTR)strconnet,m_conn.GetInterfacePtr(),adOpenDynamic,
adLockOptimistic,adCmdText);
}
catch(_com_error e)
{
AfxMessageBox("数据库打开失败!");
}
...........
{
m_rec->Open((LPCTSTR)strconnet,m_conn.GetInterfacePtr(),adOpenDynamic,
adLockOptimistic,adCmdText);
}
catch(_com_error e)
{
AfxMessageBox("数据库打开失败!");
}
用这个,看看错误提示是什么 try{
m_rec->Open((LPCTSTR)strconnet,m_conn.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch(_com_error& e)
{
CString strValue;
strValue.Format(_T("%s"),(LPCTSTR)e.Description());
AfxMessageBox(strValue);
}
用一下select tabel_name from user_tables where tbalename='xxxxxxx';检查返回值,能不能得到这个表名。
看楼主是什么数据库了!!!!