在vb中执行存储过程

时间:2019-02-21 13:15:58

标签: sql vb.net stored-procedures

我正在使用当前代码尝试执行存储过程,它没有显示任何错误,但是未执行该过程所做的更新。

import timeit

rep = 100000
n = 5

setup = """
from collections import Counter
from functools import reduce
x = ['potato', 'pie']
z = ['chicken','chicken','pie','donkey','potato','potato']
all_lists = [x, z]
items = set(x + z)
list_final = []
dict_final = {}
"""

fcts = {
        'Counter/reduce':
            'Counter(reduce(lambda x, z: x + list(set(z)), all_lists, [])).most_common()',
        'Counter/reduce into dict':
            'dict(Counter(reduce(lambda x, z: x + list(set(z)), all_lists, [])).most_common())',
        'double for loop dict':
            '''for i in items:
                for lst in (x, z):
                    if i in lst:
                        dict_final[i] = dict_final.get(i, 0) + 1
                        ''',
        'for loop LC into dict':
            '''for i in items:
    cntr = sum([i in lst for lst in (x, z)])
    dict_final[i] = cntr''',
        'for loop LC into list':
            '''for i in items:
    cntr = sum([i in lst for lst in (x, z)])
    list_final.append((i, cntr))''',
        'LC into LC':
            '[(i, sum([1 for lst in (x, z) if i in lst])) for i in items]',
        'LC into DC':
            '{i: sum([i in lst for lst in (x, z)]) for i in items}'
        }

L = max((len(k) for k in fcts.keys()))
results = []
for k, v in fcts.items():
    print(f'timing {k}...')
    tt = timeit.Timer(v, setup=setup)
    t = min(tt.repeat(repeat=rep, number=n))
    results.append((k, t))
for i, (k, t) in enumerate(sorted(results, key=lambda x: x[1])):
    print(f'{i+1}. {k:{L}}:   {t:.3}')

SQL存储过程如下:

Public Function UpdateRouteByRegister(ByVal conexion As String, ByVal idRegister As Integer, ByVal route As String) As Boolean
    Try
        ConnectionString = conexion
        myConnection = New SqlConnection(ConnectionString)
        myDataAdapter = New SqlDataAdapter("spUpdateHistoricLoad", myConnection)
        myDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

        myDataAdapter.SelectCommand.Parameters.Add(New SqlParameter("@idRegister", SqlDbType.Int))
        myDataAdapter.SelectCommand.Parameters("@idRegister").Value = idRegister

        myDataAdapter.SelectCommand.Parameters.Add(New SqlParameter("@route", SqlDbType.VarChar, 100))
        myDataAdapter.SelectCommand.Parameters("@route").Value = route

        myDataAdapter.SelectCommand.CommandTimeout = 0
        myDataset = New DataSet
        myDataAdapter.Fill(myDataset)
        myDataset.Dispose()
        myConnection.Close()
        Log("******* Register updated = " + idRegister.ToString + " *******")
        Return True
    Catch objException As Exception
        Log("******* Error on updating register = " + idRegister.ToString + " *******")
        Return False
    End Try
End Function

1 个答案:

答案 0 :(得分:1)

我建议删除DataAdapter和DataSet对象,只使用SqlCommand

Public Function UpdateRouteByRegister(ByVal conexion As String, ByVal idRegister As Integer, ByVal route As String) As Boolean
Try
    ConnectionString = conexion
    myConnection = New SqlConnection(ConnectionString)
    myConnection.Open
    Dim cmd as SqlCommand
    cmd = New SqlCommand("spUpdateHistoricLoad", myConnection)
    cmd.Parameters.AddWithValue("@idRegister", idRegister)
    cmd.Parameters.AddWithValue("@route", route)
    cmd.CommandTimeout = 0
    cmd.ExecuteNonQuery
    myConnection.Close()
    Log("******* Register updated = " + idRegister.ToString + " *******")
    Return True
Catch objException As Exception
    Log("******* Error on updating register = " + idRegister.ToString + " *******")
        Return False
End Try
End Function