sql-server - 使用查询将数据从 Excel 导入 SQL Server
问题描述
如果不允许我使用 SQL Server 的导入功能将数据从 Excel 导入 SQL Server,您可以使用查询来导入数据吗?
解决方案
有多种方法可以将数据从 Excel 导出到 SQL Server。这是一个概念。
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=Northwind;Data Source=server_name"
'cnn.ConnectionString = "DRIVER=SQL Server;SERVER=server_name;DATABASE=Northwind;Trusted_Connection=Yes"
'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
推荐阅读
- angular - 为什么垫排序和分页器不起作用?
- ruby-on-rails - Rails ActiveRecord sanitize_sql 替换 ? 在字符串中
- google-cloud-platform - 谷歌 gcr 图像在哪里存储在谷歌存储中?
- c# - 加入未按预期工作(实体框架)
- php - 使用 php 和 ajax 从数据库中获取数据并显示到文本框
- php - MySQL vs Firebird:使用 PDO 访问 PHP 中的查询列
- user-interface - UI如何评估?
- iphone - 创建新会话是不可能的,因为“createSession”需要 HttpClient,
- angular - Angular FormGroup valueChanges 将属性转换为数组
- java - 我在 android apk 中执行命令,但我无法获得正确的返回