⑴ 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