1

我想将此代码用于 php。是否可以将此代码转换为 PHP?

数据库:ODBC

- 提前致谢

USE [ECPNWEB]
GO

/****** Object:  Table [dbo].[tblMCWD]    Script Date: 11/16/2012 14:42:12 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tblMCWD](
    [ID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
    [ConsumerCode] [varchar](15) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [ConsumerName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [BillingPeriod] [datetime] NULL,
    [AccountStatus] [nchar](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [AccountNumber] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [DueDate] [datetime] NULL,
    [CurrentBill] [float] NULL,
    [PreviousBill] [float] NULL,
    [TotalDiscount] [float] NULL,
    [TotalGrossAmountDue] [float] NULL,
    CONSTRAINT [PK_tblMCWD] 
        PRIMARY KEY CLUSTERED ([ID] ASC)
        WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) 
        ON [PRIMARY]
GO

BULK INSERT tblmcwd
FROM 'c:\tblmcwd.txt'
WITH (
    FIELDTERMINATOR = '|',
    ROWTERMINATOR = '\n'
)
GO

/* Check the content of the table. */
SELECT *
FROM tblmcwd
GO

/* Drop the table to clean up database. */
DROP TABLE tblmcwd
GO
4

1 回答 1

1

这是一个开始......我不能保证它会工作,但......你需要测试它并调试它:

<?php
try {
    $dbh = new PDO("mssql:host={$hostname};dbname={$dbname}", $username, $password);
} catch (PDOException $e) {
    exit;
}
if (($handle = fopen("c:\tblmcwd.txt", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 4096, "|")) !== FALSE) {
        if (count($data) == 10) {
            $sql = "INSERT INTO [dbo].[tblMCWD] (
                        [ID], 
                        [ConsumerCode], 
                        [ConsumerName], 
                        [AccountStatus], 
                        [AccountNumber], 
                        [DueDate], 
                        [CurrentBill], 
                        [PreviousBill], 
                        [TotalDiscount], 
                        [TotalGrossAmountDue]
                    ) VALUES (
                        ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
                    )";

            $stmt = $dbh->prepare($sql);
            $stmt->bindValue(1, $data[0]);
            $stmt->bindValue(2, $data[1]);
            $stmt->bindValue(3, $data[2]);
            $stmt->bindValue(4, $data[3]);
            $stmt->bindValue(5, $data[4]);
            $stmt->bindValue(6, $data[5]);
            $stmt->bindValue(7, $data[6]);
            $stmt->bindValue(8, $data[7]);
            $stmt->bindValue(9, $data[8]);
            $stmt->bindValue(10, $data[9]);
            $stmt->execute();
        }
    }
    fclose($handle);
}
于 2012-12-04T06:09:36.957 回答