⑴ linux下c++遠程連接oracle資料庫
你說的復合結構應該是結構體吧。若是舉個例子給你看看
/*
* sample2.pc
*
* This program connects to ORACLE, declares and opens a cursor,
* fetches the names, salaries, and commissions of all
* salespeople, displays the results, then closes the cursor.
*/
#include <stdio.h>
#include <sqlca.h>
#define UNAME_LEN 20
#define PWD_LEN 40
/*
* Use the precompiler typedef』ing capability to create
* null-terminated strings for the authentication host
* variables. (This isn』t really necessary--plain char *』s
* does work as well. This is just for illustration.)
*/
typedef char asciiz[PWD_LEN];
EXEC SQL TYPE asciiz IS STRING(PWD_LEN) REFERENCE;
asciiz username;
asciiz password;
struct emp_info
{
asciiz emp_name;
float salary;
float commission;
};
/* Declare function to handle unrecoverable errors. */
void sql_error();
main()
{
struct emp_info *emp_rec_ptr;
/* Allocate memory for emp_info struct. */
if ((emp_rec_ptr =
(struct emp_info *) malloc(sizeof(struct emp_info))) == 0)
{
fprintf(stderr, "Memory allocation error. ");
exit(1);
}
/* Connect to ORACLE. */
strcpy(username, "SCOTT");
strcpy(password, "TIGER");
EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--");
EXEC SQL CONNECT :username IDENTIFIED BY :password;
printf(" Connected to ORACLE as user: %s ", username);
/* Declare the cursor. All static SQL explicit cursors
* contain SELECT commands. 』salespeople』 is a SQL identifier,
* not a (C) host variable.
*/
EXEC SQL DECLARE salespeople CURSOR FOR
SELECT ENAME, SAL, COMM FROM EMP WHERE JOB LIKE 』SALES%』;
/* Open the cursor. */
EXEC SQL OPEN salespeople;
/* Get ready to print results. */
printf(" The company』s salespeople are-- ");
printf("Salesperson Salary Commission ");
printf("----------- ------ ---------- ");
/* Loop, fetching all salesperson』s statistics.
* Cause the program to break the loop when no more
* data can be retrieved on the cursor.
*/
EXEC SQL WHENEVER NOT FOUND DO break;
for (;;)
{
EXEC SQL FETCH salespeople INTO :emp_rec_ptr;
printf("%-11s%9.2f%13.2f ", emp_rec_ptr->emp_name,emp_rec_ptr->salary, emp_rec_ptr->commission);
}
/* Close the cursor. */
EXEC SQL CLOSE salespeople;
printf(" Arrivederci. ");
EXEC SQL COMMIT WORK RELEASE;
exit(0);
}
void sql_error(msg) char *msg;
{
char err_msg[512];
int buf_len, msg_len;
EXEC SQL WHENEVER SQLERROR CONTINUE;
printf(" %s ", msg);
/* Call sqlglm() to get the complete text of the
* error message.
*/
buf_len = sizeof (err_msg);
sqlglm(err_msg, &buf_len, &msg_len);
printf("%.*s ", msg_len, err_msg);
EXEC SQL ROLLBACK RELEASE;
exit(1);
}
⑵ 請教C#、WCF高手,如何訪問遠程伺服器上的資料庫
兩種方式:1.IP+SID方式 2.配置鏈接方式
1..IP+SID方式
[csharp] view plainprint?
DbHelperOracle.connectionString = string.Format(@"Data Source=(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = {0})(PORT = 1521))
)
(CONNECT_DATA =
(SID = {1})
(SERVER = DEDICATED)
)
);User Id={2};Password={3};",
txtDBServerIP.Text.Trim(), txtOracleSID.Text.Trim(), txtDBUserName.Text.Trim(), txtDBPassword.Text.Trim());
這種方式不需要在Oracle Net Manager管理中添加鏈接配置信息。
2. 2.配置鏈接方式,
在Oracle Net Manager管理中添加鏈接配置信息,然後鏈接字元串如下寫法:
[csharp] view plainprint?
Data Source=TORCL;User Id=myUsername;Password=myPassword;
另外其他的連接方式,參考如下,從其他網站轉來的:
Oracle連接字元串總結
Oracle XE
標准連接
Oracle XE(或者"Oracle Database 10g Express Edition")是一個簡單免費發布的版本。
以下是語法格式:
Driver=(Oracle in XEClient);dbq=111.21.31.99:1521/XE;Uid=myUsername;Pwd=myPassword;
ODBC
新版本連接方式
以下是語法格式:
Driver={Microsoft ODBC for Oracle};Server=myServerAddress;Uid=myUsername;Pwd=myPassword;
老版本連接方式
以下是語法格式:
Driver={Microsoft ODBC Driver for Oracle};ConnectString=OracleServer.world;Uid=myUsername;Pwd=myPassword;
OLE DB, OleDbConnection (.NET)
標准安全連接
此連接方式使用來自微軟的Provider。
以下是語法格式:
Provider=msra;Data Source=MyOracleDB;User Id=myUsername;Password=myPassword;
信任連接
以下是語法格式:
Provider=msra;Data Source=MyOracleDB;Persist Security Info=False;Integrated Security=Yes;
標准安全連接
此連接方式使用來自Oracle的Provider。
以下是語法格式:
Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;User Id=myUsername;Password=myPassword;
信任連接
以下是語法格式:
Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;OSAuthent=1;
以下是語法格式:
Provider=OraOLEDB.Oracle;Data Source=(DESCRIPTION=(CID=GTU_APP)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=myHost)(PORT=myPort)))(CONNECT_DATA=(SID=MyOracleSID)(SERVER=DEDICATED)));User Id=myUsername;Password=myPassword;
Oracle.DataAccess.Client.OracleConnection
標准連接
以下是語法格式:
Data Source=TORCL;User Id=myUsername;Password=myPassword;
帶integrated security的連接
以下是語法格式:
Data Source=TORCL;Integrated Security=SSPI;
帶 ODP.NET 不帶 tnsnames.ora的連接
以下是語法格式:
Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword;
OracleConnection, Oracle Data Provider, ODP.NET, System.Data.OracleClient.OracleConnection
標准連接
以下是語法格式:
Data Source=MyOracleDB;Integrated Security=yes
僅在Oracle8i release 3 或更高版本
指明用戶密與密碼的連接
以下是語法格式:
Data Source=MyOracleDB;User Id=myUsername;Password=myPassword;Integrated Security=no;
這是另一種連接方式不依賴你的DNS.You create a connection string based on the format used in the tnsnames.ora file without the need to actually have one of these files on the client pc.
以下是語法格式:
SERVER=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort))(CONNECT_DATA=(SERVICE_NAME=MyOracleSID)));uid=myUsername;pwd=myPassword;
Some reported problems with the one above and Visual Studio. Use the next one if you've encountered problems.
以下是語法格式:
Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort))(CONNECT_DATA=(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword;
使用連接池
連接池服務如果找不到一個完全匹配連接字元串的連接,他將會創建一個。如果能找到該連接,將會重新使用。
以下是語法格式:
Data Source=myOracleDB;User Id=myUsername;Password=myPassword;Min Pool Size=10;Connection Lifetime=120;Connection Timeout=60;Incr Pool Size=5;Decr Pool Size=2;
Windows驗證
以下是語法格式:
Data Source=myOracleDB;User Id=/;
特權連接
使用SYSOPER的特權
以下是語法格式:
Data Source=myOracleDB;User Id=SYS;Password=SYS;DBA Privilege=SYSOPER;
利用密碼終止函數
當第一次打開連接時,當打開鏈接後,一個密碼過期錯誤被拋出,捕獲這個錯誤並執行OpenWithNewPassword命令行設置新密碼。
以下是語法格式:
Data Source=myOracleDB;User Id=myUsername;Password=myPassword;
oConn.OpenWithNewPassword(sTheNewPassword);
代理驗證
以下是語法格式:
Data Source=myOracleDB;User Id=myUsername;Password=myPassword;Proxy User Id=pUserId;Proxy Password=pPassword;
Core Labs OraDirect (.NET)
標准連接
以下是語法格式:
User ID=myUsername;Password=myPassword;Host=ora;Pooling=true;Min Pool Size=0;Max Pool Size=100;Connection Lifetime=0;
Data Shape
MS Data Shape
以下是語法格式:
Provider=MSDataShape.1;Persist Security Info=False;Data Provider=MSDAORA;Data Source=orac;User Id=myUsername;Password=myPassword;
⑶ Wincc的C腳本如何連接到遠程SQLServer資料庫具體代碼如何
1、建立存儲:
CREATE PROCere[owner.]procerename[;number]
[[(]@parameter_name datatype[=default][OUTput]
[,@parameter_name datatype[=default][OUTput]]...[)]]
[WITH RECOMPILE]
AS SQL_statements
2、存儲規則:
[EXECute][@return-status=]
[[[server.]database.]owner.]procerename[;number]
[[@parameter_name=]value|[@parameter_name=]@varialbe[OUTput]
[,[@parameter_name=]value|[@parameter_name=]@variable[OUTput]...]]
[WITH RECOMPILE]
⑷ c語言怎樣連接資料庫(c語言和資料庫連接)
1、配置ODBC數據源。
2、使用SQL函數進行連接。
對於1、配置數據源,配置完以後就可以編程操作資料庫了。
對於2、使用SQL函數進行連接,參考代碼如下:
#include
#include
#include
voidmain()
{
HENVhenv;//環境句柄
HDBChdbc;//數據源句柄
HSTMThstmt;//執行語句句柄
unsignedchardatasource[]="數據源名稱";//即源中設置的源名稱
unsignedcharuser[]="用戶名";//數襲此據庫的帳戶拍野迅名
unsignedcharpwd[]="密碼";//資料庫的密碼
unsignedcharsearch[]="selectxmfromstuwherexh=0";
SQLRETURNretcode;//記錄各SQL函數的返回情況
//分配環境句柄
retcode=SQLAllocEnv(&henv);//等介於(SQL_HANDLE_ENV,SQL_NULL
,&henv);
//設置ODBC環境版本號為3.0
retcode=(henv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
//分配連接句柄
retcode=(henv,&hdbc);//等介於(SQL_HANDLE_DBC,henv,&hdbc);
//設置連接屬性,登錄超時為*rgbValue秒(可以沒有)
//(hdbc,SQL_LOGIN_TIMEOUT,(SQLPOINTER)(rgbValue),0);
//直接連接數據源
//如果是windows身份驗證,第二、三參數可以是
,也可以是任何字串
//SQL_NTS即"
retcode=SQLConnect(hdbc,datasource,SQL_NTS,user,SQL_NTS,pwd,SQL_NTS);
//分配語句句柄
retcode=(hdbc,&hstmt);//等介於(SQL_HANDLE_STMT,hdbc,&hstmt);
//直接執行查詢語句
retcode=(hstmt,search,SQL_NTS);
//將數據緩沖區綁定資料庫中的相應脊塵欄位(i是查詢結果集列號,queryData是綁定緩沖區,BUFF_LENGTH是緩沖區長度)
SQLBindCol(hstmt,i,SQL_C_CHAR,queryData[i-1],BUFF_LENGTH,0);
//遍歷結果集到相應緩沖區queryData
SQLFetch(hstmt);
/*
*對遍歷結果的相關操作,如顯示等
*/
//注意釋放順序,否則會造成未知錯誤!
(SQL_HANDLE_STMT,hstmt);
(hdbc);
(SQL_HANDLE_DBC,hdbc);
(SQL_HANDLE_ENV,henv);
}
⑸ 如何遠程訪問MySQL資料庫詳解
默認情況下,mysql只允許本地登錄,如果要開啟遠程連接,則需要修改/etc/mysql/my.conf文件。
一、修改/etc/mysql/my.conf
找到bind-address = 127.0.0.1這一行
改為bind-address = 0.0.0.0即可
二、為需要遠程登錄的用戶賦予許可權
1、新建用戶遠程連接mysql資料庫
grant all on *.* to admin@'%' identified by '123456' with grant option;
flush privileges;
允許任何ip地址(%表示允許任何ip地址)的電腦用admin帳戶和密碼(123456)來訪問這個mysql server。
注意admin賬戶不一定要存在。
2、支持root用戶允許遠程連接mysql資料庫
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
flush privileges;
三、查看系統用戶
⑹ c與資料庫連接的詳細步驟
C#連接資料庫有以下幾個步驟:
1:使用配置的資料庫連接串,創建資料庫連接 Connection 對象
2:構建操作的sql語句
3:定嫌孝義command對象
4:打開數據連接
5:執芹叢稿行命令
舉一個例子,刪除操作
public class StudentService
{
//從配置文件中讀取資料庫連接字元串
private readonly static string connString = ConfigurationManager.ConnectionStrings["accpConnectionString"].ToString();
private readonly static string dboOwner = ConfigurationManager.ConnectionStrings["DataBaseOwner"].ToString();
AdoNetModels.Student model = new Student();
#region 刪除數據1
public int DeleteStudent(int stuID)
{
int result = 0;
// 資料庫連接 Connection 對象
SqlConnection connection = new SqlConnection(connString);
// 構建刪除的sql語句
string sql = string.Format("Delete From Student Where stuID={0}", stuID);
// 定義command對象
SqlCommand command = new SqlCommand(sql, connection);
try
{
connection.Open();
result = command.ExecuteNonQuery(); //鄭沒 執行命令
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
connection.Close();
}
return result;
}
#endregion