【问题标题】:Read from a visual basic api an SQL Server stored procedure从 Visual Basic api 读取 SQL Server 存储过程
【发布时间】:2018-08-27 18:26:12
【问题描述】:

有人能指出我正确的方向吗?我无法找到帮助我构建可以从 SQL Server 存储过程读取的 vb api 的文档。我目前有一个有效的 api,我能够测试我的控制器并且方法(GET、POST、)有效。请告诉我如何读取.sql 文件。

下面的代码在日历模型-Calendar.vb中

Imports System.Data.SqlClient
Imports System.IO

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim sqlFile As String = "C:\temp\test.sql"
        Dim sqlText = File.ReadAllText(sqlFile)
        Dim connStr = "Server=.\SQLEXPRESS; Database=testing; Integrated Security=SSPI"
        Dim conn = New SqlConnection(connStr)

        Dim sqlCmd = New SqlCommand(sqlText, conn)

        Try
            conn.Open()
            sqlCmd.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox("Something went wrong: " & ex.Message)
        Finally
            If conn.State = ConnectionState.Open Then
                conn.Close()
            End If
        End Try

    End Sub
End Class

【问题讨论】:

  • 我不明白您发布的代码与 API 有什么关系,或者您所说的“可以读取 sql 存储过程的 vb api”是什么意思。你为什么要从 .sql 文件中读取数据?
  • 你想从 sql-file 还是从 sql-database-table 读取?
  • 您的意思是test.sql 包含一个您想针对您要连接到的SQL Express 数据库运行的存储过程?如果是这样,那不是它的做法——你需要将存储过程添加到数据库中,然后你可以使用各种Execute 方法来执行它。
  • 能把test.sql的内容加进去吗?

标签: sql-server vb.net stored-procedures


【解决方案1】:

如果 test.sql 执行了一个存储过程,如果它返回一个表,你可以这样理解:

    Dim reader As SqlDataReader = sqlCmd.ExecuteReader()
    While reader.Read()
        Console.WriteLine("{0}", reader(0))
    End While

如果它返回一个值,使用这个

 intValue =Convert.ToInt32(sqlCmd.ExecuteScalar())

【讨论】:

  • 如果他使用的是c#,这将更加有用。
猜你喜欢
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多