【问题标题】:Import data from Excel file to SQL Server database将数据从 Excel 文件导入 SQL Server 数据库
【发布时间】:2021-07-27 09:01:02
【问题描述】:

我想使用 Laravel 将 Excel 文件中的数据导入 SQL Server 数据库,但出现此错误

{
SQLSTATE[22007]:[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]La conversion d'un type de données nvarchar en type de données datetime a créé une valeur hors limites。 (SQL:插入 [users]([name]、[email]、[password]、[updated_at]、[created_at])
}

注意:当我将相同的文件导入到具有相同属性的 MySQL 表时,它就可以了。但是当我想在 SQL Server 数据库中做同样的事情时,我会收到错误消息

(userimport)    
    <?php
        
        namespace App\Imports;
        
        use App\Models\User;
        use Maatwebsite\Excel\Concerns\ToModel;
        use Maatwebsite\Excel\Concerns\WithHeadingRow;
        use Hash;
        
        class UserImport implements ToModel,WithHeadingRow
        {
            /**
            * @param array $row
            *
            * @return \Illuminate\Database\Eloquent\Model|null
            */
           
        
        
            public function model(array $row)
            {
                return new User([
                    'name' => $row['name'],
                    'email' => $row['email'], 
                    'password' => Hash::make('password')
                ]);
            }
        
        }
       
    
    
    ----------
    
    
    (Controller)
         public function import(Request $request)
            {
                //$file = $request->file('file');
                
                Excel::import(new UserImport,$request->file);
        
                //Excel::import(new UserImport,request()->file('file'));
                   
                
            }
      
    
    
    ----------
    
    
    (model)
         protected $fillable = [
                'name',
                'email',
                'password',
            ];




that's my code source from 3 classes(model,controller,Userimport)

【问题讨论】:

  • 你的问题整个应该是英文的;包括错误消息。我们中的许多人不会理解该错误的含义,这意味着我们无法为您提供帮助。
  • 我在您的代码中没有看到 [updated_at], [created_at]。该错误涉及到日期时间的转换,因此这些可能很重要
  • 是的,我的代码中没有 [updated_at]、[created_at] 导致它由 sql server 自动添加(日期系统)...这就是为什么我不明白这个问题
  • 刚刚转发了昨天的同一个问题!显然你删除了旧的。

标签: php sql-server laravel


【解决方案1】:

我不知道 Laravel 是什么,但如果你使用 Excel 和 SQL Server,你可以利用一些 VBA 代码来完成提升。

Sub InsertInto()

'Declare some variables
Dim cnn As adodb.Connection
Dim cmd As adodb.Command
Dim strSQL As String

'Create a new Connection object
Set cnn = New adodb.Connection

'Set the connection string
cnn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=your_database_name;Data Source=your_server_name"


'Create a new Command object
Set cmd = New adodb.Command

'Open the Connection to the database
cnn.Open

'Associate the command with the connection
cmd.ActiveConnection = cnn

'Tell the Command we are giving it a bit of SQL to run, not a stored procedure
cmd.CommandType = adCmdText

'Create the SQL
strSQL = "UPDATE TBL SET JOIN_DT = '2013-01-22' WHERE EMPID = 2"

'Pass the SQL to the Command object
cmd.CommandText = strSQL


'Execute the bit of SQL to update the database
cmd.Execute

'Close the connection again
cnn.Close

'Remove the objects
Set cmd = Nothing
Set cnn = Nothing

End Sub

最后,设置适当的参考。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    相关资源
    最近更新 更多