我正在使用 MVC4 和 Visual Basic 来制作 Web 系统。但是当我必须将数据保存在我的表和关系中时,我需要保存在很多表中。所以,我这样做:
conn.Open()
Dim guardador_datos2 As New SqlCommand("INSERT INTO Creadores (ID_Creador,Usuario) VALUES (" + ultimo_id_creador.ToString() + ", '" + creada_por + "')", conn)
guardador_datos2.ExecuteNonQuery()
conn.Close()
conn.Open()
Dim guardador_datos3 As New SqlCommand("INSERT INTO ReunionCreada_X_Usuario (ID_Reunion,ID_Creador) VALUES (" + ultimo_id.ToString() + "," + ultimo_id_creador.ToString() + ")", conn)
guardador_datos3.ExecuteNonQuery()
conn.Close()
conn.Open()
Dim guardador_datos4 As New SqlCommand("INSERT INTO Reuniones_X_DepartamentoEmpresa (ID_Departamento_X_Empresa,ID_Reunion) VALUES (" + departamento.ToString() + "," + ultimo_id.ToString() + ")", conn)
guardador_datos4.ExecuteNonQuery()
conn.Close()
我打开和关闭SqlConnection
conn
了很多次(例如,我在这里只放了三个,但实际上有 9conn.Open
和Close
)。
难道没有更好的方法来做到这一点吗?就像只打开conn
1 次并执行一切?实际上,如果我这样做,它会向我发送一个错误,告诉我连接实际上是打开的