编辑: 问题被标记为重复。我希望看看是否有更简洁的方法将数据插入SQL,而不是如何插入它。
ORIGINAL: 正如标题所说,你能想到一种更简洁的方法来将这些数据插入到SQL中吗?
cntcn.Open()
Dim nsrtCmnd As String = ("INSERT INTO SQLTable1 (Column1, Column2, Column3,
Column4, Column5, Column6, Column7, Column8)
VALUES ('" & Data1 & "','" & Data2 & "','" & Data3 & "','" & Data4 & "',
'" & Data5 & "','" & Data6 & "','" & Data7 & "','" & Data8 & "');
SELECT SCOPE_IDENTITY()")
Dim qry As New SqlCommand(nsrtCmnd, cntcn)
Dim rslt As Integer = CInt(qry.ExecuteScalar())
sqlID = rslt
cntcn.Close()
答案 0 :(得分:0)
我创建了一个存储过程。
CREATE PROCEDURE [dbo].[MyStoredProcedure]
@Column1 nvarchar(200),
@Column2 nvarchar(200),
@Column3 nvarchar(200),
@Column4 nvarchar(200),
@Column5 nvarchar(200),
@Column6 nvarchar(200),
@Column7 nvarchar(200),
@Column8 nvarchar(200),
@outparm int output
AS
insert SQLTable1
(Column1, Column2, Column3, Column4,
Column5, Column6, Column7, Column8)
values
(@Column1, @Column2, @Column3, @Column4,
@Column5, @Column6, @Column7, @Column8)
select @outparm = SCOPE_IDENTITY()
然后通过以下方式调用:
Dim command As New SqlCommand("MyStoredProcedure", cntcn)
command.CommandType = CommandType.StoredProcedure
command.Parameters.Add("@Column1", Data1);
command.Parameters.Add("@Column2", Data2);
command.Parameters.Add("@Column3", Data3);
command.Parameters.Add("@Column4", Data4);
command.Parameters.Add("@Column5", Data5);
command.Parameters.Add("@Column6", Data6);
command.Parameters.Add("@Column7", Data7);
command.Parameters.Add("@Column8", Data8);
Dim parmout = new SqlParameter("@outparm", SqlDbType.Int);
parmout .Direction = ParameterDirection.Output;
command.Parameters.Add(parmout);
command.ExecuteNonQuery()
result = parmout.Value