I am working on a program in VB.Net that interfaces with a database using LINQ. Right now I am attempting to grab data from the following stored procedure on the database:
CREATE PROCEDURE dbo.GetTableColumn(
@ColName VARCHAR(MAX),
@TblName VARCHAR(MAX),
@Result BIT OUT
) AS
BEGIN
IF (EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @TblName AND COLUMN_NAME = @ColName))
BEGIN
DECLARE @SQL VARCHAR(MAX)
SET @SQL = 'SELECT ' + @ColName + ' FROM ' + @TblName
EXEC (@SQL)
SET @Result = 1
END
ELSE
SET @RESULT = 0
RETURN @Result
END
The goal of the procedure, is that I can send a column name, and table name as variables, and if the column exists within the table the data from the table is returned.
I have added the procedure to my DataLinqContext, and this is the code that references the procedure:
Public Function GetTableColumn(ByVal col As String, ByVal table As String) As AutoCompleteStringCollection
GetTableColumn = New AutoCompleteStringCollection
Using dbContext As New Customer_LINQDataContext
Dim result As Boolean
Dim query = dbContext.GetTableColumn(col, table, result)
MessageBox.Show(query.ToString())
End Using
End Function
Now I put in the Message Box dialogue to test the output data. However every time the method runs, the result is always 1
or 0
. I can never get the actual data I am trying to query for.
So what am I doing wrong here, and how do I fix it?
EDIT: I asked over on the Database Admin Stack Exchange to see if anyone there could offer any help. I got the following answer that can hopefully help, but I am still unsure how to fix the problem.
Your code is obtaining the result of the RETURN @result at the end of the stored proc. The results you want are being passed out of the stored procedure as a ResultSet. You need to use a DataReader object to see the rows.
I'm not sure how to do that using Linq. You'll hopefully get a better answer on your StackOverflow question.