function DoSqlInt(strSQL As String)as int32
    Dim objConn As New SqlConnection()
    objConn.ConnectionString = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BugSys;Data Source=."
    objConn.Open()
    Dim objCmd As New SqlCommand()
    objCmd.Connection = objConn
    objCmd.CommandText = strSQL
    Dim objReader as SqlDataReader=objCmd.ExecuteReader()
    Dim val As int32
    objReader.Read()
    val=Cint(objReader.GetValue(0))  
    DoSqlInt = val 
    objCmd = Nothing
    objConn.Close()
    objReader.Close()
    End function
--------------------
我是这样写的 好象有问题

解决方案 »

  1.   

    可以写一个存储过程,返回@@indentity变量
      

  2.   

    SqlCommand myCommand=new SqlCommand("select @@Identity",myconection);
    int add_id=0;
    try{   add_id=Convert.ToInt32(myCommand.ExecuteScalar()); }
    catch{ add_id=0; }
      

  3.   

    你的CommandText是不是用存储过程的?是返回存储过程中的参数吗?如果是的话可以在存储过程中定义变量时加一个"output":
    declare @@Identity output
    然后在程序中可以用objCmd("@@Identity")得到它的值。
    或者在存储过程最末尾的GO前面加select @@Identity然后在程序中Execute这个存储过程。