如何将参数传递给vb.net中的sql视图? [英] How to pass a parameter to view of sql in vb.net?

查看:85
本文介绍了如何将参数传递给vb.net中的sql视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,



我正在使用vb.net n sql server2005。我创建了视图。我想把它们连接到水晶报告。我的观点包含参数。我如何将它们传递到按钮单击的视图,以便我的视图将获得参数,它将连接到水晶报告?



请帮助我

hello Friends,

i am using vb.net n sql server2005. i have created views. n i want to connect them to a crystal reports. My views contains parameters. How can i pass them to a view on a button click, so that my view will get parameter and it will connect to a crystal report?

Please help me out

推荐答案



尝试使用存储过程或SQL函数而不是View。



Hi ,
Try With Stored Procedure or with SQL Functions instead of View.

CREATE PROCEDURE Test
(
    @testNo INT
) 
AS 
SELECT
    * 
FROM
    test
WHERE 
    testNo=@testNo

For example Function like

CREATE FUNCTION fn_test
(   
    @testNo INT
)
RETURNS TABLE 
AS
RETURN 
(
    SELECT    
    	* 
    FROM    
    	test
    WHERE     
    	testNo=@testNo
)


1。只需在水晶报告上连接您的视图,这只是为了进行设计。不要担心WHERE子句(参数),您可以稍后在您的vb.net代码上设置数据源。



Ex。

将[view]连接到水晶报告后,您现在可以像ff一样填充水晶报告。



1. Just Connect your view on the crystal report,this is just to make the design. don't worry about the WHERE clause(parameter) you can set the data source later on your vb.net code.

Ex.
after you have connected your [view] to the crystal report you can now populate the crystal report like the ff.

Dim Dt As New DataTable
   'Get records
   Private Sub GetRecords()
       Using con As New SqlClient.SqlConnection("YourConnectionString")
           Using com As New SqlClient.SqlCommand("SELECT * FROM [View] WHERE [ID]=@ID;", con)
               com.Parameters.AddWithValue("@ID", "SomeValue")
               Using da As New SqlClient.SqlDataAdapter(com)
                   Dt.Clear()
                   da.Fill(Dt)
               End Using
           End Using
       End Using
   End Sub

   'Set the report
   Private Sub SetReport()
       Dim m_ReportFullPath As String = "Path\of\your\report.rpt"
       Dim MyReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
       MyReport.Load(m_ReportFullPath)
       MyReport.SetDataSource(Dt)
       CrystalReportViewer1.ReportSource = MyReport
   End Sub


这篇关于如何将参数传递给vb.net中的sql视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆