I have created a few stored functions in SQL Server that return a table via a select statement. Like so:
CREATE FUNCTION [dbo].[mFunSelectStudents] ()
RETURNS #result TABLE
(IDStudent int,
Name nchar(50),
Password nchar(50))
AS
BEGIN
INSERT INTO #result select * from School.dbo.Student
RETURN
END
I tried to assign the function to an SqlDataAdapter in c# like this:
SqlCommand cmd = new SqlCommand("mFunSelectStudents", con);
SqlDataAdapter adpStudents = new SqlDataAdapter();
adpStudents.SelectCommand = cmd;
But this doesn't work..
Where #result is a return parameter of the stored function. Now, how do I call the function in C# and assign the data to a grid ?
Any help is appreciated..
The command cannot be just the name of the function. You are supposed to put a SQL command there, and in SQL one retrieves data from a TVF by SELECTing from it, like this:
SELECT * FROM dbo.mFunSelectStudents()
Consequently, the first line of your C# code snippet should be:
SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.mFunSelectStudents()", con);
Wrap the function in a stored procedure, or do the work itself in a SP. The results of a single select statement will be accessible as a DataTable in the C# client.
create proc selectSomeData
.
.
.
In the client, your commandType would be StoredProcedure and the CommandText would be the name of the sp.
Your first line :
SqlCommand cmd = new SqlCommand("mFunSelectStudents", con);
Is correct, however this one you should check it
SqlDataAdapter adpStudents.SelectCommand = cmd;
First you need to use new with the SqlDataAdapter before you can assign the selectCommand, as follows:
SqlDataAdapter adpStudents = new SqlDataAdapter(cmd);
Assign the command to the DataAdapter
adpStudents.SelectCommand = cmd;
And then you prepare a Dataset
DataSet ds = new DataSet();
Fill it with your DataAdapter
adpStudents.Fill(ds);
Assign it to your grid
gridName.DataSource = ds;
And call DataBind to update the info on the grid
gridName.DataBind();
Related
In How to use a DataAdapter with stored procedure and parameter, the data adapter's selectCommand property has been used. Can the same be used if a stored procedure updates as well as retrieves data from a database?
On implementing it, and using the selectCommand (and not the property, it seems to work alright.
...
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlQuery, sqlConnection);
foreach (SqlParameter sqlParameter in sqlParameterCollection)
{
sqlCommand.Parameters.Add(new SqlParameter(sqlParameter.ParameterName, sqlParameter.Value));
}
sqlDataAdapter.SelectCommand = sqlCommand;
DataSet dataSet = new DataSet();
sqlDataAdapter.Fill(dataSet);
...
The short answer is yes. Passing parameters to a stored procedure which updates and returns values is no different from the SqlDataAdapter side compared to a stored procedure that only returns values based on parameters passed in.
How can i use an SqlDataAdapter to execute an sql stored procedure that contains statements which update one table and insert into another table, but returns no value?
EDIT:There are answers related to the execution of stored procedures with SELECT statements. However, that's not what i'm looking for.
I'm assuming you're using the SqlDataAdapter to populate a DataTable or DataSet object. If this is the case and you want the changes to the DataTable/DataSet to be persisted to your database and you require using stored procs then you'd need to set the UpdateCommand, InsertCommand, and/or DeleteCommand properties.
Example:
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM WHATEVER", connection);
adapter.UpdateCommand = new SqlCommand()
{
Connection = connection,
CommandText = "EXEC myStoredProc",
CommandType = CommandType.StoredProcedure
};
//Don't forget to set your parameters!
Otherwise if you don't need the adapter just use a SqlCommand and call ExecuteNonQuery()
If the statement returns no data, you wouldn't use a SqlDataAdapter. Just use a SqlCommand and call ExecuteNonQuery().
I know that tableadapter does not support the OVER SQL command as in the following SQL statement
SELECT * FROM
(
SELECT ROW_NUMBER() OVER(ORDER BY dbo.alerts.id) NUM,
* FROM dbo.Alerts
) A
WHERE NUM > #StartRow AND NUM < #EndRow
how would you make the equivalent statement so that the dataset designer will support such queries
Basicly I want to be able to select a subset of rows to present to the UI without loading the whole dataset. Maybe I am going about this the wrong way, any guidance would be appreciated
I don't know if this is the direction you want to take, but you could put that sql statement in a stored procedure and use the sproc's parameters to filter the desired subset and then have that get loaded into the dataset.
Doing something like the following:
SqlCommand sqlCommand = new SqlCommand(.........);
sqlCommand.CommandText = "theStoredProcedureNameHere";
sqlCommand.CommandType = CommandType.StoredProcedure;
SqlParameter paramWhatever1 = new SqlParameter(......);
SqlParameter paramWhatever2 = new SqlParameter(......);
sqlCommand.Parameters.Add(paramWhatever1);
sqlCommand.Parameters.Add(paramWhatever2);
SqlDataAdapter sqlAdapter = new SqlDataAdapter();
sqlAdapter.SelectCommand = sqlCommand;
DataSet ds = new DataSet();
sqlAdapter.Fill(ds);
Put your query in a stored procedure instead and call it to fill your dataset
Through designer I have created a typed data set and included stored procedures for insert / update / delete. The problem is now, how to call those stored procedures? How to actually change data in database this way? And how to receive answer from db (number of rows changed)?
try this for get data from database.
DataSet ds = new DataSet("dstblName");
using(SqlConnection conn = new SqlConnection("ConnectionString"))
{
SqlCommand sqlComm = new SqlCommand("spselect", conn);
sqlComm.Parameters.AddWithValue("#parameter1", parameter1value);
sqlComm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = sqlComm;
da.Fill(ds);
}
Similarly you need to call "spdelte" etc.
I found out that far easiest way is through designer - create table adapter and simply set it to call stored procedure. No extra typing needed, arguments are also added to procedure call.
Can anyone tell me how I can control the output from an SQL stored procedure that returns more than one set of output?
I am currently doing the following:
DataTable allData = new DataTable();
SqlConnection connection = new SqlConnection(mySource);
SqlCommand cmd = new SqlCommand(procedureName, connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(paramName, SqlDbType.Int);
cmd.Parameters[paramName].Value = paramValue;
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(allData);
connection.Close();
Now this works fine if the procedure has only one output value, but how do I deal with the following:
My stored procedure broadly does the following:
It calls a number of other stored procedures in order to construct a dynamic SQL query (lets call this #query) and then calls EXECUTE(#query) which does a SELECT.
Using the code snippet above returns the result from the SELECT query, which is fine. But what I would also like it so have the string #query returned. I can specify it as an output type and let SQL fetch it, but how do I access it from c#? (Actually, more specifically, when I do this the code snipped above returns only the string #query and no longer returns the results of the SELECT)
Thanks
Karl
You can do like this:
DataSet allData = new DataSet ();
...
...
...
adapter.Fill(allData);
then each result of the select is in different dataTable
Using SqlDataReader.NextResult .
This little bit shifts you from using SqlDataAdapter, but you still is able to populate DataTable with DataTable.Load