用SQLConfigDataSource动态建立与SQL SERVER 的DSN是,默认的身份认证是:使用用户登陆ID和密码的SQL SERVER。所以在连接时老是出错。请问如何在建立DSN时将身份验证设为:使用网络登陆ID的WINDOWS 验证。或者在默认情况下的连接用什么UID,PSW。
程序如下:
char *szDriver = "SQL Server";
char *szAttributes = "DSN=SQL\0SERVER=.\0Description=人事管理系统\0DATABASE=test\0";
if(!SQLConfigDataSource(NULL, ODBC_ADD_SYS_DSN, szDriver, szAttributes))
{
AfxMessageBox(_T("创建数据源失败!"));
return false;
}
try
{
m_pConnection.CreateInstance("ADODB.CONNECTION");//设置连接对象
m_pConnection->ConnectionTimeout = 3;//设置连接超时为3秒
m_pConnection->Open("Provider=MSDASQL; Data Source=SQL", "", "", adConnectUnspecified);
}
程序如下:
char *szDriver = "SQL Server";
char *szAttributes = "DSN=SQL\0SERVER=.\0Description=人事管理系统\0DATABASE=test\0";
if(!SQLConfigDataSource(NULL, ODBC_ADD_SYS_DSN, szDriver, szAttributes))
{
AfxMessageBox(_T("创建数据源失败!"));
return false;
}
try
{
m_pConnection.CreateInstance("ADODB.CONNECTION");//设置连接对象
m_pConnection->ConnectionTimeout = 3;//设置连接超时为3秒
m_pConnection->Open("Provider=MSDASQL; Data Source=SQL", "", "", adConnectUnspecified);
}
解决方案 »
- 客户端如何立即知道服务器端的崩溃
- VS2010下使用CThumbListCtrl出现error LNK2001 错误,非常急!
- 不好意思BZ,这个贴我不知道可以发这里不. 华昌HCE-706读卡器(ID卡),键盘输入,RS-232串口输出, 谁有这个设备的二次开发模块,如果没有,自己可不可以写这个readdata函数
- 请教多线程编程高手,在线程中获得MDI主窗体(MainFrame)中的变量
- MFC中的图片
- 菜鸟问题,怎样动态得设置视图得标题
- 多个线程共用一个SOCKET的问题
- 如何在"static"或"button"中写入不断变化的字?
- 怎样分行写代码?
- 有一个想法,但不知道能不能实现,请帮助看一看
- 要做个弹出对话框,具有 always on top 属性,比如说你在用word写文章,突然会弹出个QQ的消息的对话框来,该怎么设置这个弹出对话框啊?
- 哪位大哥能详细的解析下这句话啊,INT_PTR GetCount() const;
如何通过VC在已知数据库中创建数据表?(ADO)
如果能给点代码就最好了!^_^
{
_bstr_t strSQL;
CString strTemSQL;
CString str, str1, str2, str3;
CString strTabName,strName;
CStringArray stt;
CStringArray stTabName; int i,n;
HRESULT hr(-1); UpdateData(TRUE);
//OnSelchangeComboType();
n=m_List.GetItemCount();
stt.SetSize(n);
for (i=0;i<n;i++)
{
if(m_List.GetItemText(i,1)=="Text")
{
strTemSQL=m_List.GetItemText(i,0) + " " +
m_List.GetItemText(i,1) + "(" +
m_List.GetItemText(i,2) + ")" + ",";
}
else if (m_List.GetItemText(i,1)=="Autoincremnet")
{
strTemSQL=m_List.GetItemText(i,0) + " " +
m_List.GetItemText(i,1) + ",";
}
else if (m_List.GetItemText(i,1)=="Currency")
{
strTemSQL=m_List.GetItemText(i,0) + " " +
m_List.GetItemText(i,1) + ",";
}
else if(m_List.GetItemText(i,1)=="DateTime")
{
strTemSQL=m_List.GetItemText(i,0) + " " +
m_List.GetItemText(i,1) + ",";
}
else if(m_List.GetItemText(i,1)=="Text")
{
strTemSQL=m_List.GetItemText(i,0) + " " +
m_List.GetItemText(i,1) + "(" +
m_List.GetItemText(i,2) + ")" + ",";
}
else if(m_List.GetItemText(i,1)=="Double")
{
strTemSQL=m_List.GetItemText(i,0) + " " +
m_List.GetItemText(i,1) + ",";
}
else if(m_List.GetItemText(i,1)=="Decimal")
{
strTemSQL=m_List.GetItemText(i,0) + " " +
m_List.GetItemText(i,1) + "(" +
m_List.GetItemText(i,2) + "," +
m_List.GetItemText(i,3) + ")" + ",";
}
else if(m_List.GetItemText(i,1)=="Single")
{
strTemSQL=m_List.GetItemText(i,0) + " " +
m_List.GetItemText(i,1) + ",";
}
else if(m_List.GetItemText(i,1)=="Integer")
{
strTemSQL=m_List.GetItemText(i,0) + " " +
m_List.GetItemText(i,1) + ","; }
/* else if (m_List.GetItemText(i,1)=="Yes")
{
strTemSQL=m_List.GetItemText(i,0) + " " +
m_List.GetItemText(i,1) + ",";
}*/
stt[i] = strTemSQL; }
str2 = m_List.GetItemText(0,0);
str3 = "";
for(i=0;i<n;i++)
{
if(ListView_GetCheckState(m_List,i))
{
if(i>0)
{
str1 = m_List.GetItemText(i,0); str.Format("%s,%s",str2,str1);
str2 = str;
}
str.Format("%s%s",str3,stt[i]);
str3 = str;
}
}
// BOOL bOpenFileDialog= false;
// CString filepath;
// CString m_strResult;
// CString strName;
// LPCTSTR lpszDefExt = _T("");
// LPCTSTR lpszFileName= _T("tabAccess");
// //DWORD dwFlags = OFN_HIDEREADONLY;
// DWORD dwFlags = OFN_OVERWRITEPROMPT;
//
//
// LPCTSTR lpszFilter = _T("(*.*)|*.*|");
//
// CFileDialog dlg(bOpenFileDialog,
// lpszDefExt,lpszFileName,
// dwFlags,lpszFilter,CWnd::FromHandle(hMainWnd));
// dlg.m_ofn.lpstrTitle = _T("Êý¾Ý¿âÎļþ");
// if(dlg.DoModal()==IDOK)
// {
// filepath=dlg.GetPathName();
// strName=dlg.GetFileName();
//
// UpdateData(FALSE);
// }
try
{
m_pRecordset=theApp.m_pConnection->OpenSchema(adSchemaTables);
while(!(m_pRecordset->adoEOF))
{
//»ñÈ¡±í¸ñ
_bstr_t table_name = m_pRecordset->Fields->GetItem("TABLE_NAME")->Value;
//»ñÈ¡±í¸ñÀàÐÍ
_bstr_t table_type = m_pRecordset->Fields->GetItem("TABLE_TYPE")->Value;
//¹ýÂËһϣ¬Ö»Êä³ö±í¸ñÃû³Æ£¬ÆäËûµÄÊ¡ÂÔ
if ( strcmp(((LPCSTR)table_type),"TABLE")==0)
{
strTabName=(LPCSTR)table_name;
if(m_strTabName=="")
{
::MessageBox(NULL,"±íÃû²»ÄÜΪ¿Õ,ÇëÊäÈë±íÃû","Ìáʾ",MB_OK);
return false;
} else if(strTabName==m_strTabName)
{
strName.Format("±íÃû%sÒѾ­´æÔÚ,ÇëÖØÐÂÊäÈë!",m_strTabName);
::MessageBox(NULL,strName,"Ìáʾ",MB_OK);
return false;
}
}
m_pRecordset->MoveNext();
}
strName=m_strTabName;
}
catch(_com_error e)///²¶×½Òì³£
{
::MessageBox(NULL,e.Description(),"Ìáʾ",MB_OK);
return FALSE;
}
str.Format("CREATE TABLE %s (ID INTEGER,X decimal(10,2),Y decimal(10,2),Z decimal(10,2),%s CONSTRAINT MyTableConstraint PRIMARY KEY(ID),UNIQUE(X,Y,Z,%s))", strName,str3, str2);
// str = "CREATE TABLE tabAccess (ID INTEGER,X F(10,2),Y F(10,2),Z F(10,2), CONSTRAINT MyTableConstraint PRIMARY KEY(ID), UNIQUE(X,Y,Z))";
// str = "CREATE TABLE %s (ID INTEGER,X Number,Y Double,Z Double, CONSTRAINT MyTableConstraint PRIMARY KEY(ID), UNIQUE(X,Y,Z))";
strSQL= str;
try
{
theApp.m_pConnection->Execute(strSQL,NULL,adCmdText);
//theApp.m_pConnection->Execute(strTemSQL,NULL,adCmdText);
}
// hr = (HRESULT)0;
catch (_com_error &e)
{
::MessageBox(NULL,e.Description(),"Ìáʾ",MB_OK);
//hr = e.WCodeToHRESULT(e.WCode());
return FALSE;
}
//::MessageBox(NULL,"´´½¨±í³É¹¦£¡","Ìáʾ",MB_OK);
UINT j=::MessageBox(NULL,"´´½¨±í³É¹¦£¡","Ìáʾ",MB_OK);;
if(j==IDOK)
{
UINT i=::MessageBox(NULL,"ÊÇ·ñÏÖÔڲ鿴±í½á¹¹","Ìáʾ",MB_YESNO);
if(i==IDYES)
{
UpdateData(true);
CString str="";
/* int ncount=rstab.m_cmbTable.GetCount();
for(int num=0;num<ncount;num++)
{
rstab.m_cmbTable.GetLBText(num,m_strTabName);
}*/
CDlgRecordset dlg(this);
dlg.m_strCMBTable = m_strTabName;
/* int ncount=dlg.m_cmbTable.GetCount();
for(int num=0;num<ncount;num++)
{
dlg.m_cmbTable.GetLBText(num,str);
if(str==m_strTabName)
{
str=m_strTabName;
break;
}
}*/
/// dlg.m_cmbTable.SetCurSel(num);
dlg.DoModal();
}
else
{
CDlgTest dlg(this);
dlg.DoModal();
}
}
return TRUE;}
http://blog.csdn.net/laiyiling/archive/2004/08/28/87654.aspx
创建数据库表同样是执行SQL语句,用_CommandPtr执行语句就可以了,参考
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=201018
string.Format("ODBC;DRIVER={SQL Server};SERVER=%s;UID=%s;PWD=%s", "(local)", "sa", "");
Database->Open(NULL, FALSE, FALSE, string);
Database->ExecuteSQL("create database image");
Database->ExecuteSQL(建表的sql语句);
Database->Close();