當前位置:首頁 » 操作系統 » c連接資料庫

c連接資料庫

發布時間: 2022-08-03 09:14:59

『壹』 c連接oracle資料庫的連接語句

我只會在unix下面寫,嘿嘿win的我還不會呢。。
把代碼貼上。至於怎麼
編譯,再查查吧~
int
main()
{
EXEC
sql
BEGIN
DECLARE
SECTION;
char
oc_passwd[101];
/*資料庫密碼*/
char
oc_userid[101];
/*資料庫用戶名*/
char
oc_dbname[101];
/*資料庫名*/
char
oc_coad[101];
EXEC
SQL
END
DECLARE
SECTION;
memset(oc_passwd,
0x00,
sizeof(oc_passwd));
memset(oc_userid,
0x00,
sizeof(oc_userid));
memset(oc_dbname,
0x00,
sizeof(oc_dbname));
/*取資料庫用戶名*/
strcpy(oc_userid,
"userid");
/*取資料庫用戶密碼*/
strcpy(oc_passwd,
"passwd")
;
/*取資料庫名*/
strcpy(oc_dbname,
"dbname");
EXEC
SQL
CONNECT
:oc_userid
IDENTIFIED
BY
:oc_passwd
USING
:oc_dbname;
if
(sqlca.sqlcode
!=
0)
{
printf("用戶名[%s]密碼[%s]資料庫[%s]\n",
oc_userid,
oc_passwd,
oc_dbname);
printf("連接資料庫失敗,sqlcode=%d\n",
sqlca.sqlcode);
return
-1;
}
/*讀table取coad欄位*/
memset(oc_coad,
0x00,
sizeof(oc_coad));
EXEC
SQL
SELECT
coad
INTO
:oc_coad
FROM
table
WHERE
1=1;
if
(sqlca.sqlcode
==
NORECORD)
{
printf("查詢無記錄\n");
return
-1;
}
else
if
(sqlca.sqlcode
!=
0)
{
printf("查詢失敗,sqlcode=%d\n",
sqlca.sqlcode);
return
-1;
}
return
0;
}

『貳』 C++如何連接資料庫 用什麼方式最好

1、在stdafx.h文件最後(即#endif // _AFX_NO_AFXCMN_SUPPORT下面)添加:
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")

2、初始化COM:
AfxOleInit();//這行代碼要放在功能執行前,如果是基於對話框建立的程序,那就放在第一個對話框類的OnInitDialog()函數的return TRUE;前

3、在用到資料庫的地方:
_ConnectionPtr m_pConnection;///聲明資料庫連接變數
_RecordsetPtr m_pRecordset;///聲明資料庫集合變數
CString strCn;
strCn.Empty();

(1)連接資料庫
HRESULT hr;
try
{
_variant_t RecordsAffected;
hr = m_pConnection.CreateInstance("ADODB.Connection");///創建Connection對象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open("DSN=test;UID=;PWD=;","","",adModeUnknown);///連接資料庫
}
}
catch( _com_error e)///捕捉異常
{
CString errormessage;
errormessage.Format("連接資料庫失敗!\r\n錯誤信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///顯示錯誤信息
}

(2)通過SQL讀數據
CString sql;
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
}
catch(_com_error e)///捕捉異常
{
CString errorMessage = e.ErrorMessage();
AfxMessageBox("讀取數據時出錯:"+sql+errorMessage);///顯示錯誤信息
}

(3)通過sql語句添加、修改、刪除記錄
_variant_t RecordsAffected;

try
{
m_pConnection->Execute((_bstr_t)Sql,&RecordsAffected,adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}

『叄』 c語言怎麼連接mysql資料庫

如鵬網上有詳細的視頻教程,楊中科的C語言也能幹大事,裡面講得很清楚。要是在這里講需要寫很多東西,累手,還沒有視頻直觀

『肆』 c語言怎樣連接資料庫

別胡思亂想了,你都用了微軟的sql server了,用api吧
除非你用sqlite3,用c介面還情有可原

徐琿冠郊普蘆姓祠止鞏郎准贊四煢鄞昶下奘君船

『伍』 C++或C程序如何與資料庫建立連接

一般要看使用的資料庫。如果
操作
sql
server
需要用到
ado
驅動,這種驅動使用mfc做的包裝類比較多一些,在控制台直接編寫代碼可能稍顯繁瑣。
如果操作mysql,在安裝mysql的時候,有相應的include頭文件和庫文件,可以在自己的ide開發環境中進行設置。

『陸』 如何用C語言連接MYSQL資料庫

1、配置ODBC數據源。
2、使用SQL函數進行連接。
對於1、配置數據源,配置完以後就可以編程操作資料庫了。
對於2、使用SQL函數進行連接,參考代碼如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include<windows.h>
#include<sql.h>
#include<sqlext.h>
void main()
{
HENV henv; //環境句柄
HDBC hdbc; //數據源句柄
HSTMT hstmt; //執行語句句柄
unsigned char datasource[]="數據源名稱"; //即源中設置的源名稱
unsigned char user[]= "用戶名"; //資料庫的帳戶名
unsigned char pwd[]= "密碼"; //資料庫的密碼
unsigned char search[]="select xm from stu where xh=0";
SQLRETURN retcode; //記錄各SQL函數的返回情況
// 分配環境句柄
retcode= SQLAllocEnv(&henv); // 等介於 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL
, &henv);
// 設置ODBC環境版本號為3.0
retcode= SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
// 分配連接句柄
retcode= SQLAllocConnect(henv,&hdbc); // 等介於 SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
//設置連接屬性,登錄超時為*rgbValue秒(可以沒有)
// SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)(rgbValue), 0);
//直接連接數據源
// 如果是windows身份驗證,第二、三參數可以是

『柒』 c語言怎麼連接mysql資料庫 代碼

//vc工具中添加E:\WAMP\BIN\MYSQL\MYSQL5.5.8\LIB 路徑
//在工程設置-》鏈接》庫模塊中添加 libmysql.lib
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <winsock.h>
#include "E:\wamp\bin\mysql\mysql5.5.8\include\mysql.h"
void main(){
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server ="localhost";
char *user ="root";
char *password="";
char *database="test";
char sql[1024]="select * from chinaren";
conn=mysql_init(NULL);
if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0)){
fprintf(stderr,"%s\n",mysql_error(conn));
exit(1);
}
if(mysql_query(conn,sql)){
fprintf(stderr,"%s\n",mysql_error(conn));
exit(1);
}
res=mysql_use_result(conn);
while((row = mysql_fetch_row(res))!=NULL){
printf("%s\n",row[2]);
}
mysql_free_result(res);
mysql_close(conn);
}
===============================
#if defined(_WIN32) || defined(_WIN64) //為了支持windows平台上的編譯
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"
//定義資料庫操作的宏,也可以不定義留著後面直接寫進代碼
#define SELECT_QUERY "show tables;"
int main(int argc, char **argv) //char **argv 相當於 char *argv[]
{
MYSQL mysql,*handle; //定義資料庫連接的句柄,它被用於幾乎所有的MySQL函數
MYSQL_RES *result; //查詢結果集,結構類型
MYSQL_FIELD *field ; //包含欄位信息的結構
MYSQL_ROW row ; //存放一行查詢結果的字元串數組
char querysql[160]; //存放查詢sql語句字元串
//初始化
mysql_init(&mysql);
//連接資料庫
if (!(handle = mysql_real_connect(&mysql,"localhost","user","pwd","dbname",0,NULL,0))) {
fprintf(stderr,"Couldn't connect to engine!\n%s\n\n",mysql_error(&mysql));
}
sprintf(querysql,SELECT_QUERY,atoi(argv[1]));
//查詢資料庫
if(mysql_query(handle,querysql)) {
fprintf(stderr,"Query failed (%s)\n",mysql_error(handle));
}
//存儲結果集
if (!(result=mysql_store_result(handle))) {
fprintf(stderr,"Couldn't get result from %s\n", mysql_error(handle));
}
printf("number of fields returned: %d\n",mysql_num_fields(result));
//讀取結果集的內容
while (row = mysql_fetch_row(result)) {
printf("table: %s\n",(((row[0]==NULL)&&(!strlen(row[0]))) ? "NULL" : row[0]) ) ;
}
//釋放結果集
mysql_free_result(result);
//關閉資料庫連接
mysql_close(handle);
system("PAUSE");
//為了兼容大部分的編譯器加入此行
return 0;
}

『捌』 c語言如何和資料庫連接

C函數庫沒有相應的資料庫連接介面函數。
只能夠嘗試用二進制或文本模式讀寫文件,來模擬相應的資料庫操作等。
可以嘗試下C++庫類,裡面有資料庫連接的介面

『玖』 c/c++怎麼連接資料庫,並執行SQL語句

C++連接SQL資料庫第一步 系統配置
1.設置SQLSERVER伺服器為SQL登錄方式,並且系統安全性中的sa用戶要設置登錄功能為「啟用」,還有必須要有密碼。
2.需要在ODBC中進行數據源配置,數據源選\」SQL SERVER」,登錄方式使用「使用輸入用戶登錄ID和密碼的SQL SERVER驗證」,並填寫登錄名(sa)和密碼,注意一點,密碼不能為空,這就意味著你的sa用戶必須得有密碼。否則無法通過系統本身的安全策略。測試通過就完成了配置。
C++連接SQL資料庫第二步 C++與SQL連接初始化
1.在你所建立的C++項目中的stdafx.h頭文件中引入ADO
具體代碼如下
#import 「c:\Program Files\Common Files\System\ado\msado15.dll」
no_namespace rename(」EOF」, 「adoEOF」) rename(」BOF」, 「adoBOF」)
2.定義_ConnectionPtr變數後調用Connection對象的Open方法建立與伺服器的連接。
數據類型_ConnectionPtr實際上是由類模板_com_ptr_t得到的一個具體的實例類。_ConnectionPtr類封裝了Connection對象的Idispatch介面指針及其一些必要的操作。可以通過這個指針操縱Connection對象。
例如連接SQLServer資料庫,代碼如下:
//連接到MS SQL Server
//初始化指針
_ConnectionPtr pMyConnect = NULL;
HRESULT hr = pMyConnect.CreateInstance(__uuidof(Connection));
if (FAILED(hr))
return;
//初始化鏈接參數
_bstr_t strConnect = 「Provider=SQLOLEDB;
Server=hch;
Database=mytest;
uid=sa; pwd=sa;」; //Database指你系統中的資料庫
//執行連接
try
{
// Open方法連接字串必須四BSTR或者_bstr_t類型
pMyConnect->Open(strConnect, 「」, 「」, NULL);
}
catch(_com_error &e)
{
MessageBox(e.Description(), 「警告」, MB_OK|MB_ICONINFORMATION);
}//發生鏈接錯誤

C++連接SQL資料庫第三步 簡單的數據連接
//定義_RecordsetPtr變數,調用它Recordset對象的Open,即可打開一個數據集
//初始化過程 以下是個實例
_RecordsetPtr pRecordset;
if (FAILED(pRecordset.CreateInstance(__uuidof(Recordset))))
{
return;
}
//執行操作
try
{
pRecordset->Open(_variant_t(」userinfo」),
_variant_t((IDispatch*)pMyConnect),
adOpenKeyset, adLockOptimistic, adCmdTable);
}
catch (_com_error &e)
{
MessageBox(」無法打開userinfo表\」, 「系統提示」,
MB_OK|MB_ICONINFORMATION);
}

C++連接SQL資料庫第四步 執行SQL語句
這里是關鍵,我認為只要你懂點SQL語句那麼一切都會方便許多比用上面的方法簡單,更有效率點。
首先
m_pConnection.CreateInstance(_uuidof(Connection));
//初始化Connection指針
m_pRecordset.CreateInstance(__uuidof(Recordset));
//初始化Recordset指針
CString strSql=」select * from tb_goods」;//具體執行的SQL語句
m_pRecordset=m_pConnection->Execute(_bstr_t(strSql),
NULL, adCmdText);//將查詢數據導入m_pRecordset數據容器
至此 你的SQL語句已經執行完成了m_pRecordset內的數據就是你執行的結果。
取得記錄:
while(!m_pRecordset->adoEOF)//遍歷並讀取name列的記錄並輸出
{
CString temp = (TCHAR *)(_bstr_t)m_pRecordset->GetFields()->GetItem
(」name」)->Value;
AfxMessageBox(temp);
pRecordset->MoveNext();
}

插入記錄
//記得初始化指針再執行以下操作
CString strsql;
strsql.Format(」insert into tb_goods(no,name, price)
values(』%d』,'%s』, %d)」,m_intNo,m_strName,m_intPrice);
m_pRecordset=m_pConnection->
Execute(_bstr_t(strsql),NULL,adCmdText);

修改記錄
CString strsql;
strsql.Format(」update tb_goods set name=』%s』 ,
price=%d where no=%d 「,m_strName,m_intPrice,m_intNo);
m_pRecordset=m_pConnection->Execute(_bstr_t(strsql),NULL,adCmdText);

刪除記錄
CString strsql;
strsql.Format(」delete from tb_goodswhere no= 『%d』 「,m_intNo);
m_pRecordset=m_pConnection->Execute(_bstr_t(strsql),NULL,adCmdText)

『拾』 用c語言怎麼連接資料庫呢

c語言當然可以連接資料庫了。看你是想要連接什麼資料庫呢?各種資料庫都有很多相應的函數實現了。看你想要使用什麼技術了,比如說ado,odbc,,等等都可以連接資料庫。

熱點內容
電腦配置光纖介面怎麼標注 發布:2024-05-17 17:06:56 瀏覽:976
如何用方向鍵控制安卓機 發布:2024-05-17 16:38:11 瀏覽:198
雨田系統源碼 發布:2024-05-17 16:28:06 瀏覽:586
新手直播腳本 發布:2024-05-17 16:27:25 瀏覽:847
python雙引號單引號 發布:2024-05-17 16:19:31 瀏覽:948
0xxc語言 發布:2024-05-17 16:17:40 瀏覽:700
php與java的區別 發布:2024-05-17 16:12:48 瀏覽:340
registrar伺服器地址是什麼 發布:2024-05-17 16:11:46 瀏覽:113
訂閱號助手如何找到密碼 發布:2024-05-17 15:57:47 瀏覽:712
搜解壓縮 發布:2024-05-17 15:38:32 瀏覽:765