简体   繁体   English

VB.NET执行存储过程-编辑

[英]VB.NET Executing a store procedure - Edit

I am exporting database information into an excel file in a VB.NET MVC project. 我正在将数据库信息导出到VB.NET MVC项目中的excel文件中。

I have the excel part done and I am able to export a hard code table into excel. 我已经完成了excel部分,并且能够将硬代码表导出到excel中。 But I need to fill that table with fields from my database. 但是我需要用数据库中的字段填充该表。 I have a stored procedure that will get me all the information I need but I'm having trouble getting it to run and be able to let me go through and place the fields I need in the order I need them in. 我有一个存储过程,可以为我获取所有我需要的信息,但是我无法使其运行,并且无法让我按照需要的顺序遍历并放置所需的字段。

This is how I'm making my excel sheet if that helps. 如果可以的话,这就是我制作Excel工作表的方式。

Dim blah = New DataTable("testing")
blah.Columns.Add("Col1", Type.GetType("System.String"))
blah.Columns.Add("Col2", Type.GetType("System.String"))

'Below will be switched out for table data, this is where the try for the SQL will end up

blah.Rows.Add(1, "hello")
blah.Rows.Add(2, "hello")
blah.Rows.Add(3, "hello")
blah.Rows.Add(4, "hello")

Dim grid = New GridView()
grid.DataSource = blah
grid.DataBind()

Response.ClearContent()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment; filename=Test-Report.xls")
Response.ContentType = "application/ms-excel"
Response.Charset = ""

Dim sw = New StringWriter()
Dim htw = New HtmlTextWriter(sw)

grid.RenderControl(htw)

Response.Output.Write(sw.ToString())
Response.Flush()
Response.End()

EDIT---- I am having an issue with figuring out the syntax and values to use to run a stored procedure that has a return value and being able to iterate through it and take the row values. 编辑----我在弄清楚用于运行具有返回值的存储过程的语法和值时存在问题,并且能够迭代它并获取行值。 I've done a ton of google searches and most have stored procedures that don't have a return value or queries with one value being returned and I am returning a lot of fields. 我已经完成了大量的google搜索,并且大多数存储过程没有返回值,或者查询的返回值是一个值,并且我正在返回很多字段。

The stored procedure I'm running looks like this: 我正在运行的存储过程如下所示:

SELECT
    i.insurer_name
    ,cs.customer_name
    ,cs.customer_address
    ,cs.claim_number
    ,cs.work_type
    ,anu.FirstName
    ,anu.LastName
    ,l.location_name
    ,cs.completed_date
    ,q.question_text
    ,a.answer_text
FROM insurer i
inner join completed_survey cs on cs.insurer_id = i.insurer_id
inner join AspNetUsers anu on anu.Id = cs.user_id
inner join location l on l.location_id = cs.location_id
inner join answer a on a.complete_id = cs.completed_id
inner join question q on q.question_id = a.question_id
ORDER BY i.insurer_name, cs.survey_score desc

EDIT 2 ---- I'm not entirely sure how to answer what DB connection I'm using. 编辑2 ----我不太确定如何回答我正在使用的数据库连接。 I have a connection string in the web config and I'm using entity framework so most of my code is like 我在Web配置中有一个连接字符串,并且我使用的是实体框架,所以我的大部分代码都像

Private db As New SurveyDBModel
db.table_name.ToList()
db.table_name.Add(new_row)

As for your sample query, I'd use a View for a simple data manipulation like that... 至于您的示例查询,我将使用View这样的简单数据操作...

If you want to modify a data in DB, then a stored procedure comes handy and is easily executed from within SQL query... 如果要修改DB中的数据,则存储过程会很方便,并且可以在SQL查询中轻松执行...

EXEC dbo.MyStoredProc @MyKeyNo

It does not return data this way, the data are modified in the DB. 它不会以这种方式返回数据,而是在DB中修改数据。

Thanks to @ADyson, although the code you provided had a little more stuff to it, the base of it worked for me. 感谢@ADyson,尽管您提供的代码还有很多东西,但是它的基础对我有用。

One of my main problems was I didn't actually know I had to make a Model for the excel sheet to be able to get my fields to export. 我的主要问题之一是我实际上并不知道我必须为excel工作表制作模型才能将字段导出。 Also apparently my stored procedure didn't get transferred over so I need to do raw SQL. 同样显然我的存储过程没有转移过来,所以我需要做原始SQL。

Here's the basis of what ended up working: 这是最终工作的基础:

Dim blah = New DataTable("insurer")
blah.Columns.Add("Insurer Name", Type.GetType("System.String"))
blah.Columns.Add("Col2", Type.GetType("System.String"))

Using db As New SurveyDBModel()

Dim rsInsurer = db.Database.SqlQuery(Of report)("SELECT
i.insurer_name ,cs.customer_name ,cs.customer_address ,cs.claim_number ,cs.work_type ,anu.FirstName ,anu.LastName ,l.location_name ,cs.completed_date ,q.question_text ,a.answer_text
FROM insurer i
inner join completed_survey cs on cs.insurer_id = i.insurer_id
inner join AspNetUsers anu on anu.Id = cs.user_id
inner join location l on l.location_id = cs.location_id
inner join answer a on a.complete_id = cs.completed_id
inner join question q on q.question_id = a.question_id
ORDER BY i.insurer_name, cs.survey_score desc").ToList()

For Each item In rsInsurer
    blah.Rows.Add(item.insurer_name, "Bye")
 Next

 End Using

Thank you to all the people who tried to help me understand what the hey I was doing! 感谢所有试图帮助我了解我在做什么的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM