Friday, February 24, 2012

Can an adapter handle a stored procedures?!

Can someone please help on this issue and tell me how to amend this code:
<WebMethod()> _
Public Function GetRecord(ByVal anyname As String) As DataSet
Dim adapter As New SqlDataAdapter
Dim result As New DataSet
adapter.SelectCommand.Connection = Conn
adapter.SelectCommand.CommandType = CommandType.StoredProcedure
adapter.SelectCommand.CommandText = "GetSingleName"
adapter.SelectCommand.Parameters.Add("@.myname", SqlDbType.VarChar)
adapter.SelectCommand.Parameters("@.myname").Direction =
ParameterDirection.Input
adapter.SelectCommand.Parameters("@.myname").Value = anyname
adapter.Fill(result, "nabData")
Return result
This code is supposed to resturn dataset filled by an adapter that uses a
stored procedure called "GetSingleName". The Database table is "nabData". Th
e
connection is established through the GUI and is named Conn.
Many thanks in advance.Nab wrote:
> Can someone please help on this issue and tell me how to amend this code:
> <WebMethod()> _
> Public Function GetRecord(ByVal anyname As String) As DataSet
> Dim adapter As New SqlDataAdapter
> Dim result As New DataSet
> adapter.SelectCommand.Connection = Conn
> adapter.SelectCommand.CommandType = CommandType.StoredProcedure
> adapter.SelectCommand.CommandText = "GetSingleName"
> adapter.SelectCommand.Parameters.Add("@.myname", SqlDbType.VarChar)
> adapter.SelectCommand.Parameters("@.myname").Direction =
> ParameterDirection.Input
> adapter.SelectCommand.Parameters("@.myname").Value = anyname
> adapter.Fill(result, "nabData")
> Return result
> This code is supposed to resturn dataset filled by an adapter that uses a
> stored procedure called "GetSingleName". The Database table is "nabData".
The
> connection is established through the GUI and is named Conn.
--BEGIN PGP SIGNED MESSAGE--
Hash: SHA1
This is a question, better answered in a VB.NET newsgroup.
Wrist slapping done...
An example in C# (untested):
// create connection
String connString = "Data Source=(local);Integrated security=SSPI;" +
"Initial Catalog=Northwind;";
SqlConnection conn = new SqlConnection(connString);
// create a Command object based on a stored procedure
String selectSql = "MyStoredProcedure";
SqlCommand selectCmd = new SqlCommand(selectSql, conn);
selectCmd.CommandType = CommandType.StoredProcedure;
// create and set the parameter for the stored procedure
selectCmd.Parameters.Add("@.CustomerID", SqlDbType.NChar, 5);
selectCmd.Parameters["@.CustomerID"].Value = "VINET";
SqlDataAdapter da = new SqlDataAdapter(selecteCmd);
// create a new DataSet to receive the data
DataSet ds = new DataSet();
// read the data from stored procedure & load it into the DataSet
da.Fill(ds);
A good ADO.NET reference is _ADO.NET In a Nutshell_ (pub: O'Reilly).
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)
--BEGIN PGP SIGNATURE--
Version: PGP for Personal Privacy 5.0
Charset: noconv
iQA/ AwUBQhjdeoechKqOuFEgEQIg9QCgxmdNXsVA6uhC
wOZGYdYzwpAW+E4An2q8
rXQKtPYE2AAus6bK5zAe4beW
=IcRz
--END PGP SIGNATURE--

No comments:

Post a Comment