當前位置:首頁 » 數據倉庫 » c連接資料庫的步驟
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c連接資料庫的步驟

發布時間: 2023-06-02 22:42:51

① c怎麼連接sql server資料庫

1.准備工作: 准備相關的軟體(Eclipse除外,開源軟體可以從官網下載) <1>.Microsoft SQL server 2005 Express Edition 下載地址:http://download.microsoft.com/download/0/9/0/09020fab-d2c3-4a8c-b9e0-db53a7a30ae8/SQLEXPR_CHS.EXE <2>.SQL Server Management Studio 下載地址:http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=c243a5ae-4bd1-4e3d-94b8-5a0f62bf7796#filelist <3>.SQL Server 2005 driver for JDBC 下載地址:http://download.microsoft.com/download/8/B/D/8BDABAE2-B6EA-41D4-B903-7916EF3690EF/sqljdbc_1.2.2323.101_enu.exe 2.JDBC連接SQL Server的驅動安裝 ,前兩個是屬於資料庫軟體,正常安裝即可(注意資料庫登陸不要使用windows驗證) 是用java連接嗎? 如果是,方法如下: <1> 將JDBC解壓縮到任意位置,比如解壓到C盤program files下面,並在安裝目錄里找到sqljdbc.jar文件,得到其路徑開始配置環境變數 在環境變數classpath 後面追加 C:\Program Files\Microsoft SQL Server2005 JDBC Driver\sqljdbc_1.2\enu\sqljdbc.jar <2> 設置SQLEXPRESS伺服器: a.打開SQL Server Configuration Manager -> SQLEXPRESS的協議 -> TCP/IP b.右鍵單擊啟動TCP/IP c.雙擊進入屬性,把IP地址中的IP all中的TCP埠設置為1433 d.重新啟動SQL Server 2005服務中的SQLEXPRESS伺服器 e.關閉SQL Server Configuration Manager <3> 打開剛剛安裝好的 SQL Server Management Studio,連接SQLEXPRESS伺服器, 新建資料庫,起名字為sample <4> 打開Eclipse a.新建工程-> Java -> Java project,起名為Test b.選擇eclipse->窗口->首選項->Java->installed JRE 編輯已經安裝好的jdk,查找目錄添加sqljdbc.jar c.右鍵單擊目錄窗口中的Test, 選擇Build Path ->Configure Build Path..., 添加擴展jar文件,即把sqljdbc.jar添加到其中 <5> 編寫Java代碼來測試JDBC連接SQL Server資料庫

② 用c語言怎麼連接資料庫呢

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

③ 用C語言怎麼實現與資料庫的連接

#include<mysql/mysql.h>

#include<stdio.h>

intmain()

{

MYSQL*conn;

MYSQL_RES*res;

MYSQL_ROWrow;

char*server="localhost";//本地連接

char*user="root";//

char*password="525215980";//mysql密碼

char*database="student";//資料庫名

char*query="select*fromclass";//需要查詢的語句

intt,r;

conn=mysql_init(NULL);

if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0))

{

printf("Errorconnectingtodatabase:%s ",mysql_error(conn));

}else{

printf("Connected... ");

}

t=mysql_query(conn,query);

if(t)

{

printf("Errormakingquery:%s ",mysql_error(conn));

}else{

printf("Querymade... ");

res=mysql_use_result(conn);

if(res)

{

while((row=mysql_fetch_row(res))!=NULL)

{

//printf("num=%d ",mysql_num_fields(res));//列數

for(t=0;t<mysql_num_fields(res);t++)

printf("%8s",row[t]);

printf(" ");

}

}

mysql_free_result(res);

}

mysql_close(conn);

return0;

}

(3)c連接資料庫的步驟擴展閱讀

C語言使用注意事項:

1、指針是c語言的靈魂,一定要靈活的使用它:

(1)、指針的聲明,創建,賦值,銷毀等

(2)、指針的類型轉換,傳參,回調等

2、遞歸調用也會經常用到:

(1)、遞歸遍歷樹結構

(2)、遞歸搜索

④ C語言怎樣連接mysql資料庫

mysql是有c語言介面的,安裝相應庫後就可以鏈接了,一般連接mysql的函數是mysql_connect或者mysql_real_connect(大概就是這么拼的吧。。。)可以使用mysql_query執行sql語句

⑤ 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語言怎麼連接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;
}