通过单击 .Net 网页中的按钮来运行存储过程

发布于 2024-12-12 07:56:03 字数 1308 浏览 0 评论 0原文

下午,

我在 SQL 2005 数据库中有一个名为 GasNominationsRawData_Insert 的存储过程。

执行时,它只是从另一个数据库中提取一些数据并将结果插入到表中。这个查询工作正常。

我需要做的是通过单击网页上的按钮来执行此操作。我在互联网上很幸运,成功地在我的 .net 2008 网页中创建了以下代码。但我认为我要么遗漏了一些东西,要么我的代码完全错误。我是 .Net 编程新手,但我知道我需要声明存储过程、创建 SQL 连接、创建命令行、打开连接、执行查询,然后关闭连接。

我不需要或没有任何参数。本质上,这个按钮只是用来将数据推送到表中。

导入系统数据 导入 System.Data.SqlClient

部分类 RawData 继承 System.Web.UI.Page

Protected Sub btnAddRawData_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddRawData.Click

    'Declare Stored Procedure
    Dim GasNominationsRawData_Insert As String = "GasNominationsRawData_Insert"

    'Declare SQL Connection (This is the connection string located on the web.config page)
    Dim SQLConn As SqlConnection
    SQLConn = New SqlConnection("GasNominationsDataConnectionString")

    'Declare command
    Dim SqlComm As SqlCommand = New SqlCommand("GasNominationsRawData_Insert", SQLConn)
    SqlComm.CommandType = CommandType.StoredProcedure

    Try
        'Open SQL Connection
        SQLConn.Open()

        'Execute Query
        SqlComm.ExecuteNonQuery()

        'Close connection
        SQLConn.Close()

    Catch ex As Exception
        Throw (ex)

    End Try

End Sub

End Class

非常感谢任何帮助。

问候贝蒂

Afternoon All,

I have a stored procedure in an SQL 2005 database named GasNominationsRawData_Insert.

When executed this simply extracts some data from another database and inserts the result into a table. This query works fine.

What i need to do is enable this to be executed on the click on a button on my web page. I have had a good luck around the internet have have managed to create the following code in my .net 2008 web page. But i think im either missing something or i have have completly the wrong code. Im new to programming in .Net but i understand that i need to delclare the stored procedure, create the SQL connection, create the command line, open the conection, execute the query and then close the connection.

I dont need or have any parameters. Essentially this button is just used to push data to a table.

Imports System.Data
Imports System.Data.SqlClient

Partial Class RawData
Inherits System.Web.UI.Page

Protected Sub btnAddRawData_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddRawData.Click

    'Declare Stored Procedure
    Dim GasNominationsRawData_Insert As String = "GasNominationsRawData_Insert"

    'Declare SQL Connection (This is the connection string located on the web.config page)
    Dim SQLConn As SqlConnection
    SQLConn = New SqlConnection("GasNominationsDataConnectionString")

    'Declare command
    Dim SqlComm As SqlCommand = New SqlCommand("GasNominationsRawData_Insert", SQLConn)
    SqlComm.CommandType = CommandType.StoredProcedure

    Try
        'Open SQL Connection
        SQLConn.Open()

        'Execute Query
        SqlComm.ExecuteNonQuery()

        'Close connection
        SQLConn.Close()

    Catch ex As Exception
        Throw (ex)

    End Try

End Sub

End Class

Any Help is much appreciated.

Regards Betty

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

今天小雨转甜 2024-12-19 07:56:03

乌普斯。我忽略了这一点。您正在尝试使用连接字符串“GasNominationsDataConnectionString”连接到数据库。这当然不是正确的连接字符串。
您的连接字符串应该看起来像这样:
“数据源=myServerAddress;初始目录=myDataBase;用户ID=myUsername;密码=myPassword;”
因此,您必须首先从 web.config 中获取连接字符串,例如
Dim GasNominationsDataConnectionString As String = ConfigurationManager.ConnectionStrings("GasNominationsDataConnectionString").ConnectionString

问候
安雅

Upps. I overlooked that. You're trying to connect to your database with the connectionstring: "GasNominationsDataConnectionString". That's of course no correct connection string.
Your connection string should look somehow like that:
"Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
So you have to get your connection string first out of your web.config like
Dim GasNominationsDataConnectionString As String = ConfigurationManager.ConnectionStrings("GasNominationsDataConnectionString").ConnectionString

Regards
Anja

笑脸一如从前 2024-12-19 07:56:03

乍一看还不错。那么点击按钮会发生什么?您确定单击按钮时会调用 btnAddRawData_Click 吗?

Looks fine at a first glance. So what happens on clicking the button? Have you assured that your btnAddRawData_Click is called when the button is clicked?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文