『壹』 VC++ MFC 的一個sql查詢語句
CADODatabase m_DBCn;//資料庫對象
CADORecordset m_Rs;//記錄集對象
在頭文件里
public:
CADORecordset m_Rs;//新增變數
CADODatabase m_DBCn;//新增變數
CDataGRid m_datagrid;//DataGrid控制項對象
...
CString m_adodc; m_adodc.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;")
_T("Data Source=note.mdb"));
m_DBCn.Open((LPCTSTR)m_adodc); //打開程序資料庫
m_Rs.SetDatabase(&m_DBCn); m_Rs.Open(_T("select * from list where id=1;")); //執行查詢
m_datagrid.SetRefDataSource((LPUNKNOW)m_Rs.GetRecordset());//顯示在DataGrid控制項中
『貳』 vc里執行SQL語句~
因為這個()里要接受參數值的。0,adCmdText。分別對應著這兩個變數的值。也就是說把0,和adCmdText的內容插入數據表中。
你可以這樣改("insert into zrn values(%d,/'%s/')",0,adCmdText)
『叄』 SQL語句 VC++
分類: 電腦/網路 >> 程序設計 >> 其他編程語言
問題描述:
客戶信息記錄
sql.Format("Insert into customer_info_tab(id,name,area,profession,vocation,pany,approach,email,phone,mobile,interest,memo) VALUES(%d,'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",id,m_strName,
m_strSelectedArea,m_strSelectedProfession,m_strSelectedVocation,m_strCompany,m_strSelectedApproach,m_strEmail,m_strPhone,m_strMobile,m_strInterest,m_strMemo);
更新地區信息下拉列表框的數據
TRACE(sql);
m_db.ExecuteSQL(sql);
向界面中插入新的客戶信息
設置該行的其他列的值
m_listCR.SetItemText(nItem,1,m_strName);
m_listCR.SetItemText(nItem,2,m_strSelectedArea);
m_listCR.SetItemText(nItem,3,m_strSelectedProfession);
m_listCR.SetItemText(nItem,4,m_strSelectedVocation);
m_listCR.SetItemText(nItem,5,m_strCompany);
m_listCR.SetItemText(nItem,6,m_strSelectedApproach);
m_listCR.SetItemText(nItem,7,m_strEmail);
m_listCR.SetItemText(nItem,8,m_strPhone);
m_listCR.SetItemText(nItem,9,m_strMobile);
m_listCR.SetItemText(nItem,10,m_strInterest);
m_listCR.SetItemText(nItem,11,m_strMemo);
m_db.CommitTrans();
}
我的要求是 點了列表控制項中的某一行 在上面的文本框中顯示 然後修改內容 在點修改按鈕就修改成功 使得資料庫和列表控制項內容該為修改後的信息
問題出在:里表空間里有3條記錄 我先插入一條記錄在修改就成功,如果修改原來的3條記錄就會提示:違反了PRIMARY KEY約束,『PK_customer_info_ta_31EC6D26'.不能在customer_info_tab沖插入重復鍵,語句終止。
怎麼修改????或者用UPDATE 做什麼弄啊?郁悶死了
解析:
新插入用 insert into
修改用 update
update ttt set xxx=xxxValue where ...
update customer_info_tab
set id = %d, name ='%s', area = '%s', profession = '%s',
vocation = '%s', pany = '%s', approach = '%s',
email = '%s', phone = '%s', mobile = '%s', interest = '%s',
memo = '%s')
where id = %d;
『肆』 怎樣在VC里執行SQL語句
CString sql; sql.Format("insert into zrn(姓名)values('%s')",as); cc->Execute((_bstr_t)sql,0,adCmdText);
『伍』 VC++中如何直接執行sql語句阿
環境:WindowsXP ; VC++6.0 + sp5 1。通過odbc直接執行SQL語句CDatabase mydb;
CRecordset myRecord;
CString strSQL;
CDBVariant cv;try{if ( !mydb.IsOpen() )
mydb.OpenEx("資料庫連接字元串",CDatabase::noOdbcDialog); myRecord.m_pDatabase = &mydb;
//直接執行SQL語句,
//注意,SQL語句字元串一定不能以空格開始
strSQL = "SELECT 欄位或計算列 FROM 表 WHERE 條件";
//strSQL = " SELECT 欄位或計算列 FROM 表 WHERE 條件 "; //這樣不行,會報錯,查詢語句中沒有欄位
myRecord.Open(CRecordset::snapshot,"需要執行的SQL語句"); int i = 0;
while ( !myRecord.IsEOF() ){myRecord.MoveFirst();
myRecord.GetFieldValue(i,cv); //獲得第(i+1)列的數據,數據保存在CDBVariant變數中,不可以直接使用數字0
myRecord.MoveNext();}
『陸』 VC中寫SQl語句,查找數據,資料庫是sql2000
1. 在stdafx.h 添加下面代碼:
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
2. 在C*App::InitInstance()中添加:
AfxOleInit();
3. 下面是一個按鈕的相應事件, 資料庫連接字元串 和 查詢語句換成你的就可以了:
void CTestDlg::OnButton1()
{
CString strDatasource = "my2005";
CString strDatabase = "myoa";
CString strUserName = "sa";
CString strUserPwd = "yourpwd";
CString strConn; // 連接資料庫字元串
CString strSql; // 查詢語句
CString strRet; // 查詢結果
LPTSTR lpszConn = NULL;
LPTSTR lpszSql = NULL;
_RecordsetPtr pRecordset;
_CommandPtr pCommand;
_ConnectionPtr pConnection;
strConn.Format(_T("Provider=SQLOLEDB.1;Data Source=%s;Initial Catalog=%s;User ID=%s; PWD=%s"), strDatasource, strDatabase, strUserName, strUserPwd);
strSql = _T("select pwd from UserInfo where loginName= 'test'");
try
{
HRESULT hr = pConnection.CreateInstance("ADODB.Connection");
lpszConn = new TCHAR[strConn.GetLength()+1];
_tcscpy(lpszConn, strConn);
pConnection->put_ConnectionTimeout(long(5));
if (SUCCEEDED(hr))
{
pConnection->Open(lpszConn, "", "", adModeUnknown); //adModeUnknown adConnectUnspecified
pRecordset.CreateInstance("ADODB.Recordset");
lpszSql = new TCHAR[strSql.GetLength()+1];
_tcscpy(lpszSql, strSql);
pRecordset = pConnection->Execute(lpszSql, NULL, adCmdText);
_variant_t vCount = pRecordset->GetCollect("pwd"); //取得第一個欄位的值放入vCount變數
strRet.Format((_bstr_t)vCount);
MessageBox(strRet); // 顯示查詢結果
}
}
catch(_com_error e)
{
CString strTemp;
strTemp.Format(_T("錯誤:\r\n%s"), e.ErrorMessage());
AfxMessageBox(strTemp);
return;
}
/*釋放資源*/
if (pRecordset->State)
{
pRecordset->Close();
pRecordset.Release();
pRecordset = NULL;
}
if ( pConnection->State)
{
pConnection->Close();
pConnection= NULL;
}
::CoUninitialize(); //釋放COM 資源。
if ( lpszConn != NULL)
delete lpszConn;
if ( lpszSql != NULL)
delete lpszSql;
}
注意: sql 語句 我一般都是 查詢分析器里 執行一下,看是否有錯誤, 然後在代碼中構建 相應的字元串,根據欄位的類型決定要不要 單引號。
『柒』 VC執行sql查詢
SQL語句字元串匹配確實是用=
你的open你錯了,是Open,VC中要注意大小寫啊
另外Open不是你這么用的
virtual BOOL Open(
UINT nOpenType = AFX_DB_USE_DEFAULT_TYPE,
LPCTSTR lpszSQL = NULL,
DWORD dwOptions = none
);
還有你要先與資料庫建立連接啊,Open怎麼知道去查哪個資料庫的表?