通达OA2017版连接sqlserver2008数据库

时间:2021-11-28 23:16:57
<?
$dbhost = '111.40.7.93,1433';
$dbuser ='sa'; //你的mssql用户名
$dbpass = '123456AAA'; //你的mssql密码
$dbname = 'sun1'; //你的mssql库名

$connect=odbc_connect("Driver={SQL Server};Server=$dbhost;Database=$dbname",$dbuser,$dbpass);
if($connect)
{
echo "连接成功!<br/>";
} else {
echo "链接失败!";
}

$sql="select * from table1";

$rs=odbc_exec($connect,$sql);
//$rs=odbc_do($connect,$sql); //do和exec都可以
if (!$rs) {
exit("Error in SQL");
}

while (odbc_fetch_row($rs))
{
$a_id=odbc_result($rs,"a_id");
$name=odbc_result($rs,"name");
echo $a_id."**".$name."<br>";

}
$sql5="select * from table1";

$rs5=odbc_exec($connect,$sql5);
while( $row = odbc_fetch_array($rs5)) {
print_r($row);
}

$sql2="INSERT INTO [sun1].[dbo].[table1] ( [name], [beizhu]) VALUES ( 'sun33', '5533')";
odbc_exec($connect,$sql2);

//odbc_free_result($Result);
$Result = odbc_exec($connect, "select @@identity");
$NewID = odbc_result($Result, 1);
echo $NewID."<BR>";//刚才插入的id
odbc_free_result($Result);

$sql3="UPDATE [sun1].[dbo].[table1] SET [name]='sun334', [beizhu]='55334' WHERE ([a_id]='14')";
odbc_exec($connect, $sql3);


$sql4="delete from [sun1].[dbo].[table1] WHERE ([a_id]='$NewID')";
odbc_exec($connect, $sql4);

$result=odbc_do($connect,$sql);
$q=odbc_num_rows($result);
echo $q;//显示的行数
odbc_close($connect);






?>

SQL 创建表的语句:

-- ----------------------------
-- Table structure for table1
-- ----------------------------
DROP TABLE [dbo].[table1]
GO
CREATE TABLE [dbo].[table1] (
[a_id] int NOT NULL IDENTITY(1,1) ,
[name] varchar(60) NOT NULL ,
[beizhu] text NOT NULL
)


GO
DBCC CHECKIDENT(N'[dbo].[table1]', RESEED, 23)
GO

-- ----------------------------
-- Indexes structure for table table1
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table table1
-- ----------------------------
ALTER TABLE [dbo].[table1] ADD PRIMARY KEY ([a_id])
GO